Permalink
Browse files

Fix usage of date format for date filter without range. #90

  • Loading branch information...
bogdan committed Mar 2, 2014
1 parent ee71528 commit 570c956d19a3349dd49b975ebf5c489cc06978b0
Showing with 39 additions and 12 deletions.
  1. +8 −3 lib/datagrid/filters.rb
  2. +20 −9 spec/datagrid/filters/date_filter_spec.rb
  3. +11 −0 spec/datagrid/form_builder_spec.rb
View
@@ -48,6 +48,7 @@ module ClassMethods
# Returns filter definition object by name
def filter_by_name(attribute)
+ return attribute if attribute.is_a?(Datagrid::Filters::BaseFilter)
self.filters.find do |filter|
filter.name.to_sym == attribute.to_sym
end
@@ -133,10 +134,14 @@ def filter_value(filter)
end
# Returns string representation of filter value
- def filter_value_as_string(filter)
+ def filter_value_as_string(name)
+ filter = filter_by_name(name)
value = filter_value(filter)
- value = value.is_a?(Array) ? value.join(filter.separator) : value.to_s
- value.blank? ? nil : value
+ if value.is_a?(Array)
+ value.map {|v| filter.format(v) }.join(filter.separator)
+ else
+ filter.format(value)
+ end
end
# Returns filter object with the given name
@@ -7,7 +7,7 @@
e2 = Entry.create!(:created_at => 4.days.ago)
e3 = Entry.create!(:created_at => 1.day.ago)
report = test_report(:created_at => 5.day.ago..3.days.ago) do
- scope { Entry }
+ scope { Entry }
filter(:created_at, :date)
end
report.assets.should_not include(e1)
@@ -21,7 +21,7 @@
let(:klass) { klass }
subject do
test_report(:created_at => _created_at) do
- scope { klass }
+ scope { klass }
filter(:created_at, :date, :range => true)
end.assets.to_a
end
@@ -57,7 +57,7 @@ def entry_dated(date)
e2 = Entry.create!(:created_at => 4.days.ago)
e3 = Entry.create!(:created_at => 1.day.ago)
report = test_report(:created_at => [5.day.ago.to_date.to_s, 3.days.ago.to_date.to_s]) do
- scope { Entry }
+ scope { Entry }
filter(:created_at, :date, :range => true)
end
report.assets.should_not include(e1)
@@ -70,7 +70,7 @@ def entry_dated(date)
e2 = Entry.create!(:created_at => 4.days.ago)
e3 = Entry.create!(:created_at => 1.day.ago)
report = test_report(:created_at => [5.day.ago.to_date.to_s, nil]) do
- scope { Entry }
+ scope { Entry }
filter(:created_at, :date, :range => true)
end
report.assets.should_not include(e1)
@@ -83,7 +83,7 @@ def entry_dated(date)
e2 = Entry.create!(:created_at => 4.days.ago)
e3 = Entry.create!(:created_at => 1.day.ago)
report = test_report(:created_at => [nil, 3.days.ago.to_date.to_s]) do
- scope { Entry }
+ scope { Entry }
filter(:created_at, :date, :range => true)
end
report.assets.should include(e1)
@@ -97,7 +97,7 @@ def entry_dated(date)
e2 = Entry.create!(:created_at => 4.days.ago)
e3 = Entry.create!(:created_at => 1.day.ago)
report = test_report(:created_at => (4.days.ago.to_date..4.days.ago.to_date)) do
- scope { Entry }
+ scope { Entry }
filter(:created_at, :date, :range => true)
end
report.assets.should_not include(e1)
@@ -110,7 +110,7 @@ def entry_dated(date)
e2 = Entry.create!(:created_at => 4.days.ago)
e3 = Entry.create!(:created_at => 1.day.ago)
report = test_report(:created_at => (1.days.ago.to_date..7.days.ago.to_date)) do
- scope { Entry }
+ scope { Entry }
filter(:created_at, :date, :range => true)
end
report.assets.should_not include(e1)
@@ -121,15 +121,15 @@ def entry_dated(date)
it "should support block" do
report = test_report(:created_at => Date.today) do
- scope { Entry }
+ scope { Entry }
filter(:created_at, :date, :range => true) do |value|
where("created_at >= ?", value)
end
end
report.assets.should_not include(Entry.create!(:created_at => 1.day.ago))
report.assets.should include(Entry.create!(:created_at => DateTime.now))
end
-
+
context "when date format is configured" do
around(:each) do |example|
@@ -170,4 +170,15 @@ def entry_dated(date)
end
report.created_at.should == [Date.new(2012, 01, 01), Date.new(2013, 01, 01)]
end
+
+ it "should properly format date in filter_value_as_string" do
+ with_date_format do
+ report = test_report(:created_at => "2012-01-02") do
+ scope {Entry}
+ filter(:created_at, :date)
+ end
+ report.filter_value_as_string(:created_at).should == "01/02/2012"
+ end
+ end
+
end
@@ -63,6 +63,17 @@ class MyTemplate
it { should equal_to_dom(
'<input class="created_at date_filter" id="report_created_at" name="report[created_at]" size="30" type="text"/>'
)}
+ context "when special date format specified" do
+ around(:each) do |example|
+ _grid.created_at = Date.parse('2012-01-02')
+ with_date_format do
+ example.run
+ end
+ end
+ it { should equal_to_dom(
+ '<input class="created_at date_filter" id="report_created_at" name="report[created_at]" size="30" type="text" value="01/02/2012"/>'
+ )}
+ end
end
context "with integer filter type and range option" do

0 comments on commit 570c956

Please sign in to comment.