datagrid 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.3
1
+ 0.8.4
data/datagrid.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "datagrid"
8
- s.version = "0.8.3"
8
+ s.version = "0.8.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bogdan Gusiev"]
12
- s.date = "2013-05-20"
12
+ s.date = "2013-05-22"
13
13
  s.description = "This allows you to easily build datagrid aka data tables with sortable columns and filters"
14
14
  s.email = "agresso@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -2,6 +2,8 @@ module Datagrid
2
2
  module Drivers
3
3
  class AbstractDriver
4
4
 
5
+ TIMESTAMP_CLASSES = [DateTime, Time, ActiveSupport::TimeWithZone]
6
+
5
7
  class_attribute :subclasses
6
8
 
7
9
  def self.inherited(base)
@@ -59,6 +61,15 @@ module Datagrid
59
61
  def reverse_order(scope)
60
62
  raise NotImplementedError
61
63
  end
64
+
65
+ def is_timestamp?(scope, field)
66
+ raise NotImplementedError
67
+ end
68
+
69
+ protected
70
+ def timestamp_class?(klass)
71
+ TIMESTAMP_CLASSES.include?(klass)
72
+ end
62
73
  end
63
74
  end
64
75
  end
@@ -45,11 +45,11 @@ module Datagrid
45
45
  end
46
46
 
47
47
  def greater_equal(scope, field, value)
48
- scope.where(["#{scope.table_name}.#{field} >= ?", value])
48
+ scope.where(["#{prefix_table_name(scope, field)} >= ?", value])
49
49
  end
50
50
 
51
51
  def less_equal(scope, field, value)
52
- scope.where(["#{scope.table_name}.#{field} <= ?", value])
52
+ scope.where(["#{prefix_table_name(scope, field)} <= ?", value])
53
53
  end
54
54
 
55
55
  def has_column?(scope, column_name)
@@ -57,6 +57,16 @@ module Datagrid
57
57
  rescue ::ActiveRecord::StatementInvalid
58
58
  false
59
59
  end
60
+
61
+ def is_timestamp?(scope, field)
62
+ has_column?(scope, field) && scope.columns_hash[field.to_s].type == :datetime
63
+ end
64
+
65
+ protected
66
+
67
+ def prefix_table_name(scope, field)
68
+ has_column?(scope, field) ? [scope.table_name, field].join(".") : field
69
+ end
60
70
  end
61
71
  end
62
72
  end
@@ -45,7 +45,12 @@ module Datagrid
45
45
  end
46
46
 
47
47
  def has_column?(scope, column_name)
48
- scope.first.respond_to?(column_name)
48
+ scope.any? && scope.first.respond_to?(column_name)
49
+ end
50
+
51
+ def is_timestamp?(scope, column_name)
52
+ has_column?(scope, column_name) &&
53
+ timestamp_class?(scope.first.send(column_name).class)
49
54
  end
50
55
  end
51
56
  end
@@ -42,6 +42,11 @@ module Datagrid
42
42
  def has_column?(scope, column_name)
43
43
  scope.key?(column_name)
44
44
  end
45
+
46
+ def is_timestamp?(scope, column_name)
47
+ #TODO implement the support
48
+ false
49
+ end
45
50
  end
46
51
  end
47
52
  end
@@ -16,6 +16,9 @@ module Datagrid
16
16
  end
17
17
 
18
18
  def where(scope, attribute, value)
19
+ if value.is_a?(Range)
20
+ value = {"$gte" => value.first, "$lte" => value.last}
21
+ end
19
22
  scope.where(attribute => value)
20
23
  end
21
24
 
@@ -42,6 +45,11 @@ module Datagrid
42
45
  def has_column?(scope, column_name)
43
46
  to_scope(scope).klass.fields.keys.include?(column_name.to_s)
44
47
  end
48
+
49
+ def is_timestamp?(scope, column_name)
50
+ has_column?(scope, column_name) &&
51
+ timestamp_class?(to_scope(scope).klass.fields[column_name.to_s].type)
52
+ end
45
53
  end
46
54
  end
47
55
  end
@@ -14,10 +14,12 @@ class Datagrid::Filters::DateFilter < Datagrid::Filters::BaseFilter
14
14
  def parse(value)
15
15
  return nil if value.blank?
16
16
  return value if value.is_a?(Range)
17
- formats.each do |format|
18
- begin
19
- return Date.strptime(value, format)
20
- rescue ArgumentError
17
+ if value.is_a?(String)
18
+ formats.each do |format|
19
+ begin
20
+ return Date.strptime(value, format)
21
+ rescue ArgumentError
22
+ end
21
23
  end
22
24
  end
23
25
  return value.to_date if value.respond_to?(:to_date)
@@ -38,5 +40,26 @@ class Datagrid::Filters::DateFilter < Datagrid::Filters::BaseFilter
38
40
  super
39
41
  end
40
42
  end
43
+
44
+ def default_filter_where(driver, scope, value)
45
+ if driver.is_timestamp?(scope, name)
46
+ value = format_value_timestamp(value)
47
+ end
48
+ super(driver, scope, value)
49
+ end
50
+
51
+ protected
52
+ def format_value_timestamp(value)
53
+ if !value
54
+ value
55
+ elsif (range? && value.is_a?(Array))
56
+ [value.first.try(:beginning_of_day), value.last.try(:end_of_day)]
57
+ elsif value.is_a?(Range)
58
+ (value.first.beginning_of_day..value.last.end_of_day)
59
+ else
60
+ value.beginning_of_day..value.end_of_day
61
+ end
62
+ end
63
+
41
64
  end
42
65
 
@@ -15,6 +15,43 @@ describe Datagrid::Filters::DateFilter do
15
15
  report.assets.should_not include(e3)
16
16
  end
17
17
 
18
+ {:active_record => Entry, :mongoid => MongoidEntry}.each do |orm, klass|
19
+ describe "with orm #{orm}" do
20
+ describe "date to timestamp conversion" do
21
+ let(:klass) { klass }
22
+ subject do
23
+ test_report(:created_at => _created_at) do
24
+ scope { klass }
25
+ filter(:created_at, :date, :range => true)
26
+ end.assets.to_a
27
+ end
28
+
29
+ def entry_dated(date)
30
+ klass.create(:created_at => date)
31
+ end
32
+
33
+ context "when single date paramter given" do
34
+ let(:_created_at) { Date.today }
35
+ it { should include(entry_dated(1.second.ago))}
36
+ it { should include(entry_dated(Date.today.end_of_day))}
37
+ it { should_not include(entry_dated(Date.today.beginning_of_day - 1.second))}
38
+ it { should_not include(entry_dated(Date.today.end_of_day + 1.second))}
39
+ end
40
+
41
+ context "when range date range given" do
42
+ let(:_created_at) { [Date.yesterday, Date.today] }
43
+ it { should include(entry_dated(1.second.ago))}
44
+ it { should include(entry_dated(1.day.ago))}
45
+ it { should include(entry_dated(Date.today.end_of_day))}
46
+ it { should include(entry_dated(Date.yesterday.beginning_of_day))}
47
+ it { should_not include(entry_dated(Date.yesterday.beginning_of_day - 1.second))}
48
+ it { should_not include(entry_dated(Date.today.end_of_day + 1.second))}
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+
18
55
  it "should support date range given as array argument" do
19
56
  e1 = Entry.create!(:created_at => 7.days.ago)
20
57
  e2 = Entry.create!(:created_at => 4.days.ago)
@@ -93,16 +130,39 @@ describe Datagrid::Filters::DateFilter do
93
130
  report.assets.should include(Entry.create!(:created_at => DateTime.now))
94
131
  end
95
132
 
96
- it "should have configurable date format" do
97
- with_date_format do
133
+
134
+ context "when date format is configured" do
135
+ around(:each) do |example|
136
+ with_date_format do
137
+ example.run
138
+ end
139
+ end
140
+
141
+ it "should have configurable date format" do
98
142
  report = test_report(:created_at => "10/01/2013") do
99
143
  scope {Entry}
100
144
  filter(:created_at, :date)
101
145
  end
102
146
  report.created_at.should == Date.new(2013,10,01)
103
147
  end
148
+
149
+ it "should support default explicit date" do
150
+ report = test_report(:created_at => Date.parse("2013-10-01")) do
151
+ scope {Entry}
152
+ filter(:created_at, :date)
153
+ end
154
+ report.created_at.should == Date.new(2013,10,01)
155
+ end
104
156
  end
105
157
 
158
+
159
+ it "should automatically reverse Array if first more than last" do
160
+ report = test_report(:created_at => ["2013-01-01", "2012-01-01"]) do
161
+ scope {Entry}
162
+ filter(:created_at, :date, :range => true)
163
+ end
164
+ report.created_at.should == [Date.new(2012, 01, 01), Date.new(2013, 01, 01)]
165
+ end
106
166
  it "should automatically reverse Array if first more than last" do
107
167
  report = test_report(:created_at => ["2013-01-01", "2012-01-01"]) do
108
168
  scope {Entry}
@@ -92,5 +92,16 @@ describe Datagrid::Filters::IntegerFilter do
92
92
  report.assets.should_not include(Entry.create!(:group_id => 1))
93
93
  report.assets.should include(Entry.create!(:group_id => 5))
94
94
  end
95
+
96
+
97
+ it "should not prefix table name if column is joined" do
98
+ report = test_report(:rating => [4,nil]) do
99
+ scope { Entry.joins(:group) }
100
+ filter(:rating, :integer, :range => true)
101
+ end
102
+ report.assets.should_not include(Entry.create!(:group => Group.create!(:rating => 3)))
103
+ report.assets.should include(Entry.create!(:group => Group.create!(:rating => 5)))
104
+ end
105
+
95
106
 
96
107
  end
@@ -1,12 +1,13 @@
1
1
  def with_date_format(format = "%m/%d/%Y")
2
2
  begin
3
+ old_format = Datagrid.configuration.date_formats
3
4
  Datagrid.configure do |config|
4
5
  config.date_formats = format
5
6
  end
6
7
  yield
7
8
  ensure
8
9
  Datagrid.configure do |config|
9
- config.date_formats = nil
10
+ config.date_formats = old_format
10
11
  end
11
12
  end
12
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagrid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-20 00:00:00.000000000 Z
12
+ date: 2013-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -335,7 +335,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
335
335
  version: '0'
336
336
  segments:
337
337
  - 0
338
- hash: -1396287359807292106
338
+ hash: -3876339943882620778
339
339
  required_rubygems_version: !ruby/object:Gem::Requirement
340
340
  none: false
341
341
  requirements: