by_star 2.0.0.beta1 → 2.1.0.beta2
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/.travis.yml +2 -0
- data/Gemfile.lock +42 -42
- data/README.markdown +78 -14
- data/by_star.gemspec +3 -2
- data/lib/by_star.rb +12 -4
- data/lib/by_star/by_day.rb +4 -0
- data/lib/by_star/by_fortnight.rb +4 -0
- data/lib/by_star/by_month.rb +24 -12
- data/lib/by_star/by_quarter.rb +32 -0
- data/lib/by_star/by_week.rb +5 -3
- data/lib/by_star/by_weekend.rb +3 -0
- data/lib/by_star/by_year.rb +5 -3
- data/lib/by_star/version.rb +1 -1
- data/lib/mongoid/by_star.rb +83 -0
- data/spec/by_star/active_record/active_record_spec.rb +50 -0
- data/spec/by_star/mongoid/mongoid_spec.rb +44 -0
- data/spec/by_star/shared/by_day.rb +62 -0
- data/spec/by_star/shared/by_direction.rb +85 -0
- data/spec/by_star/shared/by_fortnight.rb +47 -0
- data/spec/by_star/shared/by_month.rb +109 -0
- data/spec/by_star/shared/by_quarter.rb +33 -0
- data/spec/by_star/shared/by_week.rb +41 -0
- data/spec/by_star/shared/by_weekend.rb +13 -0
- data/spec/by_star/shared/by_year.rb +54 -0
- data/spec/fixtures/active_record/models.rb +13 -0
- data/spec/fixtures/{schema.rb → active_record/schema.rb} +0 -0
- data/spec/fixtures/mongoid/models.rb +65 -0
- data/spec/fixtures/{models.rb → shared/seeds.rb} +0 -16
- data/spec/spec_helper.rb +4 -11
- metadata +95 -31
- data/spec/by_star/by_day_spec.rb +0 -52
- data/spec/by_star/by_direction_spec.rb +0 -82
- data/spec/by_star/by_fortnight_spec.rb +0 -46
- data/spec/by_star/by_month_spec.rb +0 -60
- data/spec/by_star/by_week_spec.rb +0 -39
- data/spec/by_star/by_weekend_spec.rb +0 -12
- data/spec/by_star/by_year_spec.rb +0 -57
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,85 +1,85 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
by_star (2.0.0.
|
5
|
-
activerecord (
|
4
|
+
by_star (2.0.0.beta1)
|
5
|
+
activerecord (~> 3.0)
|
6
6
|
chronic
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: http://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actionpack (3.2.
|
12
|
-
activemodel (= 3.2.
|
13
|
-
activesupport (= 3.2.
|
11
|
+
actionpack (3.2.8)
|
12
|
+
activemodel (= 3.2.8)
|
13
|
+
activesupport (= 3.2.8)
|
14
14
|
builder (~> 3.0.0)
|
15
15
|
erubis (~> 2.7.0)
|
16
|
-
journey (~> 1.0.
|
16
|
+
journey (~> 1.0.4)
|
17
17
|
rack (~> 1.4.0)
|
18
|
-
rack-cache (~> 1.
|
18
|
+
rack-cache (~> 1.2)
|
19
19
|
rack-test (~> 0.6.1)
|
20
|
-
sprockets (~> 2.1.
|
21
|
-
activemodel (3.2.
|
22
|
-
activesupport (= 3.2.
|
20
|
+
sprockets (~> 2.1.3)
|
21
|
+
activemodel (3.2.8)
|
22
|
+
activesupport (= 3.2.8)
|
23
23
|
builder (~> 3.0.0)
|
24
|
-
activerecord (3.2.
|
25
|
-
activemodel (= 3.2.
|
26
|
-
activesupport (= 3.2.
|
27
|
-
arel (~> 3.0.
|
24
|
+
activerecord (3.2.8)
|
25
|
+
activemodel (= 3.2.8)
|
26
|
+
activesupport (= 3.2.8)
|
27
|
+
arel (~> 3.0.2)
|
28
28
|
tzinfo (~> 0.3.29)
|
29
|
-
activesupport (3.2.
|
29
|
+
activesupport (3.2.8)
|
30
30
|
i18n (~> 0.6)
|
31
31
|
multi_json (~> 1.0)
|
32
|
-
arel (3.0.
|
32
|
+
arel (3.0.2)
|
33
33
|
builder (3.0.0)
|
34
|
-
chronic (0.
|
34
|
+
chronic (0.7.0)
|
35
35
|
diff-lcs (1.1.3)
|
36
36
|
erubis (2.7.0)
|
37
37
|
hike (1.2.1)
|
38
|
-
i18n (0.6.
|
39
|
-
journey (1.0.
|
40
|
-
json (1.
|
41
|
-
multi_json (1.
|
38
|
+
i18n (0.6.1)
|
39
|
+
journey (1.0.4)
|
40
|
+
json (1.7.5)
|
41
|
+
multi_json (1.3.6)
|
42
42
|
mysql2 (0.3.11)
|
43
|
-
pg (0.
|
43
|
+
pg (0.14.0)
|
44
44
|
rack (1.4.1)
|
45
|
-
rack-cache (1.
|
45
|
+
rack-cache (1.2)
|
46
46
|
rack (>= 0.4)
|
47
47
|
rack-ssl (1.3.2)
|
48
48
|
rack
|
49
49
|
rack-test (0.6.1)
|
50
50
|
rack (>= 1.0)
|
51
|
-
railties (3.2.
|
52
|
-
actionpack (= 3.2.
|
53
|
-
activesupport (= 3.2.
|
51
|
+
railties (3.2.8)
|
52
|
+
actionpack (= 3.2.8)
|
53
|
+
activesupport (= 3.2.8)
|
54
54
|
rack-ssl (~> 1.3.2)
|
55
55
|
rake (>= 0.8.7)
|
56
56
|
rdoc (~> 3.4)
|
57
|
-
thor (
|
57
|
+
thor (>= 0.14.6, < 2.0)
|
58
58
|
rake (0.9.2.2)
|
59
59
|
rdoc (3.12)
|
60
60
|
json (~> 1.4)
|
61
|
-
rspec (2.
|
62
|
-
rspec-core (~> 2.
|
63
|
-
rspec-expectations (~> 2.
|
64
|
-
rspec-mocks (~> 2.
|
65
|
-
rspec-core (2.
|
66
|
-
rspec-expectations (2.
|
67
|
-
diff-lcs (~> 1.1.
|
68
|
-
rspec-mocks (2.
|
69
|
-
rspec-rails (2.
|
61
|
+
rspec (2.11.0)
|
62
|
+
rspec-core (~> 2.11.0)
|
63
|
+
rspec-expectations (~> 2.11.0)
|
64
|
+
rspec-mocks (~> 2.11.0)
|
65
|
+
rspec-core (2.11.1)
|
66
|
+
rspec-expectations (2.11.2)
|
67
|
+
diff-lcs (~> 1.1.3)
|
68
|
+
rspec-mocks (2.11.2)
|
69
|
+
rspec-rails (2.11.0)
|
70
70
|
actionpack (>= 3.0)
|
71
71
|
activesupport (>= 3.0)
|
72
72
|
railties (>= 3.0)
|
73
|
-
rspec (~> 2.
|
74
|
-
sprockets (2.1.
|
73
|
+
rspec (~> 2.11.0)
|
74
|
+
sprockets (2.1.3)
|
75
75
|
hike (~> 1.2)
|
76
76
|
rack (~> 1.0)
|
77
77
|
tilt (~> 1.1, != 1.3.0)
|
78
|
-
sqlite3 (1.3.
|
79
|
-
thor (0.
|
78
|
+
sqlite3 (1.3.6)
|
79
|
+
thor (0.16.0)
|
80
80
|
tilt (1.3.3)
|
81
|
-
timecop (0.
|
82
|
-
tzinfo (0.3.
|
81
|
+
timecop (0.4.5)
|
82
|
+
tzinfo (0.3.33)
|
83
83
|
|
84
84
|
PLATFORMS
|
85
85
|
ruby
|
data/README.markdown
CHANGED
@@ -1,7 +1,31 @@
|
|
1
1
|
# by_*
|
2
2
|
|
3
3
|
|
4
|
-
by_* (by_star) is a plugin that allows you to find ActiveRecord objects given certain date objects.
|
4
|
+
by_* (by_star) is a plugin that allows you to find ActiveRecord and/or Mongoid objects given certain date objects.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install this gem by adding this to your Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'by_star', :git => "git://github.com/radar/by_star"
|
12
|
+
```
|
13
|
+
|
14
|
+
Then run `bundle install`.
|
15
|
+
|
16
|
+
If you are using ActiveRecord, you're done!
|
17
|
+
|
18
|
+
Mongoid users, please include the Mongoid::ByStar module for each model you wish to use the functionality. This is the convention among Mongoid plugins.
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class MyModel
|
22
|
+
include Mongoid::Document
|
23
|
+
include Mongoid::ByStar
|
24
|
+
```
|
25
|
+
|
26
|
+
## What it does
|
27
|
+
|
28
|
+
This was originally crafted for only finding objects within a given month, but now has extended out to much more. It now supports finding objects for:
|
5
29
|
|
6
30
|
* A given year
|
7
31
|
* A given month
|
@@ -9,6 +33,8 @@ by_* (by_star) is a plugin that allows you to find ActiveRecord objects given ce
|
|
9
33
|
* A given week
|
10
34
|
* A given weekend
|
11
35
|
* A given day
|
36
|
+
* A given quarter
|
37
|
+
* The weeks of a given month as shown on a calendar
|
12
38
|
* The current weekend
|
13
39
|
* The current work week
|
14
40
|
* The Past
|
@@ -93,6 +119,14 @@ If you have a Time object you can use it to find the posts:
|
|
93
119
|
|
94
120
|
This will find all the posts in November 2012.
|
95
121
|
|
122
|
+
## By Calendar Month (`by_calendar_month`)
|
123
|
+
|
124
|
+
Finds records for a given month as shown on a calendar. Includes all the results of `by_month`, plus any results which fall in the same week as the first and last of the month. Useful for working with UI calendars which show rows of weeks.
|
125
|
+
|
126
|
+
Post.by_calendar_month
|
127
|
+
|
128
|
+
Parameter behavior is otherwise the same as `by_month`
|
129
|
+
|
96
130
|
## By Fortnight (`by_fortnight`)
|
97
131
|
|
98
132
|
Fortnight numbering starts at 0. The beginning of a fortnight is Monday, 12am.
|
@@ -127,11 +161,11 @@ To find records based on a week, you can pass in a number (representing the week
|
|
127
161
|
|
128
162
|
Post.by_week(36)
|
129
163
|
|
130
|
-
This will return all posts in the
|
164
|
+
This will return all posts in the 37th week of the current year (remember week numbering starts at 0).
|
131
165
|
|
132
166
|
Post.by_week(36, :year => 2012)
|
133
167
|
|
134
|
-
This will return all posts in the
|
168
|
+
This will return all posts in the 37th week of 2012.
|
135
169
|
|
136
170
|
Post.by_week(Time.local(2012,1,1))
|
137
171
|
|
@@ -160,6 +194,28 @@ You can also pass a string:
|
|
160
194
|
|
161
195
|
This will return all posts for the given day.
|
162
196
|
|
197
|
+
## By Quarter (`by_quarter`)
|
198
|
+
|
199
|
+
Finds records by 3-month quarterly period of year. Quarter numbering starts at 1. The four quarters of the year begin on Jan 1, Apr 1, Jul 1, and Oct 1 respectively.
|
200
|
+
|
201
|
+
To find records from the current quarter:
|
202
|
+
|
203
|
+
Post.by_quarter
|
204
|
+
|
205
|
+
To find records based on a quarter, you can pass in a number (representing the quarter number) or a time object:
|
206
|
+
|
207
|
+
Post.by_quarter(4)
|
208
|
+
|
209
|
+
This will return all posts in the 4th quarter of the current year.
|
210
|
+
|
211
|
+
Post.by_quarter(2, :year => 2012)
|
212
|
+
|
213
|
+
This will return all posts in the 2nd quarter of 2012.
|
214
|
+
|
215
|
+
Post.by_week(Time.local(2012,1,1))
|
216
|
+
|
217
|
+
This will return all posts from the first quarter of 2012.
|
218
|
+
|
163
219
|
## Tomorrow (`tomorrow`)
|
164
220
|
|
165
221
|
*This method has been shown to be shifty when passed a `Date` object, it is recommended that you pass it an `ActiveSupport::TimeWithZone` object instead.*
|
@@ -194,37 +250,37 @@ You can also pass a string:
|
|
194
250
|
|
195
251
|
Post.yesterday("next tuesday")
|
196
252
|
|
197
|
-
##
|
253
|
+
## Before (`before`)
|
198
254
|
|
199
255
|
To find all posts before the current time:
|
200
256
|
|
201
|
-
Post.
|
257
|
+
Post.before
|
202
258
|
|
203
259
|
To find all posts before certain time or date:
|
204
260
|
|
205
|
-
Post.
|
206
|
-
Post.
|
261
|
+
Post.before(Date.today + 2)
|
262
|
+
Post.before(Time.now + 5.days)
|
207
263
|
|
208
264
|
You can also pass a string:
|
209
265
|
|
210
|
-
Post.
|
266
|
+
Post.before("next tuesday")
|
211
267
|
|
212
|
-
##
|
268
|
+
## After (`after`)
|
213
269
|
|
214
270
|
To find all posts after the current time:
|
215
271
|
|
216
|
-
Post.
|
272
|
+
Post.after
|
217
273
|
|
218
274
|
To find all posts after certain time or date:
|
219
275
|
|
220
|
-
Post.
|
221
|
-
Post.
|
276
|
+
Post.after(Date.today + 2)
|
277
|
+
Post.after(Time.now + 5.days)
|
222
278
|
|
223
279
|
You can also pass a string:
|
224
280
|
|
225
|
-
Post.
|
281
|
+
Post.after("next tuesday")
|
226
282
|
|
227
|
-
## Between (`between`)
|
283
|
+
## Between (`between` or `between_times`)
|
228
284
|
|
229
285
|
To find records between two times:
|
230
286
|
|
@@ -234,6 +290,10 @@ Also works with dates:
|
|
234
290
|
|
235
291
|
Post.between(date1, date2)
|
236
292
|
|
293
|
+
`between_times` is an alias for `between`:
|
294
|
+
|
295
|
+
Post.between_times(time1, time2) #=> results identical to above
|
296
|
+
|
237
297
|
## Previous (`previous`)
|
238
298
|
|
239
299
|
To find the record prior to this one call `previous` on any model instance:
|
@@ -268,6 +328,10 @@ Or if you're doing it all the time on your model, then it's best to use `by_star
|
|
268
328
|
by_star_field :something_else
|
269
329
|
end
|
270
330
|
|
331
|
+
## Mongoid
|
332
|
+
|
333
|
+
Mongoid is only tested/supported on version 3.0+
|
334
|
+
|
271
335
|
## Collaborators
|
272
336
|
|
273
337
|
Thanks to Thomas Sinclair for the original bump for implementing it. I would like to thank #rubyonrails for their support and the following people:
|
data/by_star.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Ryan Bigg"]
|
9
9
|
s.email = ["radarlistener@gmail.com"]
|
10
|
-
s.homepage = "http://
|
10
|
+
s.homepage = "http://github.com/radar/by_star"
|
11
11
|
s.summary = "ActiveRecord extension for easier date scopes and time ranges"
|
12
12
|
s.description = "ActiveRecord extension for easier date scopes and time ranges"
|
13
13
|
|
@@ -20,8 +20,9 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.add_development_dependency "mysql2"
|
21
21
|
s.add_development_dependency "rspec-rails", "~> 2.8"
|
22
22
|
s.add_development_dependency "timecop", "~> 0.3"
|
23
|
+
s.add_development_dependency "mongoid", "~> 3.0" if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
23
24
|
|
24
|
-
s.add_dependency "activerecord", "
|
25
|
+
s.add_dependency "activerecord", "~> 3.0"
|
25
26
|
s.add_dependency "chronic"
|
26
27
|
|
27
28
|
s.files = `git ls-files`.split("\n")
|
data/lib/by_star.rb
CHANGED
@@ -10,12 +10,13 @@ require 'by_star/by_fortnight'
|
|
10
10
|
require 'by_star/by_week'
|
11
11
|
require 'by_star/by_weekend'
|
12
12
|
require 'by_star/by_day'
|
13
|
+
require 'by_star/by_quarter'
|
13
14
|
|
14
15
|
module ByStar
|
15
16
|
|
16
17
|
def by_star_field(field=nil)
|
17
18
|
@by_star_field ||= field
|
18
|
-
@by_star_field || "created_at"
|
19
|
+
@by_star_field || "#{self.table_name}.created_at"
|
19
20
|
end
|
20
21
|
|
21
22
|
include ByDirection
|
@@ -25,6 +26,7 @@ module ByStar
|
|
25
26
|
include ByWeek
|
26
27
|
include ByWeekend
|
27
28
|
include ByDay
|
29
|
+
include ByQuarter
|
28
30
|
|
29
31
|
class ParseError < StandardError
|
30
32
|
|
@@ -40,6 +42,7 @@ module ByStar
|
|
40
42
|
scope = scope.order(options[:order]) if options[:order]
|
41
43
|
scope
|
42
44
|
end
|
45
|
+
alias_method :between_times, :between
|
43
46
|
|
44
47
|
private
|
45
48
|
|
@@ -57,7 +60,12 @@ module ByStar
|
|
57
60
|
|
58
61
|
end
|
59
62
|
|
60
|
-
ActiveRecord
|
61
|
-
ActiveRecord::
|
63
|
+
if defined?(ActiveRecord)
|
64
|
+
ActiveRecord::Base.send :extend, ByStar
|
65
|
+
ActiveRecord::Relation.send :extend, ByStar
|
66
|
+
ActiveRecord::Base.send :include, ByStar::InstanceMethods
|
67
|
+
end
|
62
68
|
|
63
|
-
|
69
|
+
if defined?(Mongoid)
|
70
|
+
require 'mongoid/by_star'
|
71
|
+
end
|
data/lib/by_star/by_day.rb
CHANGED
@@ -25,6 +25,10 @@ module ByStar
|
|
25
25
|
def by_day_Time(time, options)
|
26
26
|
between(time.beginning_of_day, time.end_of_day, options)
|
27
27
|
end
|
28
|
+
alias_method :by_day_Date, :by_day_Time
|
28
29
|
|
30
|
+
def by_day_String(string, options)
|
31
|
+
by_day_Time(Time.parse(string), options)
|
32
|
+
end
|
29
33
|
end
|
30
34
|
end
|
data/lib/by_star/by_fortnight.rb
CHANGED
@@ -23,6 +23,10 @@ module ByStar
|
|
23
23
|
between(start_time, (start_time + 2.weeks).end_of_day, options)
|
24
24
|
end
|
25
25
|
|
26
|
+
def by_fortnight_Date(date, options={})
|
27
|
+
by_fortnight_Time(date.to_time, options)
|
28
|
+
end
|
29
|
+
|
26
30
|
def by_fortnight_String_or_Fixnum(weeks, options={})
|
27
31
|
weeks = weeks.to_i
|
28
32
|
current_time = Time.zone.local(options[:year] || Time.zone.now.year)
|
data/lib/by_star/by_month.rb
CHANGED
@@ -6,30 +6,42 @@ module ByStar
|
|
6
6
|
def by_month(*args)
|
7
7
|
options = args.extract_options!
|
8
8
|
time = args.first || Time.zone.now
|
9
|
-
send("by_month_#{time_klass(time)}", time, options)
|
9
|
+
send("by_month_#{time_klass(time)}", time, false, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def by_calendar_month(*args)
|
13
|
+
options = args.extract_options!
|
14
|
+
time = args.first || Time.zone.now
|
15
|
+
send("by_month_#{time_klass(time)}", time, true, options)
|
10
16
|
end
|
11
17
|
|
12
18
|
private
|
13
19
|
|
14
|
-
def by_month_Time(time, options={})
|
15
|
-
|
20
|
+
def by_month_Time(time, is_calendar=false, options={})
|
21
|
+
if is_calendar
|
22
|
+
between(time.beginning_of_month.beginning_of_week, time.end_of_month.end_of_week, options)
|
23
|
+
else
|
24
|
+
between(time.beginning_of_month, time.end_of_month, options)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def by_month_Date(date, is_calendar=false, options={})
|
29
|
+
by_month_Time(date.to_time, is_calendar, options)
|
16
30
|
end
|
17
31
|
|
18
|
-
def by_month_String_or_Fixnum(month, options={})
|
19
|
-
|
32
|
+
def by_month_String_or_Fixnum(month, is_calendar=false, options={})
|
33
|
+
begin
|
20
34
|
year = options[:year] || Time.zone.now.year
|
21
|
-
|
22
|
-
|
35
|
+
date = Date.parse("#{year}-#{month}-01").to_time
|
36
|
+
by_month_Time(date, is_calendar, options)
|
37
|
+
|
38
|
+
rescue
|
23
39
|
raise ParseError, "Month must be a number between 1 and 12 or the full month name (e.g. 'January', 'Feburary', etc.)"
|
24
40
|
end
|
25
41
|
end
|
42
|
+
|
26
43
|
alias_method :by_month_String, :by_month_String_or_Fixnum
|
27
44
|
alias_method :by_month_Fixnum, :by_month_String_or_Fixnum
|
28
45
|
|
29
|
-
|
30
|
-
def valid_month?(month)
|
31
|
-
(1..12).include?(month) || Date::MONTHNAMES.include?(month)
|
32
|
-
end
|
33
|
-
|
34
46
|
end
|
35
47
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module ByStar
|
2
|
+
module ByQuarter
|
3
|
+
# For reasoning why I use *args rather than variables here,
|
4
|
+
# please see the by_year method comments in lib/by_star/by_year.rb
|
5
|
+
|
6
|
+
def by_quarter(*args)
|
7
|
+
options = args.extract_options!.symbolize_keys!
|
8
|
+
time = args.first
|
9
|
+
time ||= Time.zone.local(options[:year], 1, 1) if options[:year]
|
10
|
+
time ||= Time.zone.now
|
11
|
+
send("by_quarter_#{time_klass(time)}", time, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def by_quarter_Time(time, options={})
|
17
|
+
between(time.beginning_of_quarter, time.end_of_quarter, options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def by_quarter_Date(date, options={})
|
21
|
+
by_quarter_Time(date.to_time, options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def by_quarter_Fixnum(quarter, options={})
|
25
|
+
raise 'Quarter must be a number between 1 and 4' unless quarter >= 1 && quarter <= 4
|
26
|
+
time = Time.zone.local(options[:year], 1, 1) if options[:year]
|
27
|
+
time ||= Time.zone.now
|
28
|
+
start_time = time.beginning_of_year + ((quarter.to_i - 1) * 3).months
|
29
|
+
between(start_time, start_time.end_of_quarter, options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|