Module: ObjectAttorney::NestedObjects::ClassMethods

Defined in:
lib/object_attorney/nested_objects.rb

Instance Method Summary collapse

Instance Method Details

#accepts_nested_objects(nested_object_name, options = {}) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/object_attorney/nested_objects.rb', line 270

def accepts_nested_objects(nested_object_name, options = {})
  options.symbolize_keys!
  reflection = AssociationReflection.new(nested_object_name, represented_object_reflection, options)

  self.instance_variable_set("@#{nested_object_name}_reflection", reflection)
  self.instance_variable_set("@association_reflections", association_reflections.merge(nested_object_name => reflection))

  self.send(:attr_accessor, "#{nested_object_name}_attributes".to_sym)

  define_method(nested_object_name) { nested_getter(nested_object_name) }
  define_method("build_#{reflection.single_name}") { |attributes = {}, nested_object = nil| build_nested_object(nested_object_name, attributes, nested_object) }
  define_method("existing_#{nested_object_name}") { existing_nested_objects(nested_object_name) }
end

#association_reflectionsObject



284
285
286
# File 'lib/object_attorney/nested_objects.rb', line 284

def association_reflections
  self.instance_variable_get("@association_reflections") || zuper_method('association_reflections') || {}
end

#belongs_to(nested_object_name, options = {}) ⇒ Object



266
267
268
# File 'lib/object_attorney/nested_objects.rb', line 266

def belongs_to(nested_object_name, options = {})
  accepts_nested_objects_overwrite_macro(nested_object_name, options, :belongs_to)
end

#has_many(nested_object_name, options = {}) ⇒ Object



258
259
260
# File 'lib/object_attorney/nested_objects.rb', line 258

def has_many(nested_object_name, options = {})
  accepts_nested_objects_overwrite_macro(nested_object_name, options, :has_many)
end

#has_one(nested_object_name, options = {}) ⇒ Object



262
263
264
# File 'lib/object_attorney/nested_objects.rb', line 262

def has_one(nested_object_name, options = {})
  accepts_nested_objects_overwrite_macro(nested_object_name, options, :has_one)
end

#reflect_on_all_associations(macro = nil) ⇒ Object



292
293
294
# File 'lib/object_attorney/nested_objects.rb', line 292

def reflect_on_all_associations(macro = nil)
  macro ? association_reflections.values.select { |reflection| reflection.macro == macro } : association_reflections.values
end

#reflect_on_association(association) ⇒ Object



288
289
290
# File 'lib/object_attorney/nested_objects.rb', line 288

def reflect_on_association(association)
  self.instance_variable_get("@#{association}_reflection") || zuper_method('reflect_on_association', association)
end

#reflectionsObject



296
297
298
# File 'lib/object_attorney/nested_objects.rb', line 296

def reflections
  association_reflections
end