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
- #primary_key ⇒ Object
- #query_model(binding, id) ⇒ Object
- #single_association? ⇒ Boolean
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
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_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
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 |
#extract_class_and_column ⇒ Object
75 76 77 78 |
# File 'lib/active_model_cachers/active_record/attr_model.rb', line 75 def extract_class_and_column return [class_name, nil] if single_association? return [@klass.to_s, @column] 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_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 |
#primary_key ⇒ Object
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 |