Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add xboolean filter instead of eboolean

  • Loading branch information...
commit cbcea945f8ff15b5d3fcb3f7b87fe2d783a4d851 1 parent c34fd49
Bogdan Gusiev authored
5 Readme.markdown
View
@@ -38,8 +38,7 @@ class SimpleReport
end
filter(:category, :enum, :select => ["first", "second"])
- filter(:disabled, :eboolean)
- filter(:confirmed, :boolean)
+ filter(:disabled, :xboolean)
filter(:group_id, :integer, :multiple => true)
filter(:logins_count, :integer, :range => true)
filter(:group_name, :string, :header => "Group") do |value|
@@ -120,7 +119,7 @@ Datagrid supports different type of filters including:
* date
* datetime
* boolean
-* eboolean - the select of "yes", "no" and any
+* xboolean - the select of "yes", "no" and any
* enum
* string
* dynamic
2  lib/datagrid/filters.rb
View
@@ -6,6 +6,7 @@ module Filters
require "datagrid/filters/base_filter"
require "datagrid/filters/enum_filter"
require "datagrid/filters/boolean_enum_filter"
+ require "datagrid/filters/extended_boolean_filter"
require "datagrid/filters/boolean_filter"
require "datagrid/filters/date_filter"
require "datagrid/filters/date_time_filter"
@@ -22,6 +23,7 @@ module Filters
:string => Filters::StringFilter,
:default => Filters::DefaultFilter,
:eboolean => Filters::BooleanEnumFilter ,
+ :xboolean => Filters::ExtendedBooleanFilter ,
:boolean => Filters::BooleanFilter ,
:integer => Filters::IntegerFilter,
:enum => Filters::EnumFilter,
4 lib/datagrid/filters/base_filter.rb
View
@@ -16,7 +16,6 @@ def parse(value)
raise NotImplementedError, "#parse(value) suppose to be overwritten"
end
-
def unapplicable_value?(value)
value.nil? ? !allow_nil? : value.blank? && !allow_blank?
end
@@ -87,11 +86,12 @@ def self.form_builder_helper_name
def default_filter_block
filter = self
lambda do |value, scope, grid|
- filter.dummy? ? nil : filter.default_filter(value, scope, grid)
+ filter.default_filter(value, scope, grid)
end
end
def default_filter(value, scope, grid)
+ return nil if dummy?
driver = grid.driver
if !driver.has_column?(scope, name) && driver.to_scope(scope).respond_to?(name)
driver.to_scope(scope).send(name, value)
8 lib/datagrid/filters/boolean_enum_filter.rb
View
@@ -10,12 +10,4 @@ def initialize(report, attribute, options = {}, &block)
super(report, attribute, options, &block)
end
- def apply(grid_object, scope, value)
- super(grid_object, scope, value)
- end
-
- def to_boolean(value)
- #TODO decide what to do with conversion
- end
-
end
23 lib/datagrid/filters/extended_boolean_filter.rb
View
@@ -0,0 +1,23 @@
+class Datagrid::Filters::ExtendedBooleanFilter < Datagrid::Filters::EnumFilter #:nodoc:
+
+ YES = "YES"
+ NO = "NO"
+
+ def initialize(report, attribute, options = {}, &block)
+ options[:select] = [YES, NO].map do |key, value|
+ [I18n.t("datagrid.filters.xboolean.#{key.downcase}"), key]
+ end
+ super(report, attribute, options, &block)
+ end
+
+ def execute(value, scope, grid_object)
+ value = value.blank? ? nil : ::Datagrid::Utils.booleanize(value)
+ super(value, scope, grid_object)
+ end
+
+ def parse(value)
+ return nil if value.blank?
+ super(value)
+ end
+
+end
3  lib/datagrid/form_builder.rb
View
@@ -20,6 +20,9 @@ def datagrid_label(filter_or_attribute, options = {}, &block)
def datagrid_boolean_enum_filter(attribute_or_filter, options = {})
datagrid_enum_filter(attribute_or_filter, options)
end
+ def datagrid_extended_boolean_filter(attribute_or_filter, options = {})
+ datagrid_enum_filter(attribute_or_filter, options)
+ end
def datagrid_boolean_filter(attribute_or_filter, options = {})
check_box(datagrid_get_attribute(attribute_or_filter), options.reverse_merge(datagrid_extra_checkbox_options))
5 lib/datagrid/locale/en.yml
View
@@ -23,6 +23,11 @@ en:
eboolean:
"yes": "Yes"
"no": "No"
+
+ xboolean:
+ "yes": "Yes"
+ "no": "No"
+
dynamic:
operations:
">=": "&ge;"
3  lib/datagrid/utils.rb
View
@@ -6,6 +6,9 @@ class << self
TRUTH = [true, 1, "1", "true", "yes", "on"]
def booleanize(value)
+ if value.respond_to?(:downcase)
+ value = value.downcase
+ end
TRUTH.include?(value)
end
35 spec/datagrid/filters/extended_boolean_filter_spec.rb
View
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe Datagrid::Filters::ExtendedBooleanFilter do
+
+ it "should support select option" do
+ test_report do
+ scope {Entry}
+ filter(:disabled, :xboolean)
+ end.class.filter_by_name(:disabled).select.should == [["Yes", "YES"], ["No", "NO"]]
+ end
+
+ it "should generate pass boolean value to filter block" do
+ grid = test_report do
+ scope {Entry}
+ filter(:disabled, :xboolean)
+ end
+
+ disabled_entry = Entry.create!(:disabled => true)
+ enabled_entry = Entry.create!(:disabled => false)
+
+ grid.disabled.should be_nil
+ grid.assets.should include(disabled_entry, enabled_entry)
+ grid.disabled = "YES"
+
+ grid.disabled.should == "YES"
+ grid.assets.should include(disabled_entry)
+ grid.assets.should_not include(enabled_entry)
+ grid.disabled = "NO"
+ grid.disabled.should == "NO"
+ grid.assets.should include(enabled_entry)
+ grid.assets.should_not include(disabled_entry)
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.