Class: Quickery::AssociationChain

Inherits:
Object
  • Object
show all
Defined in:
lib/quickery/association_chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, parent_association_chain: nil, name: nil) ⇒ AssociationChain

Returns a new instance of AssociationChain.



11
12
13
14
15
# File 'lib/quickery/association_chain.rb', line 11

def initialize(model:, parent_association_chain: nil, name: nil)
  @model = model
  @parent_association_chain = parent_association_chain
  @name = name
end

Instance Attribute Details

#belongs_toObject (readonly)

Returns the value of attribute belongs_to.



9
10
11
# File 'lib/quickery/association_chain.rb', line 9

def belongs_to
  @belongs_to
end

#child_association_chainObject (readonly)

Returns the value of attribute child_association_chain.



6
7
8
# File 'lib/quickery/association_chain.rb', line 6

def child_association_chain
  @child_association_chain
end

#dependee_column_nameObject (readonly)

Returns the value of attribute dependee_column_name.



8
9
10
# File 'lib/quickery/association_chain.rb', line 8

def dependee_column_name
  @dependee_column_name
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/quickery/association_chain.rb', line 4

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/quickery/association_chain.rb', line 7

def name
  @name
end

#parent_association_chainObject (readonly)

Returns the value of attribute parent_association_chain.



5
6
7
# File 'lib/quickery/association_chain.rb', line 5

def parent_association_chain
  @parent_association_chain
end

#quickery_builderObject

Returns the value of attribute quickery_builder.



3
4
5
# File 'lib/quickery/association_chain.rb', line 3

def quickery_builder
  @quickery_builder
end

Instance Method Details

#build_children_association_chains(names_left:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/quickery/association_chain.rb', line 17

def build_children_association_chains(names_left:)
  current_name = names_left.first

  reflections = @model.reflections
  column_names = @model.column_names
  belongs_to_association_names = reflections.map{ |key, value| value.macro == :belongs_to ? key : nil }.compact

  if belongs_to_association_names.include? current_name
    @belongs_to = reflections[current_name]
    @child_association_chain = AssociationChain.new(
      model: belongs_to.class_name.constantize,
      parent_association_chain: self,
      name: current_name,
    )
    @child_association_chain.build_children_association_chains(names_left: names_left[1..-1])
    return self

  elsif column_names.include? current_name
    @dependee_column_name = current_name
    return self

  else
    raise Quickery::Errors::InvalidAssociationOrAttributeError.new(current_name)
  end
end

#child_association_chains(include_self: false, association_chains: []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/quickery/association_chain.rb', line 43

def child_association_chains(include_self: false, association_chains: [])
  association_chains << self if include_self

  if @child_association_chain.nil?
    association_chains
  else
    association_chains << @child_association_chain
    return @child_association_chain.child_association_chains(association_chains: association_chains)
  end
end

#dependee_record(from_record) ⇒ Object

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/quickery/association_chain.rb', line 77

def dependee_record(from_record)
  raise ArgumentError, 'argument should be an instance of @model' unless from_record.is_a? model

  child_association_chains(include_self: true).inject(from_record) do |from_record, association_chain|
    if association_chain.belongs_to
      from_record.send(association_chain.belongs_to.name) if from_record
    else
      from_record
    end
  end
end

#dependent_records(from_record) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/quickery/association_chain.rb', line 89

def dependent_records(from_record)
  primary_key_value = from_record.send(from_record.class.primary_key)
  most_parent_model = parent_association_chains.last.model

  records = most_parent_model.all

  unless (joins_arg_tmp = joins_arg).nil?
    records = records.joins(joins_arg_tmp)
  end

  records = records.where(
    model.table_name => {
      model.primary_key => primary_key_value
    }
  )
end

#joins_arg(current_joins_arg = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/quickery/association_chain.rb', line 65

def joins_arg(current_joins_arg = nil)
  if @parent_association_chain.nil?
    current_joins_arg
  else
    if current_joins_arg.nil?
      @parent_association_chain.joins_arg(@name.to_sym)
    else
      @parent_association_chain.joins_arg({ @name.to_sym => current_joins_arg })
    end
  end
end

#parent_association_chains(include_self: false, association_chains: []) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/quickery/association_chain.rb', line 54

def parent_association_chains(include_self: false, association_chains: [])
  association_chains << self if include_self

  if @parent_association_chain.nil?
    association_chains
  else
    association_chains << @parent_association_chain
    @parent_association_chain.parent_association_chains(association_chains: association_chains)
  end
end