Class: Super::Filter::FormObject

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

Defined Under Namespace

Classes: FilterFormField

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FormObject.



47
48
49
50
51
52
53
# File 'lib/super/filter/form_object.rb', line 47

def initialize(model:, params:, schema:)
  @model = model
  @params = params
  @schema = schema

  @form_fields = {}
end

Instance Method Details

#apply_changes(relation) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/super/filter/form_object.rb', line 65

def apply_changes(relation)
  each_field do |form_field|
    next if form_field.specified_values.values.map(&:to_s).map(&:strip).all? { |specified_value| specified_value == "" }
    next if !Super::Filter::Operator.registry.key?(form_field.op)

    operator = Super::Filter::Operator.registry[form_field.op]
    updated_relation = operator.filter(relation, form_field.field_name, *form_field.specified_values.values)

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

  relation
end

#each_fieldObject



55
56
57
58
59
# File 'lib/super/filter/form_object.rb', line 55

def each_field
  @schema.fields.each do |field_name, _field_type|
    yield(form_field_for(field_name))
  end
end

#to_partial_pathObject



61
62
63
# File 'lib/super/filter/form_object.rb', line 61

def to_partial_path
  "filter"
end