mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
DEV: Add true_fields method for CustomFields (#24876)
This is useful for plugins that might otherwise rely on the CUSTOM_FIELD_TRUE constant.
This commit is contained in:

committed by
GitHub

parent
74f964f2b4
commit
d7a09fb08d
@ -1,6 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class CategoryCustomField < ActiveRecord::Base
|
class CategoryCustomField < ActiveRecord::Base
|
||||||
|
include CustomField
|
||||||
|
|
||||||
belongs_to :category
|
belongs_to :category
|
||||||
end
|
end
|
||||||
|
|
||||||
|
11
app/models/concerns/custom_field.rb
Normal file
11
app/models/concerns/custom_field.rb
Normal file
@ -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
|
@ -1,6 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class GroupCustomField < ActiveRecord::Base
|
class GroupCustomField < ActiveRecord::Base
|
||||||
|
include CustomField
|
||||||
|
|
||||||
belongs_to :group
|
belongs_to :group
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class PostCustomField < ActiveRecord::Base
|
class PostCustomField < ActiveRecord::Base
|
||||||
|
include CustomField
|
||||||
|
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class TopicCustomField < ActiveRecord::Base
|
class TopicCustomField < ActiveRecord::Base
|
||||||
|
include CustomField
|
||||||
|
|
||||||
belongs_to :topic
|
belongs_to :topic
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class UserCustomField < ActiveRecord::Base
|
class UserCustomField < ActiveRecord::Base
|
||||||
|
include CustomField
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
scope :searchable,
|
scope :searchable,
|
||||||
|
@ -17,6 +17,8 @@ RSpec.describe HasCustomFields do
|
|||||||
end
|
end
|
||||||
|
|
||||||
class CustomFieldsTestItemCustomField < ActiveRecord::Base
|
class CustomFieldsTestItemCustomField < ActiveRecord::Base
|
||||||
|
include CustomField
|
||||||
|
|
||||||
belongs_to :custom_fields_test_item
|
belongs_to :custom_fields_test_item
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -196,6 +198,30 @@ RSpec.describe HasCustomFields do
|
|||||||
expect(db_item.custom_fields).to eq("a" => %w[b 10 d])
|
expect(db_item.custom_fields).to eq("a" => %w[b 10 d])
|
||||||
end
|
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
|
it "supports type coercion" do
|
||||||
test_item = CustomFieldsTestItem.new
|
test_item = CustomFieldsTestItem.new
|
||||||
CustomFieldsTestItem.register_custom_field_type("bool", :boolean)
|
CustomFieldsTestItem.register_custom_field_type("bool", :boolean)
|
||||||
|
Reference in New Issue
Block a user