Module: ChronoModel::Patches::Preloader
- Defined in:
- lib/chrono_model/patches/preloader.rb
Overview
Patches ActiveRecord::Associations::Preloader to add support for temporal associations. This is tying itself to Rails internals and it is ugly :-(.
Defined Under Namespace
Modules: Association, ThroughAssociation
Instance Attribute Summary collapse
-
#chronomodel_options ⇒ Object
readonly
Returns the value of attribute chronomodel_options.
Instance Method Summary collapse
-
#initialize(**options) ⇒ Object
We overwrite the initializer in order to pass the
as_of_time
parameter above in the build_preloader. -
#preload(records, associations, given_preload_scope = nil) ⇒ Object
Patches the AR Preloader (lib/active_record/associations/preloader.rb) in order to carry around the
as_of_time
of the original invocation.
Instance Attribute Details
#chronomodel_options ⇒ Object (readonly)
Returns the value of attribute chronomodel_options.
10 11 12 |
# File 'lib/chrono_model/patches/preloader.rb', line 10 def @chronomodel_options end |
Instance Method Details
#initialize(**options) ⇒ Object
We overwrite the initializer in order to pass the as_of_time
parameter above in the build_preloader
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/chrono_model/patches/preloader.rb', line 15 def initialize(**) @chronomodel_options = .extract!(:as_of_time, :model) [:scope] = chronomodel_scope([:scope]) if .key?(:scope) if .empty? super() else super(**) end end |
#preload(records, associations, given_preload_scope = nil) ⇒ Object
Patches the AR Preloader (lib/active_record/associations/preloader.rb) in order to carry around the as_of_time
of the original invocation.
-
The
records
are the parent records where the association is defined -
The
associations
are the association names involved in preloading -
The
given_preload_scope
is the preloading scope, that is used only in the :through association and it holds the intermediate records through which the final associated records are eventually fetched.
As the preload_scope
is passed around to all the different incarnations of the preloader strategies, we are using it to pass around the as_of_time
of the original query invocation, so that preloaded records are preloaded honoring the as_of_time
.
The preload_scope
is present only in through associations, but the preloader interfaces expect it to be always defined, for consistency.
For ‘:through` associations, the given_preload_scope
is already a Relation
, that already has the as_of_time
getters and setters, so we use it directly.
47 48 49 |
# File 'lib/chrono_model/patches/preloader.rb', line 47 def preload(records, associations, given_preload_scope = nil) super(records, associations, chronomodel_scope(given_preload_scope)) end |