Class: ActiveModelCachers::ActiveRecord::AttrModel

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_cachers/active_record/attr_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, column, primary_key: nil, foreign_key: nil) ⇒ AttrModel


7
8
9
10
11
12
13
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 7

def initialize(klass, column, primary_key: nil, foreign_key: nil)
  @klass = klass
  @column = column
  @primary_key = primary_key
  @foreign_key = foreign_key
  @reflect = klass.reflect_on_association(column)
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column


5
6
7
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 5

def column
  @column
end

#klassObject (readonly)

Returns the value of attribute klass


5
6
7
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 5

def klass
  @klass
end

#reflectObject (readonly)

Returns the value of attribute reflect


5
6
7
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 5

def reflect
  @reflect
end

Instance Method Details

#association?Boolean


15
16
17
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 15

def association?
  return (@reflect != nil)
end

#belongs_to?Boolean


32
33
34
35
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 32

def belongs_to?
  return false if not association?
  return @reflect.belongs_to?
end

#class_nameObject


19
20
21
22
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 19

def class_name
  return if not association?
  return @reflect.class_name
end

#collection?Boolean


64
65
66
67
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 64

def collection?
  return false if not association?
  return @reflect.collection?
end

#foreign_key(reverse: false) ⇒ Object


51
52
53
54
55
56
57
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 51

def foreign_key(reverse: false)
  return @foreign_key if @foreign_key
  return if not association?
  # key may be symbol if specify foreign_key in association options
  return @reflect.chain.last.foreign_key.to_s if reverse and join_table
  return (@reflect.belongs_to? == reverse ? primary_key : @reflect.foreign_key).to_s
end

#has_one?Boolean


37
38
39
40
41
42
43
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 37

def has_one?
  return false if not association?
  #return @reflect.has_one? # Rails 3 doesn't have this method
  return false if @reflect.collection?
  return false if @reflect.belongs_to?
  return true
end

#join_tableObject


24
25
26
27
28
29
30
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 24

def join_table
  return nil if @reflect == nil
  options = @reflect.options
  return options[:through] if options[:through]
  return (options[:join_table] || @reflect.send(:derive_join_table)) if @reflect.macro == :has_and_belongs_to_many
  return nil
end

#primary_keyObject


45
46
47
48
49
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 45

def primary_key
  return @primary_key if @primary_key
  return if not association?
  return (@reflect.belongs_to? ? @reflect.klass : @reflect.active_record).primary_key
end

#query_model(binding, id) ⇒ Object


69
70
71
72
73
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 69

def query_model(binding, id)
  return query_self(binding, id) if @column == nil
  return query_association(binding, id) if association?
  return query_attribute(binding, id)
end

#single_association?Boolean


59
60
61
62
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 59

def single_association?
  return false if not association?
  return !collection?
end