Class: ActiveMocker::Mock::HasMany

Inherits:
Association show all
Includes:
Queries
Defined in:
lib/active_mocker/mock/has_many.rb

Direct Known Subclasses

HasAndBelongsToMany

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Queries

#all, #average, #delete_all, #destroy_all, #find, #find_by, #find_by!, #limit, #maximum, #minimum, #order, #reverse_order, #sum, #update_all, #where

Methods inherited from Collection

#<<, #==, #each, #hash, #map, #select, #to_a, #to_ary

Constructor Details

#initialize(collection, foreign_key, foreign_id, relation_class) ⇒ HasMany

Returns a new instance of HasMany.



13
14
15
16
17
18
19
# File 'lib/active_mocker/mock/has_many.rb', line 13

def initialize(collection, foreign_key, foreign_id, relation_class)
  @relation_class = relation_class
  @foreign_key    = foreign_key
  @foreign_id     = foreign_id
  self.class.include "#{relation_class.name}::Scopes".constantize
  super(collection)
end

Class Method Details

.new(collection, foreign_key = nil, foreign_id = nil, relation_class = nil) ⇒ Object



8
9
10
11
# File 'lib/active_mocker/mock/has_many.rb', line 8

def self.new(collection, foreign_key=nil, foreign_id=nil, relation_class=nil)
  return Relation.new(collection) if relation_class.nil?
  super
end

Instance Method Details

#build(options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_mocker/mock/has_many.rb', line 25

def build(options={}, &block)
  new_record = relation_class.new(init_options.merge!(options), &block)

  def new_record.belongs_to(collection)
    @belongs_to_collection = collection
  end

  new_record.belongs_to(self)

  def new_record.save
    @belongs_to_collection << self
    super
  end

  new_record
end

#create(options = {}, &block) ⇒ Object Also known as: create!



42
43
44
45
46
# File 'lib/active_mocker/mock/has_many.rb', line 42

def create(options={}, &block)
  created_record = relation_class.create(init_options.merge!(options), &block)
  collection << created_record
  created_record
end