Class: Super::Filter::FormObject

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

Defined Under Namespace

Classes: AttributeForm, OperatorForm

Instance Method Summary collapse

Constructor Details

#initialize(model:, params:, schema:) ⇒ FormObject

Returns a new instance of FormObject.



75
76
77
78
79
80
81
# File 'lib/super/filter/form_object.rb', line 75

def initialize(model:, params:, schema:)
  @model = model
  @params = params || {}
  @schema = schema

  @form_fields = {}
end

Instance Method Details

#apply_changes(relation) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/super/filter/form_object.rb', line 102

def apply_changes(relation)
  each_attribute do |attribute_form|
    attribute_form.each_operator do |operator_form|
      next if operator_form.specified_values.values.map(&:to_s).map(&:presence).none?

      operator_behavior = operator_form.operator.behavior
      updated_relation = operator_behavior.call(relation, attribute_form.field_name, **operator_form.specified_values)

      if updated_relation.is_a?(ActiveRecord::Relation)
        relation = updated_relation
      end
    end
  end

  relation
end

#each_attributeObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/super/filter/form_object.rb', line 83

def each_attribute
  return enum_for(:each_attribute) if !block_given?

  @schema.fields.each do |field_name, field_operators|
    attribute_form = AttributeForm.new(
      model: @model,
      field_name: field_name,
      operators: field_operators,
      params: @params[field_name]
    )

    yield(attribute_form)
  end
end

#to_partial_pathObject



98
99
100
# File 'lib/super/filter/form_object.rb', line 98

def to_partial_path
  "filter"
end