Module: ObjectAttorney::NestedObjects::ClassMethods
- Defined in:
- lib/object_attorney/nested_objects.rb
Instance Method Summary collapse
- #accepts_nested_objects(nested_object_name, options = {}) ⇒ Object
- #association_reflections ⇒ Object
- #belongs_to(nested_object_name, options = {}) ⇒ Object
- #has_many(nested_object_name, options = {}) ⇒ Object
- #has_one(nested_object_name, options = {}) ⇒ Object
- #reflect_on_all_associations(macro = nil) ⇒ Object
- #reflect_on_association(association) ⇒ Object
Instance Method Details
#accepts_nested_objects(nested_object_name, options = {}) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/object_attorney/nested_objects.rb', line 252 def accepts_nested_objects(nested_object_name, = {}) .symbolize_keys! reflection = AssociationReflection.new(nested_object_name, represented_object_reflection, ) self.instance_variable_set("@#{nested_object_name}_reflection", reflection) self.instance_variable_set("@association_reflections", association_reflections | [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_reflections ⇒ Object
266 267 268 |
# File 'lib/object_attorney/nested_objects.rb', line 266 def association_reflections self.instance_variable_get("@association_reflections") || zuper_method('association_reflections') || [] end |
#belongs_to(nested_object_name, options = {}) ⇒ Object
248 249 250 |
# File 'lib/object_attorney/nested_objects.rb', line 248 def belongs_to(nested_object_name, = {}) accepts_nested_objects_overwrite_macro(nested_object_name, , :belongs_to) end |
#has_many(nested_object_name, options = {}) ⇒ Object
240 241 242 |
# File 'lib/object_attorney/nested_objects.rb', line 240 def has_many(nested_object_name, = {}) accepts_nested_objects_overwrite_macro(nested_object_name, , :has_many) end |
#has_one(nested_object_name, options = {}) ⇒ Object
244 245 246 |
# File 'lib/object_attorney/nested_objects.rb', line 244 def has_one(nested_object_name, = {}) accepts_nested_objects_overwrite_macro(nested_object_name, , :has_one) end |
#reflect_on_all_associations(macro = nil) ⇒ Object
274 275 276 |
# File 'lib/object_attorney/nested_objects.rb', line 274 def reflect_on_all_associations(macro = nil) macro ? association_reflections.select { |reflection| reflection.macro == macro } : association_reflections end |
#reflect_on_association(association) ⇒ Object
270 271 272 |
# File 'lib/object_attorney/nested_objects.rb', line 270 def reflect_on_association(association) self.instance_variable_get("@#{association}_reflection") || zuper_method('reflect_on_association', association) end |