Module: EmbedsMany::Base

Defined in:
lib/embeds_many/base.rb

Instance Method Summary collapse

Instance Method Details

#embeds_many(field, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/embeds_many/base.rb', line 3

def embeds_many(field, &block)
  child_klass =  Class.new(EmbedsMany::Child)
  child_klass.instance_variable_set "@field_name", field

  # rewrite association
  define_method field do
    instance_variable_get("@#{field}_collection") ||
      instance_variable_set("@#{field}_collection", ChildrenCollection.new(self, field, child_klass))
  end

  before_save { |parent| parent.send(field).before_parent_save }

  child_klass.class_eval(&block) if block
end