Module: ProfileFieldMixins::HasChildProfileFields::HasChildProfileFieldsInstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#build_child(key) ⇒ Object



108
109
110
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 108

def build_child( key )
  children.build( label: key )
end

#build_child_fields(keys) ⇒ Object



80
81
82
83
84
85
86
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 80

def build_child_fields( keys )
  if self.parent == nil # do it only for the parent, not the children as well
    keys.each do |key|
      build_child(key) unless find_child_by_key(key)
    end
  end
end

#build_child_fields_if_absentObject



74
75
76
77
78
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 74

def build_child_fields_if_absent
  if self.children_count == 0
    build_child_fields self.keys
  end
end

#find_child_by_key(key) ⇒ Object



63
64
65
66
67
68
69
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 63

def find_child_by_key(key)
  # we have to use 'select' here instead of 'where', because this needs
  # to work before the records are saved. But this should not be a problem,
  # since there are only a couple of child fields.
  #
  self.children.select { |child| child.key.to_s == key.to_s }.first
end

#find_or_build_child_by_key(key) ⇒ Object



70
71
72
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 70

def find_or_build_child_by_key(key)
  find_child_by_key(key) || build_child(key)
end

#get_field(accessor) ⇒ Object



99
100
101
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 99

def get_field( accessor )
  find_child_by_key(accessor).value if find_child_by_key(accessor)
end

#set_field(accessor, value) ⇒ Object



103
104
105
106
# File 'app/models/profile_field_mixins/has_child_profile_fields.rb', line 103

def set_field( accessor, value )
  build_child_fields_if_absent
  find_child_by_key(accessor).value = value
end