Method: HasDynamicFields::Base::InstanceMethods#method_missing

Defined in:
lib/has_dynamic_fields/has_dynamic_fields.rb

#method_missing(name, *args) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/has_dynamic_fields/has_dynamic_fields.rb', line 251

def method_missing(name, *args)

  return if self.send(self.aad_options[:fieldgroup_singular].to_sym).blank?
  m = name.to_s
  type = :reader
  attribute_name = name.to_s.sub(/=$/) do
    type = :writer
    ""
  end

  fieldgroup_fields = self.send(self.aad_options[:fieldgroup_singular].to_sym).send(self.aad_options[:field_plural].to_sym)
  @dynamic_field_keys ||= instance_variable_set("@dynamic_field_keys", fieldgroup_fields.collect {|o| o.name.to_sym })
  @dynamic_fields ||= instance_variable_set("@dynamic_fields", fieldgroup_fields)

  if @dynamic_field_keys.include?(attribute_name.to_sym)

    field = @dynamic_fields.select { |field| field.name.to_sym == attribute_name.to_sym && field.send(self.aad_options[:fieldgroup_singular]).id == self.send(self.aad_options[:fieldgroup_singular]).id }.first

    case(type)

    when :writer
      self.update_dynamic_attributes=(true)
      self.class.send(:define_method, name) do |value|
        self.send(self.aad_options[:value_singular].to_sym).send("field_#{field.id}=".to_sym, value)
      end
    else
      self.class.send(:define_method, name) do
        self.send(self.aad_options[:value_singular]).send("field_#{field.id}".to_sym, *args)
      end
    end
    #former only set the methods now we actually have to execute them
    send(name, *args)

  else
    # commenting out super because its throwing undefined method field_ changed in carrierwave? i think this is bad
    # super
  end  

end