Method: ProfileFieldMixins::HasChildProfileFields#has_child_profile_fields

Defined in:
app/models/profile_field_mixins/has_child_profile_fields.rb

#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 "\n    def keys\n      \#{keys}\n    end\n\n  EOL\n\n\n  keys.each do |accessor|\n\n    self.class_eval <<-EOL\n\n        def \#{accessor}\n          self.get_field( :\#{accessor} )\n        end\n\n        def \#{accessor}=( new_value )\n          self.set_field( :\#{accessor}, new_value )\n        end\n\n    EOL\n\n  end\nend\n"