From f1686ae6bad2fcc8461d3bff3ab92924a8c7c822 Mon Sep 17 00:00:00 2001 From: Derek Leadbetter Date: Wed, 5 Oct 2022 17:02:48 -0400 Subject: [PATCH 1/2] UDF #21 - Adding table_name filter to user_defined_fields_controller --- .../user_defined_fields/user_defined_fields_controller.rb | 8 ++++++++ 1 file changed, 8 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 e7bc859..824ffbf 100644 --- a/app/controllers/user_defined_fields/user_defined_fields_controller.rb +++ b/app/controllers/user_defined_fields/user_defined_fields_controller.rb @@ -9,6 +9,8 @@ def apply_filters(query) query = filter_defineable(query) + query = filter_table(query) + query end @@ -19,5 +21,11 @@ def filter_defineable(query) query.where(defineable_id: params[:defineable_id], defineable_type: params[:defineable_type]) end + + def filter_table(query) + return query unless params[:table_name].present? + + query.where(table_name: params[:table_name]) + end end end From 76b5f8b3dd477206fd14a17bc28ad9eddae5a05d Mon Sep 17 00:00:00 2001 From: Derek Leadbetter Date: Thu, 6 Oct 2022 11:23:28 -0400 Subject: [PATCH 2/2] UDF #21 - Updating validation to only include fields for the current model --- app/models/concerns/user_defined_fields/fieldable.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/user_defined_fields/fieldable.rb b/app/models/concerns/user_defined_fields/fieldable.rb index 8e1e58e..388191f 100644 --- a/app/models/concerns/user_defined_fields/fieldable.rb +++ b/app/models/concerns/user_defined_fields/fieldable.rb @@ -27,9 +27,10 @@ def where_fieldable(name, value) private def validate_user_defined_fields - defineable = self.class.resolve_defineable&.call(self) + query = UserDefinedField + .where(table_name: self.class.to_s) - query = UserDefinedField.all + defineable = self.class.resolve_defineable&.call(self) if defineable query = query.where(