Class: Associationist::Associations::Preloader::SingularAssociation
- Inherits:
-
Object
- Object
- Associationist::Associations::Preloader::SingularAssociation
- Defined in:
- lib/associationist/associations/preloader/singular_association.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #future_classes ⇒ Object
-
#initialize(klass, owners, reflection, preload_scope, reflection_scope = nil, associate_by_default = true) ⇒ SingularAssociation
constructor
A new instance of SingularAssociation.
- #preloaded_records ⇒ Object
-
#run(preloader = nil) ⇒ Object
handle >= 6.0.
- #run? ⇒ Boolean
-
#runnable_loaders ⇒ Object
handle >=7.0.
- #table_name ⇒ Object
Constructor Details
#initialize(klass, owners, reflection, preload_scope, reflection_scope = nil, associate_by_default = true) ⇒ SingularAssociation
Returns a new instance of SingularAssociation.
6 7 8 9 10 11 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 6 def initialize klass, owners, reflection, preload_scope, reflection_scope = nil, associate_by_default = true @klass = klass @owners = owners @reflection = reflection @run = false end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 5 def klass @klass end |
Instance Method Details
#future_classes ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 54 def future_classes if run? [] else if @klass <= ActiveRecord::Base [@klass] else [] end end end |
#preloaded_records ⇒ Object
41 42 43 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 41 def preloaded_records @owners.flat_map { |owner| owner.association(@reflection.name).target }.compact end |
#run(preloader = nil) ⇒ Object
handle >= 6.0
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 14 def run preloader = nil return self if @run @run = true case when @reflection.config.preloader_proc @reflection.config.preloader_proc.call(@owners).each do |record, value| record.association(@reflection.name).target = value end when @reflection.config.loader_proc @owners.each do |record| record.association(@reflection.name).target = @reflection.config.loader_proc.call(record) end when @reflection.config.scope_proc case @reflection.config.type when :singular @owners.each do |record| record.association(@reflection.name).target = @reflection.config.scope_proc.call(record).first end when :collection @owners.each do |record| record.association(@reflection.name).target = @reflection.config.scope_proc.call(record) end end end self end |
#run? ⇒ Boolean
50 51 52 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 50 def run? @run end |
#runnable_loaders ⇒ Object
handle >=7.0
46 47 48 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 46 def runnable_loaders [self] end |
#table_name ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/associationist/associations/preloader/singular_association.rb', line 66 def table_name if @klass <= ActiveRecord::Base @klass.table_name else nil end end |