Class: MongoModel::Associations::HasManyByForeignKey

Inherits:
Base::Definition show all
Defined in:
lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb

Defined Under Namespace

Classes: Association, Proxy

Instance Attribute Summary

Attributes inherited from Base::Definition

#name, #options, #owner

Instance Method Summary (collapse)

Methods inherited from Base::Definition

#collection?, #for, #initialize, #klass, methods, #polymorphic?, properties, #scope, #scope_options, #singular_name

Constructor Details

This class inherits a constructor from MongoModel::Associations::Base::Definition

Instance Method Details

- (Object) define!



12
13
14
15
16
17
18
19
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 12

def define!
  raise "has_many :by => :foreign_key is only valid on Document" unless owner.ancestors.include?(Document)
  
  super
  
  define_dependency_callbacks!
  self
end

- (Object) define_dependency_callbacks!



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 21

def define_dependency_callbacks!
  association = self
  
  if options[:dependent] == :destroy
    owner.before_destroy do
      send(association.name).each { |child| child.destroy }
    end
  elsif options[:dependent] == :delete
    owner.before_destroy do
      send(association.name).delete_all
    end
  end
end

- (Object) foreign_key



4
5
6
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 4

def foreign_key
  @foreign_key ||= options[:foreign_key] || :#{inverse_of}_id"
end

- (Object) inverse_of



8
9
10
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 8

def inverse_of
  @inverse_of ||= options[:inverse_of] || owner.to_s.demodulize.underscore.singularize.to_sym
end