From fc444441c9abc0f76c49161bdd51dce9ad893d17 Mon Sep 17 00:00:00 2001 From: Derek Leadbetter Date: Tue, 20 Sep 2022 10:45:01 -0400 Subject: [PATCH 1/2] UDF #5 - Adding filter for defineable to user_defined_fields_controller --- .../user_defined_fields_controller.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/controllers/user_defined_fields/user_defined_fields_controller.rb b/app/controllers/user_defined_fields/user_defined_fields_controller.rb index b18cc88..e7bc859 100644 --- a/app/controllers/user_defined_fields/user_defined_fields_controller.rb +++ b/app/controllers/user_defined_fields/user_defined_fields_controller.rb @@ -1,5 +1,23 @@ module UserDefinedFields class UserDefinedFieldsController < ApplicationController search_attributes :table_name, :column_name + + protected + + def apply_filters(query) + query = super + + query = filter_defineable(query) + + query + end + + private + + def filter_defineable(query) + return query unless params[:defineable_id].present? && params[:defineable_type].present? + + query.where(defineable_id: params[:defineable_id], defineable_type: params[:defineable_type]) + end end end From 0e99e637d72065df91886e2414845031905b6658 Mon Sep 17 00:00:00 2001 From: Derek Leadbetter Date: Tue, 20 Sep 2022 10:45:17 -0400 Subject: [PATCH 2/2] UDF #5 - Updating README.md with component usage --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 98b8e44..563829a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ $ gem install user_defined_fields ## Usage -#### Migrations +### Migrations To install the database table necessary to support user defined fields, use the following command: ```bash @@ -49,5 +49,31 @@ class AddUserDefinedFieldsToMyModel < ActiveRecord::Migration[7.0] end ``` +### Components + +User defined fields can be configured one of two ways: At the application level, or at the model level. + +##### Application Level +If configured at the application level, each record in the specified table will contain the same user defined fields. + +For example: You could define `first_name` and `last_name` fields for all of the `people` records. Anytime a user is editing a a person form, the `first_name` and `last_name` fields will be available as inputs. + +```jsx + +``` + +##### Model Level +If configured at the model level, records can contain different fields dependent on a parent record. + +For example: You could define a `location` field for `resources` that belong to Project A, then define a `project_stage` field for `resources` that belong to Project B. On the edit for for Project A, only the `location` field will be available. On the edit form for Project B, only the `project_stage` field will be available. + +```jsx + +``` + ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).