Module: DynamicAttributes::ClassMethods

Defined in:
app/models/concerns/dynamic_attributes.rb

Instance Method Summary collapse

Instance Method Details

#dynamic_attributes(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/concerns/dynamic_attributes.rb', line 12

def dynamic_attributes(*args)
  @_dynamic_attributes ||= []
  @_dynamic_attributes |= args.map(&:to_s)

  args.each do |attr|
    class_eval "      def \#{attr}\n        read_dynamic_attribute(\#{attr.inspect})\n      end\n\n      def \#{attr}=(v)\n        write_dynamic_attribute(\#{attr.inspect}, v)\n      end\n    ENDOFCODE\n  end\nend\n"

#partition_wheres(wheres) ⇒ Object

we only define this for classes with dynamic attributes so that unmodified classes work exactly as before



8
9
10
# File 'app/models/concerns/dynamic_attributes.rb', line 8

def partition_wheres(wheres)
  wheres.partition { |k, v| column_names.include?(k.to_s) || reflect_on_association(k.to_sym) }.map { |x| Hash[x] }
end

#permitted_dynamic_attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'app/models/concerns/dynamic_attributes.rb', line 29

def permitted_dynamic_attribute?(attr)
  # make sure the regular attributes have been defined
  define_attribute_methods
  @_dynamic_attributes.include?(attr.to_s) || instance_methods.none? { |m| m.to_s == attr.to_s }
end