Module: ActiveRecord::AssociatedObject::ObjectAssociation::ClassMethods

Defined in:
lib/active_record/associated_object/object_association.rb

Instance Method Summary collapse

Instance Method Details

#has_object(*names, **callbacks) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_record/associated_object/object_association.rb', line 15

def has_object(*names, **callbacks)
  extend_source_from(names) do |name|
    const_get object_name = name.to_s.camelize
    "def #{name}; (@associated_objects ||= {})[:#{name}] ||= #{object_name}.new(self); end"
  rescue NameError
    raise "The #{self}::#{object_name} associated object referenced from #{self} doesn't exist"
  end

  extend_source_from(names) do |name|
    callbacks.map do |callback, method|
      "#{callback} { #{name}.#{method == true ? callback : method} }"
    end
  end
end