Class: Lolita::Configuration::Filter
- Includes:
- Builder
- Defined in:
- lib/lolita/configuration/filter.rb
Overview
Lolita::Configuration::Filter is for filtering data in list view. By now it supports these field types:
-
Array
-
Boolean
To use it, call “filter” method in list block, filter method accepts field names as arguments. You can pass block and configure each field. Field configuration is just like in tab configuration.
Examples
# this will build country select field and is_deleted checkbox
list do
filter :country, :is_deleted
end
# For example you have text field "status" with values opened,closed,rejected
list do
filter do
field :status, :array, :values=> %w(open closed rejected)
field :is_deleted, :title => "Deleted"
end
end
Instance Attribute Summary collapse
-
#dbi ⇒ Object
readonly
Returns the value of attribute dbi.
Instance Method Summary collapse
- #field(*args, &block) ⇒ Object
- #fields(*args, &block) ⇒ Object
-
#fields=(fields) ⇒ Object
Set all fields in tab.
- #html_option_for_select(field) ⇒ Object
-
#initialize(dbi, *args, &block) ⇒ Filter
constructor
A new instance of Filter.
- #resource(params) ⇒ Object
- #search(*args, &block) ⇒ Object
-
#set_attributes(*args) ⇒ Object
Used to set attributes if block not given.
- #update(method_name, list, request) ⇒ Object
Methods included from Builder
#build, #builder, #builder=, #builder_default_name, #builder_default_options, #builder_default_state
Constructor Details
#initialize(dbi, *args, &block) ⇒ Filter
Returns a new instance of Filter.
31 32 33 34 35 36 |
# File 'lib/lolita/configuration/filter.rb', line 31 def initialize(dbi,*args,&block) @dbi = dbi @fields=Lolita::Configuration::Fields.new set_attributes(*args) self.instance_eval(&block) if block_given? end |
Instance Attribute Details
#dbi ⇒ Object (readonly)
Returns the value of attribute dbi.
29 30 31 |
# File 'lib/lolita/configuration/filter.rb', line 29 def dbi @dbi end |
Instance Method Details
#field(*args, &block) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/lolita/configuration/filter.rb', line 38 def field *args, &block field=Lolita::Configuration::Factory::Field.create(self.dbi,*args,&block) field @fields<<field field end |
#fields(*args, &block) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/lolita/configuration/filter.rb', line 60 def fields(*args, &block) if args && args.any? || block_given? args.each do |field_name| f = field(field_name) f.instance_eval(&block) if block_given? end end @fields end |
#fields=(fields) ⇒ Object
Set all fields in tab. Accept fields
as Array. Each array element can be Lolita::Configuration::Field object or Hash, that will be passed to #field method.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lolita/configuration/filter.rb', line 48 def fields=(fields) if fields.is_a?(Array) fields.each{|field_attr| if field_attr.is_a?(Lolita::Configuration::Field) @fields<<field_attr else self.field(field_attr) end } end end |
#html_option_for_select(field) ⇒ Object
91 92 93 94 95 |
# File 'lib/lolita/configuration/filter.rb', line 91 def html_option_for_select field { :include_blank => ::I18n.t('lolita.filter.include_blank_by_title', :title => field.title) } end |
#resource(params) ⇒ Object
122 123 124 125 126 |
# File 'lib/lolita/configuration/filter.rb', line 122 def resource(params) if klass = fields.any? ? fields.first.dbi.klass : nil klass.new(params[klass.to_s.underscore.to_sym]).extend(Module.new{def persisted?; true; end}) end end |
#search(*args, &block) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/lolita/configuration/filter.rb', line 83 def search *args, &block if args && args.any? || block_given? @search = Lolita::Configuration::Search.new(self.dbi,*args, &block) else @search end end |
#set_attributes(*args) ⇒ Object
Used to set attributes if block not given.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lolita/configuration/filter.rb', line 71 def set_attributes(*args) if args && args[0] if args[0].is_a?(Hash) args[0].each{|m,value| self.send("#{m}=".to_sym,value) } else fields *args end end end |
#update(method_name, list, request) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/lolita/configuration/filter.rb', line 97 def update method_name, list, request filter_params = request && request.params && request.params[:filter] page_criteria = if method_name == :paginate && self.search && filter_params search_criteria = self.search.run(nil,request) page_criteria = if search_criteria.respond_to?(:where) list.page_criteria.merge(search_criteria) elsif search_criteria.nil? list.page_criteria else search_criteria end else list.page_criteria.merge(self.dbi.filter(filter_params || {})) end list.instance_variable_set(:@page_criteria,page_criteria) end |