simple_date_scopes 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -17,17 +17,17 @@ And then execute:
17
17
 
18
18
  $ bundle
19
19
 
20
- If you want to test that your models are using it properly, you can do:
21
-
22
- 1. include it in spec_helper.rb
20
+ Your models can now do:
23
21
  ```ruby
24
- require 'date_scopes/spec_support'
22
+ class Widget < ActiveRecord::Base
23
+ acts_as_date_scopes
24
+ end
25
25
  ```
26
26
 
27
- 2. and inside your model_spec.rb
27
+ Or alternatively if you want to specify the field for the logic:
28
28
  ```ruby
29
- it_should_behave_like 'shared date scopes' do
30
- let(:kind) { MyModelName }
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
- We can now also take a field to use instead of :created\_at:
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
- Widget.last_month(:start_production_time).limit(4).each do |w|
68
- puts w.to_s
69
- end
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 SimpleDateScopes
3
+ module ActiveRecord
4
+ module Acts
5
+ module SimpleDateScopes
4
6
 
5
- def self.included(base_class)
7
+ DEFAULT_FIELD = :created_at
6
8
 
7
- base_class.extend(ClassMethods)
9
+ def self.included(base_class)
10
+ base_class.extend(ClassMethods)
11
+ end
8
12
 
9
- base_class.class_eval do
13
+ module ClassMethods
10
14
 
11
- base_class.extend(InstanceMethods)
15
+ def acts_as_date_scopes_on(field = DEFAULT_FIELD)
12
16
 
13
- scope :in_week_of, ->(d,field = 'created_at') { in_x_of(d, field, :week) }
14
- scope :last_week, ->(f = 'created_at') { in_week_of(Date.today - 7.days, f) }
15
- scope :this_week, ->(f = 'created_at') { in_week_of(Date.today, f) }
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
- scope :in_month_of, ->(d,field = 'created_at') { in_x_of(d, field, :month) }
19
- scope :last_month, ->(f = 'created_at') { in_month_of(Date.today - 1.months, f) }
20
- scope :this_month, ->(f = 'created_at') { in_month_of(Date.today, f) }
21
- scope :next_month, ->(f = 'created_at') { in_month_of(Date.today + 1.months, f) }
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
- scope :in_year_of, ->(d,field = 'created_at') { in_x_of(d, field, :year) }
24
- scope :last_year, ->(f = 'created_at') { in_year_of(Date.today - 1.years, f) }
25
- scope :this_year, ->(f = 'created_at') { in_year_of(Date.today, f) }
26
- scope :next_year, ->(f = 'created_at') { in_year_of(Date.today + 1.years, f) }
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
- end
29
- end
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
- module ClassMethods
32
- private
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
- module InstanceMethods
41
- end
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 :created_at, Time.now - 1.years
20
- @new_item.update_attribute :created_at, Time.now + 1.years
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 :created_at, Time.now - 1.months
28
- @new_item.update_attribute :created_at, Time.now + 1.months
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 :created_at, Time.now - 1.weeks
36
- @new_item.update_attribute :created_at, Time.now + 1.weeks
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 :created_at, date
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,3 +1,3 @@
1
1
  module SimpleDateScopes
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,13 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class Widget < ActiveRecord::Base
4
- include ::SimpleDateScopes
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}
@@ -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
@@ -1,5 +1,6 @@
1
1
  FactoryGirl.define do
2
2
 
3
3
  factory :widget do ; end
4
+ factory :default_widget do ; end
4
5
 
5
6
  end
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.1.1
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-25 00:00:00.000000000 Z
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: 2204942028803521934
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: 2204942028803521934
172
+ hash: 4116473862507397523
173
173
  requirements: []
174
174
  rubyforge_project:
175
175
  rubygems_version: 1.8.23