Module: DynamicFields

Defined in:
lib/dynamo/dynamic_fields.rb

Overview

TODO: We need to generalize the logic here such that we can pass in the necessary information to DynamicFields.dynamic_fields and have all of the other functionality behave properly.

The ‘fields` call should not be hard coded, etc

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

FIELD_NAME_PATTERN =
/(.+)_field(=)?$/

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dynamo/dynamic_fields.rb', line 50

def method_missing(sym, *args, &block)
  return super unless sym.match FIELD_NAME_PATTERN

  field_name = $1
  is_setter  = $2.present?
  field      = self.fields.find {|f| f..underscored_name == field_name} || CustomFieldValue.new

  if is_setter
    field.value = args.first
  else
    field.value
  end
end

Class Method Details

.included(base) ⇒ Object



18
19
20
# File 'lib/dynamo/dynamic_fields.rb', line 18

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#attributes=(attrs) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/dynamo/dynamic_fields.rb', line 64

def attributes=(attrs)
  field_attributes = attrs.delete('fields') {{}}

  fields.each do |field|
    field.set_value_from_attributes field_attributes
    fields_will_change! if field.changed?
  end

  super
end

#field_errorsObject



28
29
30
31
32
# File 'lib/dynamo/dynamic_fields.rb', line 28

def field_errors
  self.fields.each_with_object({}) do |f, h|
    h.merge! f.field_errors
  end
end

#fields_metadata=(fields_metadata) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/dynamo/dynamic_fields.rb', line 34

def fields_metadata=()
  self.fields = []

  .each do ||
    self.fields << CustomFieldValue.new(:metadata => )
  end
end

#respond_to_missing?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/dynamo/dynamic_fields.rb', line 42

def respond_to_missing?(sym, include_private = false)
  return super unless sym.match FIELD_NAME_PATTERN

  field_name = $1

  self.fields.find {|f| f..underscored_name == field_name}.present?
end

#validate_dynamic_fieldsObject



22
23
24
25
26
# File 'lib/dynamo/dynamic_fields.rb', line 22

def validate_dynamic_fields
  self.fields.each do |field|
    self.errors.add(:fields, field.errors) unless field.valid?
  end
end