Class: Pragma::Decorator::Association::Adapter::ActiveRecord Private
- Defined in:
- lib/pragma/decorator/association/adapter/active_record.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The ActiveRecord association adapter is used in AR environments and tries to minimize the number of SQL queries that are made to retrieve the associated object’s data.
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.supports?(model) ⇒ Boolean
private
Returns whether the adapter supports the given model.
Instance Method Summary collapse
-
#full_object ⇒ Object
private
Returns the expanded associated object.
-
#initialize(bond) ⇒ ActiveRecord
constructor
private
Initializes the adapter.
-
#primary_key ⇒ String|Integer|NilClass
private
Returns the primary key of the associated object.
Constructor Details
#initialize(bond) ⇒ ActiveRecord
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the adapter.
30 31 32 33 |
# File 'lib/pragma/decorator/association/adapter/active_record.rb', line 30 def initialize(bond) super check_type_consistency end |
Class Method Details
.supports?(model) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the adapter supports the given model.
18 19 20 |
# File 'lib/pragma/decorator/association/adapter/active_record.rb', line 18 def supports?(model) Object.const_defined?('ActiveRecord::Base') && model.is_a?(ActiveRecord::Base) end |
Instance Method Details
#full_object ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Ensure the required attributes are present on the associated object
Returns the expanded associated object.
This will simply return the associated object itself, delegating caching to AR.
83 84 85 |
# File 'lib/pragma/decorator/association/adapter/active_record.rb', line 83 def full_object associated_object end |
#primary_key ⇒ String|Integer|NilClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Allow to specify a different PK attribute when exec_context
is decorator
Returns the primary key of the associated object.
If the exec_context
of the association is decorator
, this will simply return early with the value returned by #id
on the associated object.
If the association is a belongs_to
, there are three possible scenarios:
* the association does not have a custom scope: this will compute the PK by calling
the foreign key on the parent model;
* the association has a custom scope and it has not been loaded: this will compute
the PK by +pluck+ing the PK column of the associated object;
* the association has a custom scope and it has been loaded: this will compute
the PK by retrieving the PK attribute from the loaded object.
If the association is a has_one
, there are two possible scenarios:
* the association has already been loaded: this will compute the PK by retrieving the
PK attribute from the loaded object;
* the association has not been loaded: this will compute the PK by +pluck+ing the PK
column of the associated object;
Custom scopes are always respected in both belongs_to
and has_one
.
nil
values are handled gracefully in all cases.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pragma/decorator/association/adapter/active_record.rb', line 63 def primary_key return associated_object&.id if reflection.[:exec_context] == :decorator case reflection.type when :belongs_to compute_belongs_to_fk when :has_one compute_has_one_fk else fail "Cannot compute primary key for #{reflection.type} association" end end |