Class: Miscellany::ArbitraryPrefetch::PrefetcherContext
- Inherits:
-
Object
- Object
- Miscellany::ArbitraryPrefetch::PrefetcherContext
- Defined in:
- lib/miscellany/active_record/arbitrary_prefetch.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#target_attribute ⇒ Object
Returns the value of attribute target_attribute.
Instance Method Summary collapse
-
#initialize(model, opts) ⇒ PrefetcherContext
constructor
A new instance of PrefetcherContext.
- #link_models(models) ⇒ Object
- #reflection ⇒ Object
Constructor Details
#initialize(model, opts) ⇒ PrefetcherContext
Returns a new instance of PrefetcherContext.
16 17 18 19 20 21 22 23 |
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 16 def initialize(model, opts) = opts @model = model @source_key = opts[:relation] @target_attribute = opts[:attribute] @queryset = opts[:queryset] @models = [] end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
13 14 15 |
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 13 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 14 def end |
#target_attribute ⇒ Object
Returns the value of attribute target_attribute.
13 14 15 |
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 13 def target_attribute @target_attribute end |
Instance Method Details
#link_models(models) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 25 def link_models(models) Array(models).each do |m| @models << m # assoc = PrefetchAssociation.new(m, self, reflection) assoc = reflection.association_class.new(m, reflection) m.send(:association_instance_set, target_attribute, assoc) m.instance_eval " def \#{target_attribute}\n association(:\#{target_attribute}).reader\n end\n CODE\n end\nend\n", __FILE__, __LINE__ + 1 |
#reflection ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 41 def reflection @reflection ||= begin queryset = @queryset source_refl = model.reflections[@source_key.to_s] scope = lambda {|*_args| qs = queryset qs = qs.merge(source_refl.scope_for(model.unscoped)) if source_refl.scope qs } pass_opts = source_refl..merge( class_name: source_refl.class_name, inverse_of: nil, arbitrary_source_reflection: source_refl, ) if source_refl.is_a?(ActiveRecord::Reflection::ThroughReflection) pass_opts[:source] = source_refl.source_reflection_name end ActiveRecord::Reflection.create( [:type], @target_attribute, scope, pass_opts, model ) end end |