Module: Datagrid::Filters::ClassMethods
- Defined in:
- lib/datagrid/filters.rb
Overview
self.included
Instance Method Summary collapse
-
#filter(attribute, type = :string, options = {}, &block) ⇒ Object
Defines the accessible attribute that is used to filter scope by the specified value with specified code.
- #filter_by_name(attribute) ⇒ Object
- #filters ⇒ Object
Instance Method Details
#filter(attribute, type = :string, options = {}, &block) ⇒ Object
Defines the accessible attribute that is used to filter scope by the specified value with specified code.
Example:
class UserGrid
include Datagrid
scope do
User.order("users.created_at desc")
end
filter(:name)
filter(:posts_count, :integer) do |value|
self.where(["posts_count >= ?", value])
end
end
Each filter becomes grid attribute. In order to create grid that display all users with name ‘John’ that have more than zero posts:
grid = UserGrid.new(:posts_count => 1, :name => "John")
grid.assets # SELECT * FROM users WHERE users.posts_count > 1 AND name = 'John'
Important! Take care about non-breaking the filter chain and force objects loading in filter. The filter block should always return a ActiveRecord::Scope
rather than Array
Default filter block
If no block given filter is generated automatically as simple select by filter name from scope.
Filter types
Filter does types conversion automatically. The following filter types are supported:
-
:default
(default) - leave value as is -
:date
- converts value to date using date parser -
:enum
- designed to be collection select. Additional options for easy form generation:-
:select
(required) - collection of values to match against.
-
-
:boolean
- converts value to true or false depending on whether it looks truly or not -
:integer
- converts given value to integer. -
:eboolean
- subtype of enum filter that provides select of “Yes”, “No” and “Any”. Could be useful.
Default filter options
Options that could be passed to any filter type:
-
:header
- human readable name of the filter. Default: generated from the filter name. -
:default
- default value of the filter. Default: nil. -
:multiple
- if true multiple values can be assigned to this filter. Default: false. -
:allow_nil
- determines if filter should be called if filter value is nil. Default: false. -
:allow_blank
- determines if filter should be called if filter value is #blank?. Default: false.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datagrid/filters.rb', line 103 def filter(attribute, type = :string, = {}, &block) klass = type.is_a?(Class) ? type : FILTER_TYPES[type] raise ConfigurationError, "filter class #{type.inspect} not found" unless klass block ||= default_filter(attribute) filter = klass.new(self, attribute, , &block) self.filters << filter datagrid_attribute(attribute) do |value| filter.format_values(value) end end |
#filter_by_name(attribute) ⇒ Object
41 42 43 44 45 |
# File 'lib/datagrid/filters.rb', line 41 def filter_by_name(attribute) self.filters.find do |filter| filter.name.to_sym == attribute.to_sym end end |
#filters ⇒ Object
37 38 39 |
# File 'lib/datagrid/filters.rb', line 37 def filters @filters ||= [] end |