From d7a09fb08de31dbeed55428b84e0a660dbd5cf7a Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Thu, 14 Dec 2023 11:06:21 -0600 Subject: [PATCH] DEV: Add true_fields method for CustomFields (#24876) This is useful for plugins that might otherwise rely on the CUSTOM_FIELD_TRUE constant. --- app/models/category_custom_field.rb | 2 ++ app/models/concerns/custom_field.rb | 11 +++++++++ app/models/group_custom_field.rb | 2 ++ app/models/post_custom_field.rb | 2 ++ app/models/topic_custom_field.rb | 2 ++ app/models/user_custom_field.rb | 2 ++ spec/lib/concern/has_custom_fields_spec.rb | 26 ++++++++++++++++++++++ 7 files changed, 47 insertions(+) create mode 100644 app/models/concerns/custom_field.rb diff --git a/app/models/category_custom_field.rb b/app/models/category_custom_field.rb index 988d00e9108..19b790a92a7 100644 --- a/app/models/category_custom_field.rb +++ b/app/models/category_custom_field.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class CategoryCustomField < ActiveRecord::Base + include CustomField + belongs_to :category end diff --git a/app/models/concerns/custom_field.rb b/app/models/concerns/custom_field.rb new file mode 100644 index 00000000000..2c0e89e15fc --- /dev/null +++ b/app/models/concerns/custom_field.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module CustomField + extend ActiveSupport::Concern + + class_methods do + def true_fields + where(value: HasCustomFields::Helpers::CUSTOM_FIELD_TRUE) + end + end +end diff --git a/app/models/group_custom_field.rb b/app/models/group_custom_field.rb index 6cc32953355..f88189e4496 100644 --- a/app/models/group_custom_field.rb +++ b/app/models/group_custom_field.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class GroupCustomField < ActiveRecord::Base + include CustomField + belongs_to :group end diff --git a/app/models/post_custom_field.rb b/app/models/post_custom_field.rb index 56cf46a90b6..91384800b96 100644 --- a/app/models/post_custom_field.rb +++ b/app/models/post_custom_field.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class PostCustomField < ActiveRecord::Base + include CustomField + belongs_to :post end diff --git a/app/models/topic_custom_field.rb b/app/models/topic_custom_field.rb index ba5767084e5..6a8a799bad3 100644 --- a/app/models/topic_custom_field.rb +++ b/app/models/topic_custom_field.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class TopicCustomField < ActiveRecord::Base + include CustomField + belongs_to :topic end diff --git a/app/models/user_custom_field.rb b/app/models/user_custom_field.rb index fde4b371c6a..e4d9b4f9182 100644 --- a/app/models/user_custom_field.rb +++ b/app/models/user_custom_field.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class UserCustomField < ActiveRecord::Base + include CustomField + belongs_to :user scope :searchable, diff --git a/spec/lib/concern/has_custom_fields_spec.rb b/spec/lib/concern/has_custom_fields_spec.rb index 4987f74a284..8c7edc9f115 100644 --- a/spec/lib/concern/has_custom_fields_spec.rb +++ b/spec/lib/concern/has_custom_fields_spec.rb @@ -17,6 +17,8 @@ RSpec.describe HasCustomFields do end class CustomFieldsTestItemCustomField < ActiveRecord::Base + include CustomField + belongs_to :custom_fields_test_item end end @@ -196,6 +198,30 @@ RSpec.describe HasCustomFields do expect(db_item.custom_fields).to eq("a" => %w[b 10 d]) end + it "that are true can be fetched" do + test_item = CustomFieldsTestItem.new + CustomFieldsTestItem.register_custom_field_type("bool", :boolean) + + test_item.save! + + expect( + CustomFieldsTestItemCustomField + .true_fields + .where(custom_fields_test_item_id: test_item.id) + .count, + ).to eq(0) + + test_item.custom_fields["bool"] = true + test_item.save! + + expect( + CustomFieldsTestItemCustomField + .true_fields + .where(custom_fields_test_item_id: test_item.id) + .count, + ).to eq(1) + end + it "supports type coercion" do test_item = CustomFieldsTestItem.new CustomFieldsTestItem.register_custom_field_type("bool", :boolean)