DEV: Don't define methods in an included block (#24433)

This commit is contained in:
Daniel Waterworth 2023-11-17 12:22:32 -06:00 committed by GitHub
parent c55c354a13
commit 0c712eaa3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -59,23 +59,10 @@ module HasCustomFields
CUSTOM_FIELDS_MAX_ITEMS = 100 CUSTOM_FIELDS_MAX_ITEMS = 100
CUSTOM_FIELDS_MAX_VALUE_LENGTH = 10_000_000 CUSTOM_FIELDS_MAX_VALUE_LENGTH = 10_000_000
included do module ClassMethods
attr_reader :preloaded_custom_fields
has_many :_custom_fields, dependent: :destroy, class_name: "#{name}CustomField"
validate :custom_fields_max_items, unless: :custom_fields_clean?
validate :custom_fields_value_length, unless: :custom_fields_clean?
after_save :save_custom_fields
def custom_fields_fk
@custom_fields_fk ||= "#{_custom_fields.reflect_on_all_associations(:belongs_to)[0].name}_id"
end
# To avoid n+1 queries, use this function to retrieve lots of custom fields in one go # To avoid n+1 queries, use this function to retrieve lots of custom fields in one go
# and create a "sideloaded" version for easy querying by id. # and create a "sideloaded" version for easy querying by id.
def self.custom_fields_for_ids(ids, allowed_fields) def custom_fields_for_ids(ids, allowed_fields)
klass = "#{name}CustomField".constantize klass = "#{name}CustomField".constantize
foreign_key = "#{name.underscore}_id".to_sym foreign_key = "#{name.underscore}_id".to_sym
@ -94,21 +81,21 @@ module HasCustomFields
result result
end end
def self.append_custom_field(target, key, value) def append_custom_field(target, key, value)
HasCustomFields::Helpers.append_field(target, key, value, @custom_field_types) HasCustomFields::Helpers.append_field(target, key, value, @custom_field_types)
end end
def self.register_custom_field_type(name, type) def register_custom_field_type(name, type)
@custom_field_types ||= {} @custom_field_types ||= {}
@custom_field_types[name] = type @custom_field_types[name] = type
end end
def self.get_custom_field_type(name) def get_custom_field_type(name)
@custom_field_types ||= {} @custom_field_types ||= {}
@custom_field_types[name] @custom_field_types[name]
end end
def self.preload_custom_fields(objects, fields) def preload_custom_fields(objects, fields)
if objects.present? if objects.present?
map = {} map = {}
@ -136,28 +123,23 @@ module HasCustomFields
end end
end end
end end
end
private included do
extend ClassMethods
def custom_fields_max_items has_many :_custom_fields, dependent: :destroy, class_name: "#{name}CustomField"
if custom_fields.size > CUSTOM_FIELDS_MAX_ITEMS
errors.add(
:base,
I18n.t("custom_fields.validations.max_items", max_items_number: CUSTOM_FIELDS_MAX_ITEMS),
)
end
end
def custom_fields_value_length validate :custom_fields_max_items, unless: :custom_fields_clean?
return if custom_fields.values.all? { _1.to_s.size <= CUSTOM_FIELDS_MAX_VALUE_LENGTH } validate :custom_fields_value_length, unless: :custom_fields_clean?
errors.add(
:base, after_save :save_custom_fields
I18n.t( end
"custom_fields.validations.max_value_length",
max_value_length: CUSTOM_FIELDS_MAX_VALUE_LENGTH, attr_reader :preloaded_custom_fields
),
) def custom_fields_fk
end @custom_fields_fk ||= "#{_custom_fields.reflect_on_all_associations(:belongs_to)[0].name}_id"
end end
def reload(options = nil) def reload(options = nil)
@ -325,4 +307,26 @@ module HasCustomFields
@custom_fields_orig = target @custom_fields_orig = target
@custom_fields = @custom_fields_orig.deep_dup @custom_fields = @custom_fields_orig.deep_dup
end end
private
def custom_fields_max_items
if custom_fields.size > CUSTOM_FIELDS_MAX_ITEMS
errors.add(
:base,
I18n.t("custom_fields.validations.max_items", max_items_number: CUSTOM_FIELDS_MAX_ITEMS),
)
end
end
def custom_fields_value_length
return if custom_fields.values.all? { _1.to_s.size <= CUSTOM_FIELDS_MAX_VALUE_LENGTH }
errors.add(
:base,
I18n.t(
"custom_fields.validations.max_value_length",
max_value_length: CUSTOM_FIELDS_MAX_VALUE_LENGTH,
),
)
end
end end