# _associations.erb <%= '# belongs_to' unless belongs_to.empty? -%> <% belongs_to.each do |meth| -%> def <%= meth.name %> <% association = relation_find(:name, meth.name).first -%> read_association(:<%= meth.name %>) || write_association(:<%= meth.name %>, classes('<%= association.class_name %>').try{ |k| k.find_by(id: <%= association.foreign_key %>)}) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, val) ActiveMocker::BelongsTo.new(val, child_self: self, foreign_key: :<%= meth.foreign_key %>).item end def build_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> self.<%= meth.name %> = classes('<%= association.class_name %>', true).new(attributes, &block) <% end -%> end def create_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> self.<%= meth.name %> = classes('<%= association.class_name %>', true).create(attributes, &block) <% end -%> end alias_method :create_<%= meth.name %>!, :create_<%= meth.name %> <% end -%> <%= '# has_one' unless has_one.empty? -%> <% has_one.each do |meth| -%> def <%= meth.name %> read_association(:<%= meth.name %>) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, val) ActiveMocker::HasOne.new(val, child_self: self, foreign_key: '<%= meth.foreign_key %>').item end def build_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> self.<%= meth.name %>= classes('<%= association.class_name %>', true).new(attributes, &block) <% end -%> end def create_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> self.<%= meth.name %>= classes('<%= association.class_name %>', true).new(attributes, &block) <% end -%> end alias_method :create_<%= meth.name %>!, :create_<%= meth.name %> <% end -%> <%= '# has_many' unless has_many.empty? -%> <% has_many.each do |meth| -%> def <%= meth.name %> read_association(:<%= meth.name %>, -> { ActiveMocker::HasMany.new([],foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>') }) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, ActiveMocker::HasMany.new(val, foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>')) end <% end -%> <%= '# has_and_belongs_to_many' unless has_and_belongs_to_many.empty? -%> <% has_and_belongs_to_many.each do |meth| -%> def <%= meth.name %> read_association(:<%= meth.name %>, ->{ ActiveMocker::HasAndBelongsToMany.new([]) }) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, ActiveMocker::HasAndBelongsToMany.new(val)) end <% end -%>