Module: ActiveRecord::CustomAttributes::ClassMethods

Defined in:
lib/active_record/custom_attributes.rb

Instance Method Summary collapse

Instance Method Details

#has_custom_attributes(extra_field_types = {}) {|field_definitions| ... } ⇒ Object

Yields:

  • (field_definitions)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_record/custom_attributes.rb', line 17

def has_custom_attributes(extra_field_types = {}, &block)
  field_types = extra_field_types
  field_types = self.defined_custom_field_types.merge(extra_field_types) if has_custom_attributes?

  field_definitions = CustomAttributeDefinitionHelper.new field_types
  yield field_definitions if block_given?
  defined_custom_attributes = field_definitions.defined_attributes
  defined_custom_validations = field_definitions.defined_validations

  if has_custom_attributes?
    write_inheritable_attribute(:defined_custom_attributes, self.defined_custom_attributes.deep_merge(defined_custom_attributes))
    write_inheritable_attribute(:defined_custom_validations, self.defined_custom_validations.deep_merge(defined_custom_validations))
    write_inheritable_attribute(:defined_custom_field_types, field_types)
  else
    write_inheritable_attribute(:defined_custom_attributes, defined_custom_attributes)
    class_inheritable_reader(:defined_custom_attributes)

    write_inheritable_attribute(:defined_custom_validations, defined_custom_validations)
    class_inheritable_reader(:defined_custom_validations)

    write_inheritable_attribute(:defined_custom_field_types, extra_field_types)
    class_inheritable_reader(:defined_custom_field_types)

    class_eval do
      def self.has_custom_attributes?
        true
      end

      include ActiveRecord::CustomAttributes::Core
    end
  end
end

#has_custom_attributes?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/active_record/custom_attributes.rb', line 13

def has_custom_attributes?
  false
end