Class: ActiveMocker::Mock::HasMany
- Inherits:
-
Association
- Object
- Collection
- Relation
- Association
- ActiveMocker::Mock::HasMany
- Includes:
- Queries
- Defined in:
- lib/active_mocker/mock/has_many.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #build(options = {}, &block) ⇒ Object
- #create(options = {}, &block) ⇒ Object (also: #create!)
-
#initialize(collection, foreign_key, foreign_id, relation_class) ⇒ HasMany
constructor
A new instance of HasMany.
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
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(={}, &block) new_record = relation_class.new(.merge!(), &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(={}, &block) created_record = relation_class.create(.merge!(), &block) collection << created_record created_record end |