Module: ProfileFieldMixins::HasChildProfileFields

Included in:
ProfileField, ProfileField
Defined in:
app/models/profile_field_mixins/has_child_profile_fields.rb

Defined Under Namespace

Modules: HasChildProfileFieldsInstanceMethods

Instance Method Summary collapse

Instance Method Details

#has_child_profile_fields(*keys) ⇒ Object

This creates an easier way to access a composed ProfileField’s child field values. Instead of calling

.children.where( :label => :account_number ).first.value
.children.where( :label => :account_number ).first.value = "12345"

you may call

.
. = "12345"

after telling the profile_field to create these accessors:

class BankAccount < ProfileField
  ...
  has_child_profile_fields :account_holder, :account_number, ...
  ...
end

Furthermore, this method modifies the intializer to build the child fields on build of the main profile_field.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 26

def has_child_profile_fields( *keys )

  before_save :build_child_fields_if_absent
  after_save :save_child_profile_fields
  
  attr_accessible *keys if defined? attr_accessible

  include HasChildProfileFieldsInstanceMethods

  self.class_eval <<-EOL

    def keys
      #{keys}
    end

  EOL


  keys.each do |accessor|

    self.class_eval <<-EOL

        def #{accessor}
          self.get_field( :#{accessor} )
        end

        def #{accessor}=( new_value )
          self.set_field( :#{accessor}, new_value )
        end

    EOL

  end
end