Class: ActiveModelCachers::ActiveRecord::AttrModel
- Inherits:
-
Object
- Object
- ActiveModelCachers::ActiveRecord::AttrModel
- Defined in:
- lib/active_model_cachers/active_record/attr_model.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#reflect ⇒ Object
readonly
Returns the value of attribute reflect.
Instance Method Summary collapse
- #association? ⇒ Boolean
- #belongs_to? ⇒ Boolean
- #class_name ⇒ Object
- #collection? ⇒ Boolean
- #extract_class_and_column ⇒ Object
- #foreign_key(reverse: false) ⇒ Object
- #has_one? ⇒ Boolean
-
#initialize(klass, column, primary_key: nil, foreign_key: nil) ⇒ AttrModel
constructor
A new instance of AttrModel.
- #join_table ⇒ Object
- #join_table_class_name ⇒ Object
- #primary_key ⇒ Object
- #query_model(binding, id) ⇒ Object
- #single_association? ⇒ Boolean
- #through_klass ⇒ Object
- #through_reflection ⇒ Object
Constructor Details
#initialize(klass, column, primary_key: nil, foreign_key: nil) ⇒ AttrModel
Returns a new instance of 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
#column ⇒ Object (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 |
#klass ⇒ Object (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 |
#reflect ⇒ Object (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
44 45 46 47 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 44 def belongs_to? return false if not association? return @reflect.belongs_to? end |
#class_name ⇒ Object
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
76 77 78 79 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 76 def collection? return false if not association? return @reflect.collection? end |
#extract_class_and_column ⇒ Object
87 88 89 90 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 87 def extract_class_and_column return [class_name, nil] if single_association? return [@klass.to_s, @column] end |
#foreign_key(reverse: false) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 63 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
49 50 51 52 53 54 55 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 49 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_table ⇒ Object
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 = @reflect. return [:through] if [:through] return ([:join_table] || @reflect.send(:derive_join_table)) if @reflect.macro == :has_and_belongs_to_many return nil end |
#join_table_class_name ⇒ Object
32 33 34 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 32 def join_table_class_name join_table.try{|table_name| @klass.reflect_on_association(table_name).try(:class_name) || through_klass.name } end |
#primary_key ⇒ Object
57 58 59 60 61 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 57 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
81 82 83 84 85 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 81 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
71 72 73 74 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 71 def single_association? return false if not association? return !collection? end |
#through_klass ⇒ Object
40 41 42 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 40 def through_klass through_reflection.try(:klass) || through_klass_for_rails_3 end |
#through_reflection ⇒ Object
36 37 38 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 36 def through_reflection @klass.new.association(@column).reflection.through_reflection end |