Class: Quickery::AssociationBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, parent_builder: nil, inverse_association_name: nil) ⇒ AssociationBuilder



9
10
11
12
13
14
15
16
# File 'lib/quickery/association_builder.rb', line 9

def initialize(model:, parent_builder: nil, inverse_association_name: nil)
  @model = model
  @parent_builder = parent_builder
  @inverse_association_name = inverse_association_name
  @reflections = model.reflections
  @belongs_to_association_names = @reflections.map{ |key, value| value.macro == :belongs_to ? key : nil }.compact
  @column_names = model.column_names
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/quickery/association_builder.rb', line 85

def method_missing(method_name, *args, &block)
  method_name_str = method_name.to_s
  if @belongs_to_association_names.include? method_name_str
    @belongs_to = @reflections[method_name_str]
    @child_builder = AssociationBuilder.new(model: belongs_to.class_name.constantize, parent_builder: self, inverse_association_name: method_name_str)
  elsif @column_names.include? method_name_str
    QuickeryBuilder.new(dependee_column_name: method_name_str, last_association_builder: self)
  else
    super
  end
end

Instance Attribute Details

#belongs_toObject (readonly)

Returns the value of attribute belongs_to.



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

def belongs_to
  @belongs_to
end

#child_builderObject (readonly)

Returns the value of attribute child_builder.



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

def child_builder
  @child_builder
end

#inverse_association_nameObject (readonly)

Returns the value of attribute inverse_association_name.



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

def inverse_association_name
  @inverse_association_name
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#parent_builderObject (readonly)

Returns the value of attribute parent_builder.



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

def parent_builder
  @parent_builder
end

Instance Method Details

#_quickery_dependee_record(record_to_be_saved) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/quickery/association_builder.rb', line 71

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

  _quickery_get_child_builders(include_self: true).inject(record_to_be_saved) do |record, association_builder|
    if association_builder.belongs_to
      record.send(association_builder.belongs_to.name)
    else
      record
    end
  end
end

#_quickery_dependent_records(record_to_be_saved) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/quickery/association_builder.rb', line 54

def _quickery_dependent_records(record_to_be_saved)
  primary_key_value = record_to_be_saved.send(record_to_be_saved.class.primary_key)
  most_parent_model = _quickery_get_parent_builders.last.model

  records = most_parent_model.all

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

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

#_quickery_get_child_builders(include_self: false, builders: []) ⇒ Object

we need to prepend _quickery to all methods, to make sure no conflicts with association names that are dynamically invoked through ‘method_missing` below



20
21
22
23
24
25
26
27
28
29
# File 'lib/quickery/association_builder.rb', line 20

def _quickery_get_child_builders(include_self: false, builders: [])
  builders << self if include_self

  if @child_builder.nil?
    builders
  else
    builders << @child_builder
    return @child_builder._quickery_get_child_builders(builders: builders)
  end
end

#_quickery_get_joins_arg(current_joins_arg = nil) ⇒ Object



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

def _quickery_get_joins_arg(current_joins_arg = nil)
  if @parent_builder.nil?
    current_joins_arg
  else
    if current_joins_arg.nil?
      @parent_builder._quickery_get_joins_arg(@inverse_association_name.to_sym)
    else
      @parent_builder._quickery_get_joins_arg({ @inverse_association_name.to_sym => current_joins_arg })
    end
  end
end

#_quickery_get_parent_builders(include_self: false, builders: []) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/quickery/association_builder.rb', line 31

def _quickery_get_parent_builders(include_self: false, builders: [])
  builders << self if include_self

  if @parent_builder.nil?
    builders
  else
    builders << @parent_builder
    @parent_builder._quickery_get_parent_builders(builders: builders)
  end
end