Class: Friendly::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly/associations/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_klass, name, options = {}) ⇒ Association

Returns a new instance of Association.



6
7
8
9
10
11
# File 'lib/friendly/associations/association.rb', line 6

def initialize(owner_klass, name, options = {})
  @owner_klass = owner_klass
  @name        = name
  @class_name  = options[:class_name]
  @foreign_key = options[:foreign_key]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/friendly/associations/association.rb', line 4

def name
  @name
end

#owner_klassObject (readonly)

Returns the value of attribute owner_klass.



4
5
6
# File 'lib/friendly/associations/association.rb', line 4

def owner_klass
  @owner_klass
end

Instance Method Details

#class_nameObject



21
22
23
# File 'lib/friendly/associations/association.rb', line 21

def class_name
  @class_name ||= name.to_s.classify
end

#foreign_keyObject



17
18
19
# File 'lib/friendly/associations/association.rb', line 17

def foreign_key
  @foreign_key ||= [owner_klass_name, :id].join("_").to_sym
end

#klassObject



13
14
15
# File 'lib/friendly/associations/association.rb', line 13

def klass
  @klass ||= class_name.constantize
end

#owner_klass_nameObject



25
26
27
# File 'lib/friendly/associations/association.rb', line 25

def owner_klass_name
  owner_klass.name.to_s.underscore.singularize
end

#scope(document) ⇒ Object



29
30
31
# File 'lib/friendly/associations/association.rb', line 29

def scope(document)
  klass.scope(foreign_key => document.id)
end