Class: ActiveRecord::AssociatedObject
- Inherits:
-
Object
- Object
- ActiveRecord::AssociatedObject
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, Caching
- Defined in:
- lib/active_record/associated_object/version.rb,
lib/active_record/associated_object.rb
Defined Under Namespace
Modules: Caching, ObjectAssociation Classes: Railtie
Constant Summary collapse
- VERSION =
"0.9.3"
Class Attribute Summary collapse
-
.attribute_name ⇒ Object
readonly
Returns the value of attribute attribute_name.
-
.record ⇒ Object
readonly
Returns the value of attribute record.
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Class Method Summary collapse
- .associated_via(record) ⇒ Object
- .extension(&block) ⇒ Object
- .inherited(new_object) ⇒ Object
- .method_missing(meth) ⇒ Object
- .respond_to_missing?(meth) ⇒ Boolean
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(record) ⇒ AssociatedObject
constructor
A new instance of AssociatedObject.
Methods included from Caching
#cache_key, #cache_key_with_version
Constructor Details
#initialize(record) ⇒ AssociatedObject
Returns a new instance of AssociatedObject.
63 64 65 |
# File 'lib/active_record/associated_object.rb', line 63 def initialize(record) @record = record end |
Class Attribute Details
.attribute_name ⇒ Object (readonly)
Returns the value of attribute attribute_name.
21 22 23 |
# File 'lib/active_record/associated_object.rb', line 21 def attribute_name @attribute_name end |
.record ⇒ Object (readonly)
Returns the value of attribute record.
21 22 23 |
# File 'lib/active_record/associated_object.rb', line 21 def record @record end |
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
58 59 60 |
# File 'lib/active_record/associated_object.rb', line 58 def record @record end |
Class Method Details
.associated_via(record) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/active_record/associated_object.rb', line 12 def associated_via(record) unless record.respond_to?(:descends_from_active_record?) && record.descends_from_active_record? raise ArgumentError, "#{record} isn't a valid namespace; can only associate with ActiveRecord::Base subclasses" end @record, @attribute_name = record, model_name.element.to_sym alias_method record.model_name.element, :record end |
.extension(&block) ⇒ Object
24 25 26 |
# File 'lib/active_record/associated_object.rb', line 24 def extension(&block) record.class_eval(&block) end |
.inherited(new_object) ⇒ Object
8 9 10 |
# File 'lib/active_record/associated_object.rb', line 8 def inherited(new_object) new_object.associated_via(new_object.module_parent) end |
.method_missing(meth) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/active_record/associated_object.rb', line 28 def method_missing(meth, ...) if !record.respond_to?(meth) || meth.end_with?("?", "=") then super else record.public_send(meth, ...).then do |value| value.respond_to?(:each) ? value.map(&attribute_name) : value&.public_send(attribute_name) end end end |
.respond_to_missing?(meth) ⇒ Boolean
36 37 38 |
# File 'lib/active_record/associated_object.rb', line 36 def respond_to_missing?(meth, ...) (record.respond_to?(meth, ...) && !meth.end_with?("?", "=")) || super end |
Instance Method Details
#==(other) ⇒ Object
67 68 69 |
# File 'lib/active_record/associated_object.rb', line 67 def ==(other) other.is_a?(self.class) && id == other.id end |