Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for "past_"

* week
* fortnight
* month
  • Loading branch information...
commit 4543b7205880908f2b2a8ec5980ea366da3302c9 1 parent dd65a41
@davegudge davegudge authored
View
21 README.md
@@ -41,6 +41,9 @@ This was originally crafted for only finding objects within a given month, but n
* The current weekend
* The current work week
* The Past
+* The past month
+* The past fortnight
+* The past week
* The Future
* Between certain times
* Before a specific record
@@ -269,6 +272,24 @@ You can also pass a string:
Post.yesterday("next tuesday")
+## Past Month (`past_month`)
+
+To find all posts in the past month:
+
+ Post.past_month
+
+## Past Fortnight (`past_fortnight`)
+
+To find all posts in the past fortnight:
+
+ Post.past_fortnight
+
+## Past Week (`past_week`)
+
+To find all posts in the past week:
+
+ Post.past_week
+
## Before (`before`)
To find all posts before the current time:
View
12 lib/by_star/between.rb
@@ -76,5 +76,17 @@ def by_year(*args)
between_times(time.beginning_of_year, time.end_of_year, options)
end
end
+
+ def past_week(options={})
+ between_times(Time.zone.now - 1.week, Time.zone.now, options)
+ end
+
+ def past_fortnight(options={})
+ between_times(Time.zone.now - 2.weeks, Time.zone.now, options)
+ end
+
+ def past_month(options={})
+ between_times(Time.zone.now - 1.month, Time.zone.now, options)
+ end
end
end
View
11 spec/fixtures/shared/seeds.rb
@@ -1,4 +1,13 @@
-%w(2013-12-31
+%w(2013-11-01
+ 2013-11-30
+ 2013-12-01
+ 2013-12-05
+ 2013-12-08
+ 2013-12-16
+ 2013-12-22
+ 2013-12-25
+ 2013-12-28
+ 2013-12-31
2014-01-01
2014-01-01
2014-01-05
View
1  spec/integration/active_record/active_record_spec.rb
@@ -29,6 +29,7 @@
it_behaves_like 'by weekend'
it_behaves_like 'by year'
it_behaves_like 'offset parameter'
+ it_behaves_like 'past'
it 'should be able to order the result set' do
scope = Post.by_year(Time.zone.now.year, :order => 'created_at DESC')
View
1  spec/integration/mongoid/mongoid_spec.rb
@@ -28,6 +28,7 @@
it_behaves_like 'by weekend'
it_behaves_like 'by year'
it_behaves_like 'offset parameter'
+ it_behaves_like 'past'
describe '#between_times' do
subject { Post.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-06')) }
View
10 spec/integration/shared/by_calendar_month.rb
@@ -11,7 +11,7 @@
context 'timespan' do
subject { Event.by_calendar_month(1) }
- its(:count){ should eq 8 }
+ its(:count){ should eq 10 }
end
context 'timespan strict' do
@@ -23,17 +23,17 @@
context 'point-in-time' do
subject { Post.by_calendar_month(12, year: 2013) }
- its(:count){ should eq 4 }
+ its(:count){ should eq 12 }
end
context 'timespan' do
subject { Event.by_calendar_month('December', year: 2013) }
- its(:count){ should eq 5 }
+ its(:count){ should eq 13 }
end
context 'timespan strict' do
subject { Event.by_calendar_month('Dec', year: 2013, strict: true) }
- its(:count){ should eq 1 }
+ its(:count){ should eq 9 }
end
end
@@ -44,7 +44,7 @@
end
it 'should be able to use an alternative field' do
- Event.by_calendar_month(:field => 'end_time').count.should eq 8
+ Event.by_calendar_month(:field => 'end_time').count.should eq 10
end
context ':start_day option' do
View
10 spec/integration/shared/by_day.rb
@@ -11,7 +11,7 @@
context 'timespan' do
subject { Event.by_day(Time.parse '2014-01-01') }
- its(:count){ should eq 4 }
+ its(:count){ should eq 5 }
end
context 'timespan strict' do
@@ -20,7 +20,7 @@
end
it 'should be able to use an alternative field' do
- Event.by_day(:field => 'end_time').count.should eq 4
+ Event.by_day(:field => 'end_time').count.should eq 5
end
it 'should support :offset option' do
@@ -37,7 +37,7 @@
context 'timespan' do
subject { Event.today }
- its(:count){ should eq 4 }
+ its(:count){ should eq 5 }
end
context 'timespan strict' do
@@ -55,7 +55,7 @@
context 'timespan' do
subject { Event.yesterday }
- its(:count){ should eq 4 }
+ its(:count){ should eq 5 }
end
context 'timespan strict' do
@@ -73,7 +73,7 @@
context 'timespan' do
subject { Event.tomorrow }
- its(:count){ should eq 4 }
+ its(:count){ should eq 5 }
end
context 'timespan strict' do
View
6 spec/integration/shared/by_direction.rb
@@ -6,17 +6,17 @@
context 'point-in-time' do
subject { Post.before(Date.parse '2014-01-05') }
- its(:count){ should eq 3 }
+ its(:count){ should eq 12 }
end
context 'timespan' do
subject { Event.before(Time.parse '2014-01-05') }
- its(:count){ should eq 4 }
+ its(:count){ should eq 13 }
end
context 'timespan strict' do
subject { Event.before('2014-01-05', strict: true) }
- its(:count){ should eq 4 }
+ its(:count){ should eq 13 }
end
end
View
6 spec/integration/shared/by_fortnight.rb
@@ -11,7 +11,7 @@
context 'timespan' do
subject { Event.by_fortnight(0) }
- its(:count){ should eq 6 }
+ its(:count){ should eq 7 }
end
context 'timespan strict' do
@@ -28,7 +28,7 @@
context 'timespan' do
subject { Event.by_fortnight(26, year: 2013) }
- its(:count){ should eq 6 }
+ its(:count){ should eq 7 }
end
context 'timespan strict' do
@@ -42,7 +42,7 @@
end
it 'should be able to use an alternative field' do
- Event.by_fortnight(:field => 'end_time').count.should eq 6
+ Event.by_fortnight(:field => 'end_time').count.should eq 7
end
end
end
View
10 spec/integration/shared/by_month.rb
@@ -11,7 +11,7 @@
context 'timespan' do
subject { Event.by_month(1) }
- its(:count){ should eq 8 }
+ its(:count){ should eq 9 }
end
context 'timespan strict' do
@@ -23,17 +23,17 @@
context 'point-in-time' do
subject { Post.by_month(12, year: 2013) }
- its(:count){ should eq 1 }
+ its(:count){ should eq 8 }
end
context 'timespan' do
subject { Event.by_month('December', year: 2013) }
- its(:count){ should eq 4 }
+ its(:count){ should eq 12 }
end
context 'timespan strict' do
subject { Event.by_month('Dec', year: 2013, strict: true) }
- its(:count){ should eq 0 }
+ its(:count){ should eq 4 }
end
end
@@ -44,7 +44,7 @@
end
it 'should be able to use an alternative field' do
- Event.by_month(:field => 'end_time').count.should eq 8
+ Event.by_month(:field => 'end_time').count.should eq 9
end
end
end
View
10 spec/integration/shared/by_quarter.rb
@@ -11,7 +11,7 @@
context 'timespan' do
subject { Event.by_quarter(1) }
- its(:count){ should eq 12 }
+ its(:count){ should eq 13 }
end
context 'timespan strict' do
@@ -23,17 +23,17 @@
context 'point-in-time' do
subject { Post.by_quarter(4, year: 2013) }
- its(:count){ should eq 1 }
+ its(:count){ should eq 10 }
end
context 'timespan' do
subject { Event.by_quarter(4, year: 2013) }
- its(:count){ should eq 4 }
+ its(:count){ should eq 13 }
end
context 'timespan strict' do
subject { Event.by_quarter(4, year: 2013, strict: true) }
- its(:count){ should eq 0 }
+ its(:count){ should eq 8 }
end
end
@@ -43,7 +43,7 @@
end
it 'should be able to use an alternative field' do
- Event.by_quarter(1, :field => 'end_time').count.should eq 12
+ Event.by_quarter(1, :field => 'end_time').count.should eq 13
end
end
end
View
6 spec/integration/shared/by_week.rb
@@ -11,7 +11,7 @@
context 'timespan' do
subject { Event.by_week(0) }
- its(:count){ should eq 5 }
+ its(:count){ should eq 7 }
end
context 'timespan strict' do
@@ -28,7 +28,7 @@
context 'timespan' do
subject { Event.by_week(52, year: 2013) }
- its(:count){ should eq 5 }
+ its(:count){ should eq 7 }
end
context 'timespan strict' do
@@ -43,7 +43,7 @@
end
it 'should be able to use an alternative field' do
- Event.by_week(:field => 'end_time').count.should eq 5
+ Event.by_week(:field => 'end_time').count.should eq 7
end
context ':start_day option' do
View
12 spec/integration/shared/by_year.rb
@@ -11,7 +11,7 @@
context 'timespan' do
subject { Event.by_year(13) }
- its(:count){ should eq 4 }
+ its(:count){ should eq 13 }
end
context 'timespan strict' do
@@ -23,26 +23,26 @@
context 'point-in-time' do
subject { Post.by_year(year: 2013) }
- its(:count){ should eq 1 }
+ its(:count){ should eq 10 }
end
context 'timespan' do
subject { Event.by_year(year: 2014) }
- its(:count){ should eq 13 }
+ its(:count){ should eq 14 }
end
context 'timespan strict' do
subject { Event.by_year(year: 2013, strict: true) }
- its(:count){ should eq 0 }
+ its(:count){ should eq 8 }
end
end
it 'should be able to use an alternative field' do
- Event.by_year(:field => 'end_time').count.should eq 13
+ Event.by_year(:field => 'end_time').count.should eq 14
end
it 'can use a 2-digit year' do
- Post.by_year(13).count.should eq 1
+ Post.by_year(13).count.should eq 10
end
end
end
View
6 spec/integration/shared/offset_parameter.rb
@@ -10,17 +10,17 @@
context 'between_times with model offset' do
subject { Event.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-10')) }
- its(:count) { should eq 6 }
+ its(:count) { should eq 7 }
end
context 'between_times with offset override' do
subject { Event.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-10'), offset: 16.hours) }
- its(:count) { should eq 6 }
+ its(:count) { should eq 7 }
end
context 'by_day with model offset' do
subject { Event.by_day(Time.parse('2014-01-01')) }
- its(:count) { should eq 4 }
+ its(:count) { should eq 5 }
end
context 'by_day with offset override' do
View
56 spec/integration/shared/past.rb
@@ -0,0 +1,56 @@
+require 'spec_helper'
+
+shared_examples_for 'past' do
+
+ describe '#past_week' do
+ context 'point-in-time' do
+ subject { Post.past_week }
+ its(:count){ should eq 3 }
+ end
+
+ context 'timespan' do
+ subject { Event.past_week }
+ its(:count){ should eq 7 }
+ end
+
+ context 'timespan strict' do
+ subject { Event.past_week(strict: true) }
+ its(:count){ should eq 0 }
+ end
+ end
+
+ describe '#past_fortnight' do
+ context 'point-in-time' do
+ subject { Post.past_fortnight }
+ its(:count){ should eq 4 }
+ end
+
+ context 'timespan' do
+ subject { Event.past_fortnight }
+ its(:count){ should eq 8 }
+ end
+
+ context 'timespan strict' do
+ subject { Event.past_fortnight(strict: true) }
+ its(:count){ should eq 1 }
+ end
+ end
+
+ describe '#past_month' do
+ context 'point-in-time' do
+ subject { Post.past_month }
+ its(:count){ should eq 8 }
+ end
+
+ context 'timespan' do
+ subject { Event.past_month }
+ its(:count){ should eq 12 }
+ end
+
+ context 'timespan strict' do
+ subject { Event.past_month(strict: true) }
+ its(:count){ should eq 4 }
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.