Class: Friendly::Associations::Association

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

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Association) initialize(owner_klass, name, options = {})

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

- (Object) name (readonly)

Returns the value of attribute name



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

def name
  @name
end

- (Object) owner_klass (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

- (Object) class_name



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

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

- (Object) foreign_key



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

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

- (Object) klass



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

def klass
  @klass ||= class_name.constantize
end

- (Object) owner_klass_name



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

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

- (Object) scope(document)



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

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