Class: Super::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/super/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, params:, current_path:) ⇒ Query

Returns a new instance of Query.



5
6
7
8
9
10
11
# File 'lib/super/query.rb', line 5

def initialize(model:, params:, current_path:)
  @model = model
  @params = params
  @path = current_path
  @addons = {}
  @backwards_addons = {}
end

Instance Attribute Details

#addonsObject (readonly)

Returns the value of attribute addons.



13
14
15
# File 'lib/super/query.rb', line 13

def addons
  @addons
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/super/query.rb', line 16

def path
  @path
end

Instance Method Details

#apply_changes(records) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/super/query.rb', line 49

def apply_changes(records)
  addons.each_value do |addon|
    records = addon.apply_changes(records)
  end

  records
end

#build(klass, namespace:, **additional_initialization_arguments) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/super/query.rb', line 23

def build(klass, namespace:, **additional_initialization_arguments)
  params_for_querier =
    params.fetch(namespace) { ActiveSupport::HashWithIndifferentAccess.new }

  instance = klass.new(
    model: model,
    params: params_for_querier,
    **additional_initialization_arguments
  )

  addons[namespace] = instance
  backwards_addons[instance] = namespace
  instance
end

#form_optionsObject



42
43
44
45
46
47
# File 'lib/super/query.rb', line 42

def form_options
  {
    url: path,
    method: :get
  }
end

#namespace_for(query_form_object) ⇒ Object



38
39
40
# File 'lib/super/query.rb', line 38

def namespace_for(query_form_object)
  backwards_addons.fetch(query_form_object)
end

#to_partial_pathObject



57
58
59
# File 'lib/super/query.rb', line 57

def to_partial_path
  "query"
end