Class: Priloo::Preloaders::BmInjectorPreloader
- Inherits:
-
BasePreloader
- Object
- BasePreloader
- Priloo::Preloaders::BmInjectorPreloader
- Defined in:
- lib/priloo/preloaders/bm_injector_preloader.rb
Overview
This class implements preloading for Bannerman’s custom ActiveRecord injectors
Instance Attribute Summary collapse
-
#ar_class ⇒ Object
readonly
Returns the value of attribute ar_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes inherited from BasePreloader
Instance Method Summary collapse
- #extract(target) ⇒ Object
-
#initialize(ar_class, name) ⇒ BmInjectorPreloader
constructor
A new instance of BmInjectorPreloader.
- #inject(target, value) ⇒ Object
- #injected?(target) ⇒ Boolean
- #preload(ar_list) ⇒ Object
Methods inherited from BasePreloader
Constructor Details
#initialize(ar_class, name) ⇒ BmInjectorPreloader
Returns a new instance of BmInjectorPreloader.
9 10 11 12 13 14 |
# File 'lib/priloo/preloaders/bm_injector_preloader.rb', line 9 def initialize(ar_class, name) super([self.class, ar_class, name]) @name = name @ar_class = ar_class end |
Instance Attribute Details
#ar_class ⇒ Object (readonly)
Returns the value of attribute ar_class.
7 8 9 |
# File 'lib/priloo/preloaders/bm_injector_preloader.rb', line 7 def ar_class @ar_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/priloo/preloaders/bm_injector_preloader.rb', line 7 def name @name end |
Instance Method Details
#extract(target) ⇒ Object
36 37 38 |
# File 'lib/priloo/preloaders/bm_injector_preloader.rb', line 36 def extract(target) target.instance_variable_get(:@attributes).[](name).value end |
#inject(target, value) ⇒ Object
28 29 30 |
# File 'lib/priloo/preloaders/bm_injector_preloader.rb', line 28 def inject(target, value) target._preloadable_target.send(:write_attribute_without_type_cast, name, value) end |
#injected?(target) ⇒ Boolean
32 33 34 |
# File 'lib/priloo/preloaders/bm_injector_preloader.rb', line 32 def injected?(target) target.has_attribute?(name) end |
#preload(ar_list) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/priloo/preloaders/bm_injector_preloader.rb', line 16 def preload(ar_list) ids = ar_list.map { |ar| ar.send(primary_key) } injector_sql = ar_class.send("#{name}_sql") sql_query = Arel.sql <<~SQL #{quote_table_column(ar_class.table_name, primary_key)} AS id, (#{injector_sql}) AS val SQL fetched_data = ar_class.where(primary_key => ids.uniq).pluck(sql_query).to_h ar_list.map { |ar| fetched_data[ar.send(primary_key)] } end |