Class: Associationist::Associations::Preloader::SingularAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/associationist/associations/preloader/singular_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#klassObject (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_classesObject



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_recordsObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/associationist/associations/preloader/singular_association.rb', line 50

def run?
  @run
end

#runnable_loadersObject

handle >=7.0



46
47
48
# File 'lib/associationist/associations/preloader/singular_association.rb', line 46

def runnable_loaders
  [self]
end

#table_nameObject



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