Module: MightyGrid::Filters::InstanceMethods

Defined in:
lib/mighty_grid/filters.rb

Instance Method Summary collapse

Instance Method Details

#add_filter_param(param, value) ⇒ Object

Add param to filters



50
51
52
# File 'lib/mighty_grid/filters.rb', line 50

def add_filter_param(param, value)
  @mg_params[filter_param_name.to_sym][param] = value unless @mg_params[filter_param_name.to_sym].key?(param)
end

#ar_apply_filtersObject

Apply filters



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mighty_grid/filters.rb', line 73

def ar_apply_filters
  @filters.each_pair do |filter_name, filter|
    next if (filter_params.blank?   && filter.default.blank? ||
             filter_params.present? && filter_params[filter_name.to_s].blank?)

    filter_value = get_filter_value(filter_name, filter)

    table_name = filter.model.table_name

    case filter
    when EnumFilter
      @relation = @relation.where(table_name => { filter.attribute => filter_value })
    when BooleanFilter
      value = %w(true 1 t).include?(filter_value) ? true : false
      @relation = @relation.where(table_name => { filter.attribute => value })
    when StringFilter, TextFilter
      @relation = @relation.where("#{table_name}.#{filter.attribute} #{like_operator} ?", "%#{filter_value}%")
    when CustomFilter
      if filter.scope.kind_of?(Proc)
        @relation = filter.scope.call(@relation, filter_value)
      else
        @relation = @relation.send(filter.scope, filter_value)
      end
    end
  end
end

#filter_param_nameObject

Get filter parameter name



40
41
42
# File 'lib/mighty_grid/filters.rb', line 40

def filter_param_name
  'f'
end

#filter_paramsObject

Get filter parameters



45
46
47
# File 'lib/mighty_grid/filters.rb', line 45

def filter_params
  @mg_params[filter_param_name.to_sym] || {}
end

#get_filter_name(field, model = nil) ⇒ Object

Get filter name by field



55
56
57
58
# File 'lib/mighty_grid/filters.rb', line 55

def get_filter_name(field, model = nil)
  field_name = model.present? ? "#{model.table_name}.#{field}" : field
  "#{name}[#{filter_param_name}][#{field_name}]"
end

#get_filter_value(filter_name, filter) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/mighty_grid/filters.rb', line 64

def get_filter_value(filter_name, filter)
  if filter_params.present?
    filter_params[filter_name.to_s]
  else
    filter.default.to_s
  end
end

#get_search_nameObject



60
61
62
# File 'lib/mighty_grid/filters.rb', line 60

def get_search_name
  self.search_name
end

#search_apply_filterObject



105
106
107
# File 'lib/mighty_grid/filters.rb', line 105

def search_apply_filter
  self.query = get_filter_value(self.search_name, @filters[self.search_name])
end

#ts_apply_filtersObject

Thinking Sphinx apply filters



101
102
103
# File 'lib/mighty_grid/filters.rb', line 101

def ts_apply_filters
  # TODO: Make filters for Thinking Sphinx
end