Class: Spree::Admin::Table::Filter

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/admin/table/filter.rb

Constant Summary collapse

OPERATORS =
{
  eq: { label: 'equals', predicate: '_eq' },
  not_eq: { label: 'does not equal', predicate: '_not_eq' },
  cont: { label: 'contains', predicate: '_cont' },
  not_cont: { label: 'does not contain', predicate: '_not_cont' },
  start: { label: 'starts with', predicate: '_start' },
  end: { label: 'ends with', predicate: '_end' },
  gt: { label: 'greater than', predicate: '_gt' },
  gteq: { label: 'greater than or equal to', predicate: '_gteq' },
  lt: { label: 'less than', predicate: '_lt' },
  lteq: { label: 'less than or equal to', predicate: '_lteq' },
  in: { label: 'is any of', predicate: '_in' },
  not_in: { label: 'is none of', predicate: '_not_in' },
  null: { label: 'is empty', predicate: '_null', no_value: true },
  not_null: { label: 'is not empty', predicate: '_not_null', no_value: true }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field: nil, operator: :eq, value: nil, id: nil) ⇒ Filter

Returns a new instance of Filter.



24
25
26
27
28
29
# File 'app/models/spree/admin/table/filter.rb', line 24

def initialize(field: nil, operator: :eq, value: nil, id: nil)
  @field = field
  @operator = operator.to_sym
  @value = value
  @id = id || SecureRandom.hex(8)
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



22
23
24
# File 'app/models/spree/admin/table/filter.rb', line 22

def field
  @field
end

#idObject

Returns the value of attribute id.



22
23
24
# File 'app/models/spree/admin/table/filter.rb', line 22

def id
  @id
end

#operatorObject

Returns the value of attribute operator.



22
23
24
# File 'app/models/spree/admin/table/filter.rb', line 22

def operator
  @operator
end

#valueObject

Returns the value of attribute value.



22
23
24
# File 'app/models/spree/admin/table/filter.rb', line 22

def value
  @value
end

Class Method Details

.from_params(params) ⇒ Filter?

Create filter from params hash

Parameters:

  • params (Hash)

Returns:



94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/spree/admin/table/filter.rb', line 94

def self.from_params(params)
  return nil unless params.is_a?(Hash)

  params = params.symbolize_keys
  new(
    field: params[:field],
    operator: params[:operator] || :eq,
    value: params[:value],
    id: params[:id]
  )
end

.operators_for_selectArray<Hash>

Get available operators with labels for UI

Returns:

  • (Array<Hash>)


108
109
110
111
112
# File 'app/models/spree/admin/table/filter.rb', line 108

def self.operators_for_select
  OPERATORS.map do |key, config|
    { value: key.to_s, label: config[:label], no_value: config[:no_value] || false }
  end
end

Instance Method Details

#operator_labelString

Get human-readable operator label

Returns:

  • (String)


75
76
77
# File 'app/models/spree/admin/table/filter.rb', line 75

def operator_label
  OPERATORS.dig(@operator, :label) || @operator.to_s.humanize
end

#requires_value?Boolean

Check if this operator requires a value

Returns:

  • (Boolean)


81
82
83
# File 'app/models/spree/admin/table/filter.rb', line 81

def requires_value?
  !OPERATORS.dig(@operator, :no_value)
end

#to_hHash

Convert to hash

Returns:

  • (Hash)


87
88
89
# File 'app/models/spree/admin/table/filter.rb', line 87

def to_h
  { field: field, operator: operator, value: value, id: id }
end

#to_ransack_paramHash

Convert to ransack parameter format

Returns:

  • (Hash)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/spree/admin/table/filter.rb', line 33

def to_ransack_param
  return {} if field.blank?

  operator_config = OPERATORS[@operator]
  return {} unless operator_config

  param_name = "#{field}#{operator_config[:predicate]}"

  if operator_config[:no_value]
    { param_name => true }
  else
    { param_name => extract_ransack_value }
  end
end