Module: Datagrid::Filters::InstanceMethods

Defined in:
lib/datagrid/filters.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#assetsObject

:nodoc:


150
151
152
# File 'lib/datagrid/filters.rb', line 150

def assets # :nodoc:
  apply_filters(super, filters)
end

#default_filterObject


191
192
193
# File 'lib/datagrid/filters.rb', line 191

def default_filter
  self.class.default_filter
end

#filter_by(*filters) ⇒ Object

Returns assets filtered only by specified filters Allows partial filtering


177
178
179
# File 'lib/datagrid/filters.rb', line 177

def filter_by(*filters)
  apply_filters(scope, filters.map{|f| filter_by_name(f)})
end

#filter_by_name(name) ⇒ Object

Returns filter object with the given name


171
172
173
# File 'lib/datagrid/filters.rb', line 171

def filter_by_name(name)
  self.class.filter_by_name(name)
end

#filter_value(filter) ⇒ Object

Returns filter value for given filter definition


155
156
157
# File 'lib/datagrid/filters.rb', line 155

def filter_value(filter)
  self[filter.name]
end

#filter_value_as_string(name) ⇒ Object

Returns string representation of filter value


160
161
162
163
164
165
166
167
168
# File 'lib/datagrid/filters.rb', line 160

def filter_value_as_string(name)
  filter = filter_by_name(name)
  value = filter_value(filter)
  if value.is_a?(Array)
    value.map {|v| filter.format(v) }.join(filter.separator)
  else
    filter.format(value)
  end
end

#filtersObject

Returns all currently enabled filters


196
197
198
199
200
# File 'lib/datagrid/filters.rb', line 196

def filters
  self.class.filters.select do |filter|
    filter.enabled?(self)
  end
end

#initialize(*args, &block) ⇒ Object

:nodoc:


142
143
144
145
146
147
148
# File 'lib/datagrid/filters.rb', line 142

def initialize(*args, &block) # :nodoc:
  self.filters_array = self.class.filters_array.clone
  self.filters_array.each do |filter|
    self[filter.name] = filter.default(self)
  end
  super(*args, &block)
end

#select_options(filter) ⇒ Object

Returns select options for specific filter or filter name If given filter doesn't support select options raises `ArgumentError`


183
184
185
186
187
188
189
# File 'lib/datagrid/filters.rb', line 183

def select_options(filter)
  filter = filter_by_name(filter)
  unless filter.class.included_modules.include?(::Datagrid::Filters::SelectOptions)
    raise ::Datagrid::ArgumentError, "#{filter.name} with type #{FILTER_TYPES.invert[filter.class].inspect} can not have select options"
  end
  filter.select(self)
end