Class: ActiveMocker::HasMany
- Inherits:
-
Association
- Object
- Collection
- Relation
- Association
- ActiveMocker::HasMany
- Includes:
- Queries
- Defined in:
- lib/active_mocker/mock/has_many.rb
Direct Known Subclasses
Instance Attribute Summary collapse
- #foreign_id ⇒ Object readonly private
- #foreign_key ⇒ Object readonly private
- #relation_class ⇒ Object readonly private
- #source ⇒ Object readonly private
Class Method Summary collapse
Instance Method Summary collapse
- #build(options = {}, &block) ⇒ Object
- #create(options = {}, &block) ⇒ Object (also: #create!)
- #init_options ⇒ Object private
-
#initialize(collection, options = {}) ⇒ HasMany
constructor
A new instance of HasMany.
- #set_foreign_key ⇒ Object
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, #none, #order, #reverse_order, #sum, #update, #update_all, #where
Methods inherited from Relation
Methods inherited from Collection
#<<, #==, #blank?, #each, #hash, #to_a, #to_ary
Constructor Details
#initialize(collection, options = {}) ⇒ HasMany
Returns a new instance of HasMany.
10 11 12 13 14 15 16 17 18 |
# File 'lib/active_mocker/mock/has_many.rb', line 10 def initialize(collection, ={}) @relation_class = [:relation_class] @foreign_key = [:foreign_key] @foreign_id = [:foreign_id] @source = [:source] self.class.include "#{@relation_class.name}::Scopes".constantize super(collection) set_foreign_key end |
Instance Attribute Details
#foreign_id ⇒ Object (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.
27 28 29 |
# File 'lib/active_mocker/mock/has_many.rb', line 27 def foreign_id @foreign_id end |
#foreign_key ⇒ Object (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.
27 28 29 |
# File 'lib/active_mocker/mock/has_many.rb', line 27 def foreign_key @foreign_key end |
#relation_class ⇒ Object (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.
27 28 29 |
# File 'lib/active_mocker/mock/has_many.rb', line 27 def relation_class @relation_class end |
#source ⇒ Object (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.
27 28 29 |
# File 'lib/active_mocker/mock/has_many.rb', line 27 def source @source end |
Class Method Details
Instance Method Details
#build(options = {}, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_mocker/mock/has_many.rb', line 29 def build(={}, &block) new_record = relation_class.new(.merge!(), &block) # @private def new_record._belongs_to(collection) @belongs_to_collection = collection end new_record._belongs_to(self) # @private def new_record.save @belongs_to_collection << self super end new_record end |
#create(options = {}, &block) ⇒ Object Also known as: create!
48 49 50 51 52 |
# File 'lib/active_mocker/mock/has_many.rb', line 48 def create(={}, &block) created_record = relation_class.create(.merge!(), &block) collection << created_record created_record end |
#init_options ⇒ Object
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.
57 58 59 |
# File 'lib/active_mocker/mock/has_many.rb', line 57 def {foreign_key => foreign_id} end |
#set_foreign_key ⇒ Object
20 21 22 23 24 |
# File 'lib/active_mocker/mock/has_many.rb', line 20 def set_foreign_key collection.each do |item| item.send(:write_attribute, foreign_key, foreign_id) if item.respond_to?("#{foreign_key}=") end end |