Class: ActiveMocker::Mock::HasMany

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

Direct Known Subclasses

HasAndBelongsToMany

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Queries

#all, #average, #count, #delete_all, #find, #find_by, #find_by!, #find_or_create_by, #find_or_initialize_by, #limit, #maximum, #minimum, #order, #reverse_order, #sum, #update, #update_all, #where

Methods inherited from Relation

#from_limit?, #inspect

Methods inherited from Collection

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

Constructor Details

#initialize(collection, options = {}) ⇒ HasMany

Returns a new instance of HasMany.



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

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

Instance Attribute Details

#foreign_idObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/active_mocker/mock/has_many.rb', line 30

def foreign_id
  @foreign_id
end

#foreign_keyObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/active_mocker/mock/has_many.rb', line 30

def foreign_key
  @foreign_key
end

#relation_classObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/active_mocker/mock/has_many.rb', line 30

def relation_class
  @relation_class
end

#sourceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/active_mocker/mock/has_many.rb', line 30

def source
  @source
end

Class Method Details

.new(collection, options = {}) ⇒ Object



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

def self.new(collection, options = {})
  return Relation.new(collection) if options[:relation_class].nil?
  super(collection, options)
end

Instance Method Details

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_mocker/mock/has_many.rb', line 32

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!



49
50
51
52
53
# File 'lib/active_mocker/mock/has_many.rb', line 49

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

#init_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/active_mocker/mock/has_many.rb', line 58

def init_options
  {foreign_key => foreign_id}
end

#set_foreign_keyObject



23
24
25
26
27
# File 'lib/active_mocker/mock/has_many.rb', line 23

def set_foreign_key
  collection.each do |item|
    item.send(:write_attribute, foreign_key, foreign_id) if item.respond_to?("#{foreign_key}=")
  end
end