Class: Mensa::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



15
16
17
18
19
# File 'app/tables/mensa/filter.rb', line 15

def initialize(column:, config:, table:)
  @column = column
  @config = self.class.definition.merge(config || {})
  @table = table
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

Instance Method Details

#collectionObject



21
22
23
24
25
26
27
28
29
# File 'app/tables/mensa/filter.rb', line 21

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/tables/mensa/filter.rb', line 35

def filter_scope(record_scope)
  if scope
    record_scope.instance_exec(normalize(value), &scope)
  else
    case operator
    when :matches
      record_scope.where("#{column.attribute_for_condition} LIKE ?", "%#{normalize(value)}%")
    when :equals
      record_scope.where(column.attribute_for_condition => normalize(value))
    else
      # Ignore unknown operators
      record_scope
    end
  end
end

#to_sObject



31
32
33
# File 'app/tables/mensa/filter.rb', line 31

def to_s
  "#{column.human_name}: #{value}"
end