Class: Pragma::Decorator::Association::Adapter::ActiveRecord Private

Inherits:
Base
  • Object
show all
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

#bond

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • bond (Bond)

    the bond to use in the adapter

Raises:

  • (InconsistentTypeError)

    when the association’s real type is different from the one defined on the decorator ()e.g. decorator defines the association as belongs_to, but ActiveRecord reports its type is has_one)



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.

Parameters:

  • model (Object)

    the model to check

Returns:

  • (Boolean)

    whether the object is an instance of ActiveRecord::Base



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_objectObject

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.

TODO:

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.

Returns:

  • (Object)

    the associated object



83
84
85
# File 'lib/pragma/decorator/association/adapter/active_record.rb', line 83

def full_object
  associated_object
end

#primary_keyString|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.

TODO:

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.

Returns:

  • (String|Integer|NilClass)

    the PK of the associated object



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.options[: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