Skip to content
Browse files

Integer range filter form builder

  • Loading branch information...
1 parent 45baf70 commit 38aabed8fc455bc03946e7abf36c44ab89e7beb0 @bogdan committed Dec 10, 2012
Showing with 67 additions and 23 deletions.
  1. +1 −1 lib/datagrid/filters/base_filter.rb
  2. +23 −19 lib/datagrid/form_builder.rb
  3. +43 −3 spec/datagrid/form_builder_spec.rb
View
2 lib/datagrid/filters/base_filter.rb
@@ -32,7 +32,7 @@ def apply(grid_object, scope, value)
def format_values(value)
if !self.multiple && value.is_a?(Array)
- raise Datagrid::ArgumentError, "#{grid.class}.#{name} filter can not accept Array argument. Use :multiple option."
+ raise Datagrid::ArgumentError, "#{grid}##{name} filter can not accept Array argument. Use :multiple option."
end
values = Array.wrap(value)
values.map! do |v|
View
42 lib/datagrid/form_builder.rb
@@ -24,24 +24,7 @@ def datagrid_boolean_filter(attribute_or_filter, options = {})
end
def datagrid_date_filter(attribute_or_filter, options = {})
- filter = datagrid_get_filter(attribute_or_filter)
- if filter.range?
- options = options.merge(:multiple => true)
-
- from_options = Datagrid::Utils.add_html_classes(options, "from")
- from_value = object[filter.name].try(:first)
-
- to_options = Datagrid::Utils.add_html_classes(options, "to")
- to_value = object[filter.name].try(:last)
- # 2 inputs: "from date" and "to date" to specify a range
- [
- text_field(filter.name, from_options.merge!(:value => from_value)),
- I18n.t("datagrid.date_range_separator", :default => '<div class="separator"> - </div>'),
- text_field(filter.name, to_options.merge!(:value => to_value))
- ].join.html_safe
- else
- text_field(filter.name, options)
- end
+ datagrid_range_filter(:date, attribute_or_filter, options)
end
def datagrid_default_filter(attribute_or_filter, options = {})
@@ -61,7 +44,28 @@ def datagrid_integer_filter(attribute_or_filter, options = {})
if filter.multiple && self.object[filter.name].blank?
options[:value] = ""
end
- text_field filter.name, options
+ datagrid_range_filter(:integer, filter, options)
+ end
+
+ def datagrid_range_filter(type, attribute_or_filter, options = {})
+ filter = datagrid_get_filter(attribute_or_filter)
+ if filter.range?
+ options = options.merge(:multiple => true)
+
+ from_options = Datagrid::Utils.add_html_classes(options, "from")
+ from_value = object[filter.name].try(:first)
+
+ to_options = Datagrid::Utils.add_html_classes(options, "to")
+ to_value = object[filter.name].try(:last)
+ # 2 inputs: "from date" and "to date" to specify a range
+ [
+ text_field(filter.name, from_options.merge!(:value => from_value)),
+ I18n.t("datagrid.misc.#{type}_range_separator", :default => "<span class=\"separator #{type}\"> - </span>"),
+ text_field(filter.name, to_options.merge!(:value => to_value))
+ ].join.html_safe
+ else
+ text_field(filter.name, options)
+ end
end
def datagrid_string_filter(attribute_or_filter, options = {})
View
46 spec/datagrid/form_builder_spec.rb
@@ -64,6 +64,46 @@ class MyTemplate
)}
end
+ context "with integer filter type and range option" do
+ let(:_filter) { :group_id }
+ let(:_grid) {
+ test_report(:group_id => _range) do
+ scope {Entry}
+ filter(:group_id, :integer, :range => true)
+ end
+ }
+ context "with only left bound" do
+
+ let(:_range) { [10, nil]}
+ it { should equal_to_dom(
+ '<input class="group_id integer_filter from" id="report_group_id" multiple name="report[group_id][]" size="30" type="text" value="10"/>' +
+ '<span class="separator integer"> - </span>' +
+ '<input class="group_id integer_filter to" id="report_group_id" multiple name="report[group_id][]" size="30" type="text"/>'
+ )}
+ it { should be_html_safe }
+ end
+ context "with only right bound" do
+
+ let(:_range) { [nil, 10]}
+ it { should equal_to_dom(
+ '<input class="group_id integer_filter from" id="report_group_id" multiple name="report[group_id][]" size="30" type="text"/>' +
+ '<span class="separator integer"> - </span>' +
+ '<input class="group_id integer_filter to" id="report_group_id" multiple name="report[group_id][]" size="30" type="text" value="10"/>'
+ )}
+ it { should be_html_safe }
+ end
+
+ context "with invalid range value" do
+ let(:_range) { 2..1 }
+ it { should equal_to_dom(
+ '<input class="group_id integer_filter from" id="report_group_id" multiple name="report[group_id][]" size="30" type="text" value="2"/>' +
+ '<span class="separator integer"> - </span>' +
+ '<input class="group_id integer_filter to" id="report_group_id" multiple name="report[group_id][]" size="30" type="text" value="1"/>'
+ )}
+ end
+ end
+
+
context "with date filter type and range option" do
let(:_filter) { :created_at }
let(:_grid) {
@@ -77,7 +117,7 @@ class MyTemplate
let(:_range) { ["2012-01-03", nil]}
it { should equal_to_dom(
'<input class="created_at date_filter from" id="report_created_at" multiple name="report[created_at][]" size="30" type="text" value="2012-01-03"/>' +
- '<div class="separator"> - </div>' +
+ '<span class="separator date"> - </span>' +
'<input class="created_at date_filter to" id="report_created_at" multiple name="report[created_at][]" size="30" type="text"/>'
)}
it { should be_html_safe }
@@ -87,7 +127,7 @@ class MyTemplate
let(:_range) { [nil, "2012-01-03"]}
it { should equal_to_dom(
'<input class="created_at date_filter from" id="report_created_at" multiple name="report[created_at][]" size="30" type="text"/>' +
- '<div class="separator"> - </div>' +
+ '<span class="separator date"> - </span>' +
'<input class="created_at date_filter to" id="report_created_at" multiple name="report[created_at][]" size="30" type="text" value="2012-01-03"/>'
)}
it { should be_html_safe }
@@ -97,7 +137,7 @@ class MyTemplate
let(:_range) { Date.parse('2012-01-02')..Date.parse('2012-01-01') }
it { should equal_to_dom(
'<input class="created_at date_filter from" id="report_created_at" multiple name="report[created_at][]" size="30" type="text" value="2012-01-02"/>' +
- '<div class="separator"> - </div>' +
+ '<span class="separator date"> - </span>' +
'<input class="created_at date_filter to" id="report_created_at" multiple name="report[created_at][]" size="30" type="text" value="2012-01-01"/>'
)}
end

0 comments on commit 38aabed

Please sign in to comment.
Something went wrong with that request. Please try again.