simple_date_scopes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --no-drb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in date_scopes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Peter Hoeg
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # DateScopes
2
+
3
+ A super tiny gem that provides basic year, month and week scopes.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/speartail/date_scopes.png)](http://travis-ci.org/speartail/date_scopes)
6
+
7
+ Something like this already existed in various forms but I simply wanted to do
8
+ my own thing instead.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'date_scopes'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
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
23
+ ```ruby
24
+ require 'date_scopes/spec_support'
25
+ ```
26
+
27
+ 2. and inside your model_spec.rb
28
+ ```ruby
29
+ it_should_behave_like 'shared date scopes' do
30
+ let(:kind) { MyModelName }
31
+ end
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ Your ActiveRecord based models will now have the following automatic scopes:
37
+
38
+ * last\_year, this\_year, next\_year
39
+ * last\_month, this\_month, next\_month
40
+ * last\_week, this\_week, next\_week
41
+
42
+ There are also utility scopes:
43
+
44
+ * in\_year\_of
45
+ * in\_month\_of
46
+ * in\_week\_of
47
+
48
+ This means the following will work:
49
+
50
+ ```ruby
51
+ Widget.this_month.each do |w|
52
+ puts w.to_s
53
+ end
54
+
55
+ Widget.last_month.limit(4).each do |w|
56
+ puts w.to_s
57
+ end
58
+
59
+ Widget.in_month_of(Date.new(2012, 2, 7)).each do |w|
60
+ puts w.to_s
61
+ end
62
+ ```
63
+
64
+ We can now also take a field to use instead of :created\_at:
65
+
66
+ ```ruby
67
+ Widget.last_month(:start_production_time).limit(4).each do |w|
68
+ puts w.to_s
69
+ end
70
+ ```
71
+
72
+ # Changes
73
+
74
+ ## 0.1.0
75
+ * Renamed to simple\_date\_scopes so we can push to rubygems
76
+
77
+ ## 0.0.5-0.0.7
78
+ * Cleanups
79
+
80
+ ## 0.0.4
81
+ * Fixed a nasty bug that made the scopes ignore records created on the current date
82
+ * Make the specs run properly when included
83
+ * Added support for specifying another field instead of :created\_at
84
+
85
+ ## 0.0.2/3
86
+ * More specs
87
+
88
+ ## 0.0.1
89
+ * Initial version
90
+
91
+ ## Contributing
92
+
93
+ 1. Fork it
94
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
95
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
96
+ 4. Push to the branch (`git push origin my-new-feature`)
97
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ require 'simple_date_scopes/version'
2
+ require 'simple_date_scopes/simple_date_scopes'
3
+
4
+ module SimpleDateScopes
5
+ end
@@ -0,0 +1,46 @@
1
+ require 'active_record'
2
+
3
+ module SimpleDateScopes
4
+
5
+ def self.included(base_class)
6
+
7
+ base_class.extend(ClassMethods)
8
+
9
+ base_class.class_eval do
10
+
11
+ base_class.extend(InstanceMethods)
12
+
13
+ scope :in_week_of, ->(d,field = 'created_at') { where("#{field} BETWEEN ? AND ?",
14
+ d.beginning_of_week.beginning_of_day,
15
+ d.end_of_week.end_of_day ) }
16
+ scope :last_week, ->(f = 'created_at') { in_week_of(Date.today - 7.days, f) }
17
+ scope :this_week, ->(f = 'created_at') { in_week_of(Date.today, f) }
18
+ scope :next_week, ->(f = 'created_at') { in_week_of(Date.today + 7.days, f) }
19
+
20
+ scope :in_month_of, ->(d,field = 'created_at') { where("#{field} BETWEEN ? AND ?",
21
+ d.beginning_of_month.beginning_of_day,
22
+ d.end_of_month.end_of_day ) }
23
+ scope :last_month, ->(f = 'created_at') { in_month_of(Date.today - 1.months, f) }
24
+ scope :this_month, ->(f = 'created_at') { in_month_of(Date.today, f) }
25
+ scope :next_month, ->(f = 'created_at') { in_month_of(Date.today + 1.months, f) }
26
+
27
+ scope :in_year_of, ->(d,field = 'created_at') { where("#{field} BETWEEN ? AND ?",
28
+ d.beginning_of_year.beginning_of_day,
29
+ d.end_of_year.end_of_day ) }
30
+ scope :last_year, ->(f = 'created_at') { in_year_of(Date.today - 1.years, f) }
31
+ scope :this_year, ->(f = 'created_at') { in_year_of(Date.today, f) }
32
+ scope :next_year, ->(f = 'created_at') { in_year_of(Date.today + 1.years, f) }
33
+
34
+ private
35
+ def in_x_of(date, field, method)
36
+ end
37
+ end
38
+ end
39
+
40
+ module ClassMethods
41
+ end
42
+
43
+ module InstanceMethods
44
+ end
45
+
46
+ end
@@ -0,0 +1,147 @@
1
+ shared_examples_for 'simple date scopes' do
2
+
3
+ def debug_kind(kind)
4
+ kind.all.each { |i| p "#{i.id} #{i.created_at}" }
5
+ end
6
+
7
+ before :each do
8
+ @count = 4
9
+ @count.times { FactoryGirl.create kind.name.underscore.to_sym }
10
+ @old_item = kind.first
11
+ @new_item = kind.last
12
+ end
13
+
14
+ it "should have 4 items" do
15
+ kind.count.should eq(4)
16
+ end
17
+
18
+ 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
21
+ kind.last_year.count.should eq(1)
22
+ kind.this_year.count.should eq(2)
23
+ kind.next_year.count.should eq(1)
24
+ end
25
+
26
+ 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
29
+ kind.last_month.count.should eq(1)
30
+ kind.this_month.count.should eq(2)
31
+ kind.next_month.count.should eq(1)
32
+ end
33
+
34
+ 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
37
+ kind.last_week.count.should eq(1)
38
+ kind.this_week.count.should eq(2)
39
+ kind.next_week.count.should eq(1)
40
+ end
41
+
42
+ describe 'in_(week|month|year)_of' do
43
+
44
+ let(:date) { Date.today - 3.years }
45
+
46
+ before :each do
47
+ @old_item.update_attribute :created_at, date
48
+ end
49
+
50
+ it 'should support in_year_of' do
51
+ kind.in_year_of(date).length.should eq(1)
52
+ end
53
+
54
+ it 'should support in_month_of' do
55
+ kind.in_month_of(date).length.should eq(1)
56
+ end
57
+
58
+ it 'should support in_week_of' do
59
+ kind.in_week_of(date).length.should eq(1)
60
+ end
61
+
62
+ end
63
+
64
+ describe 'handling weeks' do
65
+
66
+ it 'should work in the past' do
67
+ Timecop.travel(Date.today.beginning_of_week - 1.weeks) do
68
+ kind.last_week.count.should eq(0)
69
+ kind.this_week.count.should eq(0)
70
+ kind.next_week.count.should eq(@count)
71
+ end
72
+ end
73
+
74
+ it 'should work currently' do
75
+ Timecop.travel(Date.today.beginning_of_week) do
76
+ kind.last_week.count.should eq(0)
77
+ kind.this_week.count.should eq(@count)
78
+ kind.next_week.count.should eq(0)
79
+ end
80
+ end
81
+
82
+ it 'should work in the future' do
83
+ Timecop.travel(Date.today.beginning_of_week + 1.weeks) do
84
+ kind.last_week.count.should eq(@count)
85
+ kind.this_week.count.should eq(0)
86
+ kind.next_week.count.should eq(0)
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ describe 'handling months' do
93
+
94
+ it 'should work in the past' do
95
+ Timecop.travel(Date.today.beginning_of_month - 1.months) do
96
+ kind.last_month.count.should eq(0)
97
+ kind.this_month.count.should eq(0)
98
+ kind.next_month.count.should eq(@count)
99
+ end
100
+ end
101
+
102
+ it 'should work currently' do
103
+ Timecop.travel(Date.today.beginning_of_month) do
104
+ kind.last_month.count.should eq(0)
105
+ kind.this_month.count.should eq(@count)
106
+ kind.next_month.count.should eq(0)
107
+ end
108
+ end
109
+
110
+ it 'should work in the future' do
111
+ Timecop.travel(Date.today.beginning_of_month + 1.months) do
112
+ kind.last_month.count.should eq(@count)
113
+ kind.this_month.count.should eq(0)
114
+ kind.next_month.count.should eq(0)
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ describe 'handling years' do
121
+
122
+ it 'should work in the past' do
123
+ Timecop.travel(Date.today.beginning_of_year - 1.years) do
124
+ kind.last_year.count.should eq(0)
125
+ kind.this_year.count.should eq(0)
126
+ kind.next_year.count.should eq(@count)
127
+ end
128
+ end
129
+
130
+ it 'should work currently' do
131
+ Timecop.travel(Date.today.beginning_of_year) do
132
+ kind.last_year.count.should eq(0)
133
+ kind.this_year.count.should eq(@count)
134
+ kind.next_year.count.should eq(0)
135
+ end
136
+ end
137
+
138
+ it 'should work in the future' do
139
+ Timecop.travel(Date.today.beginning_of_year + 1.years) do
140
+ kind.last_year.count.should eq(@count)
141
+ kind.this_year.count.should eq(0)
142
+ kind.next_year.count.should eq(0)
143
+ end
144
+ end
145
+
146
+ end
147
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleDateScopes
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_date_scopes/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "simple_date_scopes"
8
+ gem.version = SimpleDateScopes::VERSION
9
+ gem.authors = ["Peter Hoeg"]
10
+ gem.email = ["peter@speartail.com"]
11
+ gem.description = %q{A set of standard date scopes to handle years, months and weeks based on created_at or another field}
12
+ gem.summary = %q{A set of standard date scopes to handle years, months and weeks based on created_at or another field}
13
+ gem.homepage = "http://speartail.com"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'activerecord', '~> 3.0'
21
+
22
+ gem.add_development_dependency 'factory_girl'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'simplecov'
26
+ gem.add_development_dependency 'sqlite3'
27
+ gem.add_development_dependency 'timecop', '>= 0.5.1'
28
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ class Widget < ActiveRecord::Base
4
+ include ::SimpleDateScopes
5
+ end
6
+
7
+ describe 'Simple Date Scopes' do
8
+
9
+ it_should_behave_like 'simple date scopes' do
10
+ let(:kind) { Widget }
11
+ end
12
+
13
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'factory_girl'
4
+ require 'timecop'
5
+
6
+ require 'simplecov'
7
+ SimpleCov.start
8
+
9
+ require 'simple_date_scopes'
10
+ Dir[File.join(%w[ . spec support ** *.rb])].each {|f| require f}
11
+ require 'simple_date_scopes/spec_support'
12
+
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+
18
+ config.mock_with :rspec
19
+
20
+ # Run specs in random order to surface order dependencies. If you find an
21
+ # order dependency and want to debug it, you can fix the order by providing
22
+ # the seed, which is printed after each run.
23
+ # --seed 1234
24
+ config.order = 'random'
25
+
26
+ config.include FactoryGirl::Syntax::Methods
27
+
28
+ config.around do |example|
29
+ ActiveRecord::Base.transaction do
30
+ example.run
31
+ raise ActiveRecord::Rollback
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
4
+
5
+ # migrations
6
+ #ActiveRecord::Migrator.up "db/migrate"
7
+ ActiveRecord::Migration.create_table :widgets do |t|
8
+ t.timestamps
9
+ end
10
+
11
+ # add missing helpers
12
+ module ActiveModel::Validations
13
+ def errors_on(attribute)
14
+ self.valid?
15
+ [self.errors[attribute]].flatten.compact
16
+ end
17
+ alias :error_on :errors_on
18
+ end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+
3
+ factory :widget do ; end
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_date_scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Hoeg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: factory_girl
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: timecop
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.5.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.5.1
126
+ description: A set of standard date scopes to handle years, months and weeks based
127
+ on created_at or another field
128
+ email:
129
+ - peter@speartail.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - .rspec
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - lib/simple_date_scopes.rb
141
+ - lib/simple_date_scopes/simple_date_scopes.rb
142
+ - lib/simple_date_scopes/spec_support.rb
143
+ - lib/simple_date_scopes/version.rb
144
+ - simple_date_scopes.gemspec
145
+ - spec/lib/shared_date_scopes_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/support/database.rb
148
+ - spec/support/factories.rb
149
+ homepage: http://speartail.com
150
+ licenses: []
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ segments:
162
+ - 0
163
+ hash: -2259891919373961889
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ segments:
171
+ - 0
172
+ hash: -2259891919373961889
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 1.8.23
176
+ signing_key:
177
+ specification_version: 3
178
+ summary: A set of standard date scopes to handle years, months and weeks based on
179
+ created_at or another field
180
+ test_files:
181
+ - spec/lib/shared_date_scopes_spec.rb
182
+ - spec/spec_helper.rb
183
+ - spec/support/database.rb
184
+ - spec/support/factories.rb