Module: Tabulatr::Data::Filtering

Defined in:
lib/tabulatr/data/filtering.rb

Overview

– Copyright © 2010-2014 Peter Horn & Florian Thomas, metaminded UG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Instance Method Summary collapse

Instance Method Details

#apply_array_condition(column, value) ⇒ Object



105
106
107
# File 'lib/tabulatr/data/filtering.rb', line 105

def apply_array_condition(column, value)
  @relation = @relation.where(column.table_name => { column.name => value })
end

#apply_boolean_condition(column, value) ⇒ Object



70
71
72
# File 'lib/tabulatr/data/filtering.rb', line 70

def apply_boolean_condition(column, value)
  @relation = @relation.where("#{column.col_options.filter_sql} = ?", Tabulatr::Utility.string_to_boolean(value))
end

#apply_condition(n, v) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tabulatr/data/filtering.rb', line 54

def apply_condition(n,v)
  # puts "FILTER: »#{n.filter.inspect}«, »#{n.filter.class}«"
  case n.filter
  when :checkbox then apply_boolean_condition(n, v)
  when :decimal  then apply_string_condition("#{n.col_options.filter_sql} = ?", v.to_f)
  when :integer, :enum  then apply_string_condition("#{n.col_options.filter_sql} = ?", v.to_i)
  when :enum_multiselect then apply_array_condition(n, v)
  when :exact, Hash, Array then apply_string_condition("#{n.col_options.filter_sql} = ?", v)
  when :like     then apply_like_condition(n, v['like'])
  when :date     then apply_date_condition(n, v['date'])
  when :range    then apply_range_condition(n, v)
  when :custom   then apply_custom_filter(n, v)
  else raise "Wrong filter type for #{n.name}: #{n.filter}"
  end
end

#apply_custom_filter(filter, value) ⇒ Object



109
110
111
112
# File 'lib/tabulatr/data/filtering.rb', line 109

def apply_custom_filter(filter, value)
  filter_result = self.instance_exec @relation, value, &filter.block
  handle_search_result(filter_result)
end

#apply_date_condition(n, cond) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tabulatr/data/filtering.rb', line 74

def apply_date_condition(n, cond)
  today = Date.today
  yesterday = today - 1.day
  case cond['simple']
  when 'none' then return
  when 'today' then date_in_between(today, today.at_end_of_day, n)
  when 'yesterday' then date_in_between(yesterday, yesterday.at_end_of_day, n)
  when 'this_week' then date_in_between(today.at_beginning_of_week.beginning_of_day,
                          today.at_end_of_week.end_of_day, n)
  when 'last_7_days' then date_in_between((today - 6.day).beginning_of_day, today.at_end_of_day, n)
  when 'this_month' then date_in_between(today.at_beginning_of_month.beginning_of_day,
                            today.at_end_of_month.end_of_day, n)
  when 'last_30_days' then date_in_between((today - 29.day).beginning_of_day, today.at_end_of_day, n)
  when 'from_to' then date_in_between((Date.parse(cond['from']) rescue nil), (Date.parse(cond['to']) rescue nil), n)
  end
end

#apply_filters(filter_params) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/tabulatr/data/filtering.rb', line 44

def apply_filters(filter_params)
  return unless filter_params
  filter_params.permit!.to_hash.with_indifferent_access.each do |param|
    name, value = param
    next unless value.present?

    apply_condition(find_column(name), value)
  end
end

#apply_like_condition(column, value) ⇒ Object



95
96
97
98
# File 'lib/tabulatr/data/filtering.rb', line 95

def apply_like_condition(column, value)
  like ||= Tabulatr::Utility.like_statement
  apply_string_condition("#{column.col_options.filter_sql} #{like} ?", "%#{value}%") if value.present?
end

#apply_range_condition(column, hash) ⇒ Object



100
101
102
103
# File 'lib/tabulatr/data/filtering.rb', line 100

def apply_range_condition(column, hash)
  apply_string_condition("#{column.col_options.filter_sql} >= ?", "#{hash[:from]}")
  apply_string_condition("#{column.col_options.filter_sql} <= ?", "#{hash[:to]}")
end

#apply_search(query) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tabulatr/data/filtering.rb', line 26

def apply_search(query)
  like ||= Tabulatr::Utility.like_statement
  return unless query.present?
  if @search.is_a? Array
    query = query.strip.gsub(/['*%\s]+/, '%')
    a = @search.map do |name|
      column = table_columns.find{|c| c.name == name}
      nn = column ? column.col_options.filter_sql : name
      # nn = build_column_name name, use_for: :filter
      "(#{nn} #{like} '%#{query}%')"
    end
    a = a.join(' OR ')
    @relation = @relation.where(a)
  else # search is a proc
    execute_provided_search_block!(query)
  end
end

#apply_string_condition(replacement_string, value) ⇒ Object



91
92
93
# File 'lib/tabulatr/data/filtering.rb', line 91

def apply_string_condition(replacement_string, value)
   @relation = @relation.where(replacement_string, value) if value.present?
end