simple_date_scopes 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -14
- data/lib/simple_date_scopes/simple_date_scopes.rb +49 -29
- data/lib/simple_date_scopes/spec_support.rb +43 -11
- data/lib/simple_date_scopes/version.rb +1 -1
- data/spec/lib/shared_date_scopes_spec.rb +11 -1
- data/spec/spec_helper.rb +4 -1
- data/spec/support/database.rb +3 -0
- data/spec/support/factories.rb +1 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -17,17 +17,17 @@ And then execute:
|
|
17
17
|
|
18
18
|
$ bundle
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
1. include it in spec_helper.rb
|
20
|
+
Your models can now do:
|
23
21
|
```ruby
|
24
|
-
|
22
|
+
class Widget < ActiveRecord::Base
|
23
|
+
acts_as_date_scopes
|
24
|
+
end
|
25
25
|
```
|
26
26
|
|
27
|
-
|
27
|
+
Or alternatively if you want to specify the field for the logic:
|
28
28
|
```ruby
|
29
|
-
|
30
|
-
|
29
|
+
class OtherWidget < ActiveRecord::Base
|
30
|
+
acts_as_date_scopes_on :produced_at
|
31
31
|
end
|
32
32
|
```
|
33
33
|
|
@@ -41,9 +41,9 @@ Your ActiveRecord based models will now have the following automatic scopes:
|
|
41
41
|
|
42
42
|
There are also utility scopes:
|
43
43
|
|
44
|
-
* in\_year\_of
|
45
|
-
* in\_month\_of
|
46
|
-
* in\_week\_of
|
44
|
+
* in\_year\_of [date]
|
45
|
+
* in\_month\_of [date]
|
46
|
+
* in\_week\_of [date]
|
47
47
|
|
48
48
|
This means the following will work:
|
49
49
|
|
@@ -61,16 +61,26 @@ This means the following will work:
|
|
61
61
|
end
|
62
62
|
```
|
63
63
|
|
64
|
-
|
64
|
+
If you want to test that your models are using it properly, you can do:
|
65
65
|
|
66
|
+
1. include it in spec\_helper.rb
|
66
67
|
```ruby
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
require 'date_scopes/spec_support'
|
69
|
+
```
|
70
|
+
|
71
|
+
2. and inside your model\_spec.rb
|
72
|
+
```ruby
|
73
|
+
it_should_behave_like 'shared date scopes' do
|
74
|
+
let(:kind) { MyModelName }
|
75
|
+
let(:field) { :created_at }
|
76
|
+
end
|
70
77
|
```
|
71
78
|
|
72
79
|
# Changes
|
73
80
|
|
81
|
+
## 0.2.0
|
82
|
+
* Use class methods instead so we can just specify the data field once
|
83
|
+
|
74
84
|
## 0.1.1
|
75
85
|
* Refactoring to use a common private method instead of 3 almost identical methods
|
76
86
|
|
@@ -1,43 +1,63 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
|
3
|
-
module
|
3
|
+
module ActiveRecord
|
4
|
+
module Acts
|
5
|
+
module SimpleDateScopes
|
4
6
|
|
5
|
-
|
7
|
+
DEFAULT_FIELD = :created_at
|
6
8
|
|
7
|
-
|
9
|
+
def self.included(base_class)
|
10
|
+
base_class.extend(ClassMethods)
|
11
|
+
end
|
8
12
|
|
9
|
-
|
13
|
+
module ClassMethods
|
10
14
|
|
11
|
-
|
15
|
+
def acts_as_date_scopes_on(field = DEFAULT_FIELD)
|
12
16
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
scope :next_week, ->(f = 'created_at') { in_week_of(Date.today + 7.days, f) }
|
17
|
+
scope :yesterday, -> { in_x_of(Date.today - 1.day, field, :day) }
|
18
|
+
scope :today, -> { in_x_of(Date.today, field, :day) }
|
19
|
+
scope :tomorrow, -> { in_x_of(Date.today + 1.day, field, :day) }
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
scope :in_week_of, ->(date) { in_x_of(date, field, :week) }
|
22
|
+
scope :last_week, -> { in_week_of(Date.today - 7.days) }
|
23
|
+
scope :this_week, -> { in_week_of(Date.today) }
|
24
|
+
scope :next_week, -> { in_week_of(Date.today + 7.days) }
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
scope :in_month_of, ->(date) { in_x_of(date, field, :month) }
|
27
|
+
scope :last_month, -> { in_month_of(Date.today - 1.months) }
|
28
|
+
scope :this_month, -> { in_month_of(Date.today) }
|
29
|
+
scope :next_month, -> { in_month_of(Date.today + 1.months) }
|
27
30
|
|
28
|
-
|
29
|
-
|
31
|
+
scope :in_year_of, ->(date) { in_x_of(date, field, :year) }
|
32
|
+
scope :last_year, -> { in_year_of(Date.today - 1.years) }
|
33
|
+
scope :this_year, -> { in_year_of(Date.today) }
|
34
|
+
scope :next_year, -> { in_year_of(Date.today + 1.years) }
|
30
35
|
|
31
|
-
|
32
|
-
|
33
|
-
def in_x_of(date, field, method)
|
34
|
-
where("#{field} BETWEEN ? AND ?",
|
35
|
-
date.send("beginning_of_#{method.to_s}".to_sym).beginning_of_day,
|
36
|
-
date.send("end_of_#{method.to_s}".to_sym).end_of_day)
|
37
|
-
end
|
38
|
-
end
|
36
|
+
# extend SimpleDateScopes::SingletonMethods
|
37
|
+
# include SimpleDateScopes::InstanceMethods
|
39
38
|
|
40
|
-
|
41
|
-
|
39
|
+
end
|
40
|
+
|
41
|
+
def acts_as_date_scopes
|
42
|
+
acts_as_date_scopes_on DEFAULT_FIELD
|
43
|
+
end
|
42
44
|
|
45
|
+
private
|
46
|
+
def in_x_of(date, field, method)
|
47
|
+
where("#{field.to_s} BETWEEN ? AND ?",
|
48
|
+
date.send("beginning_of_#{method.to_s}".to_sym).beginning_of_day,
|
49
|
+
date.send("end_of_#{method.to_s}".to_sym).end_of_day)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# module InstanceMethods
|
54
|
+
# end
|
55
|
+
|
56
|
+
# module SingletonMethods
|
57
|
+
# end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
43
61
|
end
|
62
|
+
|
63
|
+
ActiveRecord::Base.send(:include, ActiveRecord::Acts::SimpleDateScopes)
|
@@ -1,9 +1,5 @@
|
|
1
1
|
shared_examples_for 'simple date scopes' do
|
2
2
|
|
3
|
-
def debug_kind(kind)
|
4
|
-
kind.all.each { |i| p "#{i.id} #{i.created_at}" }
|
5
|
-
end
|
6
|
-
|
7
3
|
before :each do
|
8
4
|
@count = 4
|
9
5
|
@count.times { FactoryGirl.create kind.name.underscore.to_sym }
|
@@ -16,35 +12,43 @@ shared_examples_for 'simple date scopes' do
|
|
16
12
|
end
|
17
13
|
|
18
14
|
it 'should support last_year, this_year and next_year' do
|
19
|
-
@old_item.update_attribute
|
20
|
-
@new_item.update_attribute
|
15
|
+
@old_item.update_attribute field, Time.now - 1.years
|
16
|
+
@new_item.update_attribute field, Time.now + 1.years
|
21
17
|
kind.last_year.count.should eq(1)
|
22
18
|
kind.this_year.count.should eq(2)
|
23
19
|
kind.next_year.count.should eq(1)
|
24
20
|
end
|
25
21
|
|
26
22
|
it 'should support last_month, this_month and next_month' do
|
27
|
-
@old_item.update_attribute
|
28
|
-
@new_item.update_attribute
|
23
|
+
@old_item.update_attribute field, Time.now - 1.months
|
24
|
+
@new_item.update_attribute field, Time.now + 1.months
|
29
25
|
kind.last_month.count.should eq(1)
|
30
26
|
kind.this_month.count.should eq(2)
|
31
27
|
kind.next_month.count.should eq(1)
|
32
28
|
end
|
33
29
|
|
34
30
|
it 'should support last_week, this_week and next_week' do
|
35
|
-
@old_item.update_attribute
|
36
|
-
@new_item.update_attribute
|
31
|
+
@old_item.update_attribute field, Time.now - 1.weeks
|
32
|
+
@new_item.update_attribute field, Time.now + 1.weeks
|
37
33
|
kind.last_week.count.should eq(1)
|
38
34
|
kind.this_week.count.should eq(2)
|
39
35
|
kind.next_week.count.should eq(1)
|
40
36
|
end
|
41
37
|
|
38
|
+
it 'should support yesterday, today and tomorrow' do
|
39
|
+
@old_item.update_attribute field, Time.now - 1.day
|
40
|
+
@new_item.update_attribute field, Time.now + 1.day
|
41
|
+
kind.yesterday.count.should eq(1)
|
42
|
+
kind.today.count.should eq(2)
|
43
|
+
kind.tomorrow.count.should eq(1)
|
44
|
+
end
|
45
|
+
|
42
46
|
describe 'in_(week|month|year)_of' do
|
43
47
|
|
44
48
|
let(:date) { Date.today - 3.years }
|
45
49
|
|
46
50
|
before :each do
|
47
|
-
@old_item.update_attribute
|
51
|
+
@old_item.update_attribute field, date
|
48
52
|
end
|
49
53
|
|
50
54
|
it 'should support in_year_of' do
|
@@ -61,6 +65,34 @@ shared_examples_for 'simple date scopes' do
|
|
61
65
|
|
62
66
|
end
|
63
67
|
|
68
|
+
describe 'handling days' do
|
69
|
+
|
70
|
+
it 'should work in the past' do
|
71
|
+
Timecop.travel(Date.today.beginning_of_day - 1.day) do
|
72
|
+
kind.yesterday.count.should eq(0)
|
73
|
+
kind.today.count.should eq(0)
|
74
|
+
kind.tomorrow.count.should eq(@count)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should work currently' do
|
79
|
+
Timecop.travel(Date.today.beginning_of_day) do
|
80
|
+
kind.yesterday.count.should eq(0)
|
81
|
+
kind.today.count.should eq(@count)
|
82
|
+
kind.tomorrow.count.should eq(0)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should work in the future' do
|
87
|
+
Timecop.travel(Date.today.beginning_of_day + 1.day) do
|
88
|
+
kind.yesterday.count.should eq(@count)
|
89
|
+
kind.today.count.should eq(0)
|
90
|
+
kind.tomorrow.count.should eq(0)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
64
96
|
describe 'handling weeks' do
|
65
97
|
|
66
98
|
it 'should work in the past' do
|
@@ -1,13 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
class Widget < ActiveRecord::Base
|
4
|
-
|
4
|
+
acts_as_date_scopes_on :updated_at
|
5
|
+
end
|
6
|
+
|
7
|
+
class DefaultWidget < ActiveRecord::Base
|
8
|
+
acts_as_date_scopes
|
5
9
|
end
|
6
10
|
|
7
11
|
describe 'Simple Date Scopes' do
|
8
12
|
|
9
13
|
it_should_behave_like 'simple date scopes' do
|
10
14
|
let(:kind) { Widget }
|
15
|
+
let(:field) { :updated_at }
|
16
|
+
end
|
17
|
+
|
18
|
+
it_should_behave_like 'simple date scopes' do
|
19
|
+
let(:kind) { DefaultWidget }
|
20
|
+
let(:field) { :created_at }
|
11
21
|
end
|
12
22
|
|
13
23
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,7 +4,10 @@ require 'factory_girl'
|
|
4
4
|
require 'timecop'
|
5
5
|
|
6
6
|
require 'simplecov'
|
7
|
-
SimpleCov.start
|
7
|
+
SimpleCov.start do
|
8
|
+
add_filter '/spec/support/'
|
9
|
+
add_filter 'spec_support.rb'
|
10
|
+
end
|
8
11
|
|
9
12
|
require 'simple_date_scopes'
|
10
13
|
Dir[File.join(%w[ . spec support ** *.rb])].each {|f| require f}
|
data/spec/support/database.rb
CHANGED
@@ -7,6 +7,9 @@ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
|
7
7
|
ActiveRecord::Migration.create_table :widgets do |t|
|
8
8
|
t.timestamps
|
9
9
|
end
|
10
|
+
ActiveRecord::Migration.create_table :default_widgets do |t|
|
11
|
+
t.timestamps
|
12
|
+
end
|
10
13
|
|
11
14
|
# add missing helpers
|
12
15
|
module ActiveModel::Validations
|
data/spec/support/factories.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_date_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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: 2012-09
|
12
|
+
date: 2012-10-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -160,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: '0'
|
161
161
|
segments:
|
162
162
|
- 0
|
163
|
-
hash:
|
163
|
+
hash: 4116473862507397523
|
164
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
165
|
none: false
|
166
166
|
requirements:
|
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
169
|
version: '0'
|
170
170
|
segments:
|
171
171
|
- 0
|
172
|
-
hash:
|
172
|
+
hash: 4116473862507397523
|
173
173
|
requirements: []
|
174
174
|
rubyforge_project:
|
175
175
|
rubygems_version: 1.8.23
|