Class: Mensa::Filter

Inherits:
Object
  • Object
show all
Includes:
ConfigReaders
Defined in:
app/tables/mensa/filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column:, config:, table:) ⇒ Filter

Returns a new instance of Filter.



36
37
38
39
40
41
# File 'app/tables/mensa/filter.rb', line 36

def initialize(column:, config:, table:)
  @column = column
  @config = self.class.definition.merge(config || {})
  @table = table
  validate_operators!
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



9
10
11
# File 'app/tables/mensa/filter.rb', line 9

def column
  @column
end

#tableObject (readonly)

Returns the value of attribute table.



9
10
11
# File 'app/tables/mensa/filter.rb', line 9

def table
  @table
end

Class Method Details

.OPERATORSObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/tables/mensa/filter.rb', line 18

def OPERATORS
  [
    [:is, I18n.t("mensa.operators.is"), true],
    [:isnt, I18n.t("mensa.operators.isnt"), true],
    [:matches, I18n.t("mensa.operators.matches"), true],
    [:does_not_match, I18n.t("mensa.operators.does_not_match"), true],
    [:gt, I18n.t("mensa.operators.gt"), true],
    [:gteq, I18n.t("mensa.operators.gteq"), true],
    [:lt, I18n.t("mensa.operators.lt"), true],
    [:lteq, I18n.t("mensa.operators.lteq"), true],
    [:is_duplicate, I18n.t("mensa.operators.is_duplicate"), false],
    [:is_current, I18n.t("mensa.operators.is_current"), false],
    [:is_empty, I18n.t("mensa.operators.is_empty"), false],
    [:isnt_empty, I18n.t("mensa.operators.isnt_empty"), false]
  ].freeze
end

Instance Method Details

#asObject

This defines how the filter should be displayed in the value popover :select => as a select input :checkbox => as a checkbox input :string => as a text input



61
62
63
# File 'app/tables/mensa/filter.rb', line 61

def as
  config[:as]
end

#collectionObject



47
48
49
50
51
52
53
54
55
# File 'app/tables/mensa/filter.rb', line 47

def collection
  return unless config&.key?(:collection)

  if config[:collection].is_a? Proc
    table.original_view_context.instance_exec(&config[:collection])
  else
    config[:collection]
  end
end

#filter_scope(record_scope) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'app/tables/mensa/filter.rb', line 81

def filter_scope(record_scope)
  if scope
    record_scope.instance_exec(normalize(value), &scope)
  else
    return apply_duplicate_filter(record_scope) if operator == :is_duplicate

    query, hash = query_and_hash_for_operator
    record_scope = (column.filter.having? ? record_scope.having(query, hash) : record_scope.where(query, hash)) if query.present?
    record_scope
  end
end

#formatted_valueObject



71
72
73
74
75
76
77
78
79
# File 'app/tables/mensa/filter.rb', line 71

def formatted_value
  options = collection_options

  if value.is_a?(Array)
    value.map { |entry| label_for_value(entry, options) }.join(", ")
  else
    label_for_value(value, options)
  end
end

#input_typeObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/tables/mensa/filter.rb', line 150

def input_type
  case column.type
  when :integer
    "number"
  when :date
    "date"
  when :datetime
    "datetime-local"
  else
    "text"
  end
end

#multiple?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/tables/mensa/filter.rb', line 43

def multiple?
  !!multiple
end

#operator_labelObject



142
143
144
# File 'app/tables/mensa/filter.rb', line 142

def operator_label
  Mensa::Filter.OPERATORS.find { |op| op[0] == operator }[1]
end

#operator_with_value?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/tables/mensa/filter.rb', line 146

def operator_with_value?
  Mensa::Filter.OPERATORS.find { |op| op[0] == operator }[2]
end

#operatorsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/tables/mensa/filter.rb', line 93

def operators
  operators = Mensa::Filter.OPERATORS.dup
  if config[:operators].present?
    operators = operators.select { |op| config[:operators].include?(op[0]) }
  else
    operators.delete_if { |op| op[0] == :is_current } unless Current.method_defined?(column.name, false)
    operators.delete_if { |op| [:matches, :does_not_match].include?(op[0]) } if collection.present?
    operators.delete_if { |op| [:matches, :does_not_match].include?(op[0]) } if column.type == :integer || column.type == :date || column.type == :datetime
    operators.delete_if { |op| [:is, :isnt].include?(op[0]) } if column.type == :date || column.type == :datetime
    operators.delete_if { |op| [:gt, :lt, :gteq, :lteq].include?(op[0]) } if column.type == :string || column.type.blank?
  end
  operators
end

#query_and_hash_for_operatorObject

Used in the where clause



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/tables/mensa/filter.rb', line 108

def query_and_hash_for_operator
  hash = {column: column.attribute_for_condition, value: normalize(value)}

  query = case operator
  when :is_empty
    (column.type == :string) ? ":column IS NULL OR :column = ''" : ":column IS NULL"
  when :isnt_empty
    (column.type == :string) ? ":column IS NOT NULL AND :column != ''" : ":column IS NOT NULL"
  when :is_current
    ":column = :value"
  when :matches
    ":column LIKE :value"
  when :does_not_match
    ":column NOT LIKE :value"
  when :is
    hash[:value] = value if hash[:value].is_a?(Array)
    ":column = :value"
  when :isnt
    hash[:value] = value if hash[:value].is_a?(Array)
    ":column != :value"
  when :gt
    ":column > :value"
  when :lt
    ":column < :value"
  when :gteq
    ":column >= :value"
  when :lteq
    ":column <= :value"
  else
    raise ArgumentError, "Unknown filter operator #{operator.inspect} for column :#{column.name}"
  end
  [query, hash]
end

#to_sObject



65
66
67
68
69
# File 'app/tables/mensa/filter.rb', line 65

def to_s
  parts = [column.human_name, operator_label]
  parts << formatted_value if formatted_value.present? && operator_with_value?
  parts.join(" ")
end