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
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Object
We overwrite the initializer in order to pass the
as_of_timeparameter 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_timeof the original invocation.
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/chrono_model/patches/preloader.rb', line 9 def 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
14 15 16 |
# File 'lib/chrono_model/patches/preloader.rb', line 14 def initialize( = {}) = .freeze 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
recordsare the parent records where the association is defined -
The
associationsare the association names involved in preloading -
The
given_preload_scopeis 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.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/chrono_model/patches/preloader.rb', line 39 def preload(records, associations, given_preload_scope = nil) if [:as_of_time] preload_scope = given_preload_scope || ChronoModel::Patches::AsOfTimeRelation.new([:model]) preload_scope.as_of_time!([:as_of_time]) end super records, associations, preload_scope end |