by_star 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +4 -4
- data/VERSION +1 -1
- data/by_star.gemspec +2 -2
- data/lib/by_star.rb +1 -1
- data/lib/neighbours.rb +3 -3
- data/lib/vanilla.rb +3 -7
- data/spec/by_star_spec.rb +19 -9
- metadata +4 -4
data/README.markdown
CHANGED
@@ -34,7 +34,7 @@ If you're not using the standard `created_at` field: don't worry! I've covered t
|
|
34
34
|
You can treat all `by_*` methods exactly how you would treat `named_scope`s: they are effectively scopes in their own right. This means you are able to call them like this:
|
35
35
|
|
36
36
|
Post.by_month.my_special_scope
|
37
|
-
|
37
|
+
|
38
38
|
Where `my_special_scope` is a `named_scope` you have specified.
|
39
39
|
|
40
40
|
All the `by_*` methods, with the exception of `previous` and `next`, take a block which will then scope the find based on the options passed into it. You can also specify these options for each method, but the syntax may differ. The supported options are the same options that are supported by `find` from ActiveRecord. Please note that if you want to use conditions you *have* to use this syntax:
|
@@ -371,7 +371,7 @@ To find records up to a certain time from the current time:
|
|
371
371
|
To find the record prior to this one call `previous` on any model instance:
|
372
372
|
|
373
373
|
Post.last.previous
|
374
|
-
|
374
|
+
|
375
375
|
You can specify a field also:
|
376
376
|
|
377
377
|
Post.last.previous("published_at")
|
@@ -391,9 +391,9 @@ You can specify a field also:
|
|
391
391
|
If your database uses something other than `created_at` for storing a timestamp, you can specify the field option like this:
|
392
392
|
|
393
393
|
Post.by_month("January", :field => :something_else)
|
394
|
-
|
394
|
+
|
395
395
|
All methods support this extra option.
|
396
|
-
|
396
|
+
|
397
397
|
Or if you're doing it all the time on your model, then it's best to use `by_star_field` at the top of your model:
|
398
398
|
|
399
399
|
class Post < ActiveRecord::Base
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/by_star.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{by_star}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.8.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ryan Bigg", "Mislav Marohni\304\207"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-06-05}
|
13
13
|
s.description = %q{ActiveRecord extension for easier date scopes and time ranges}
|
14
14
|
s.email = %q{radarlistener@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/by_star.rb
CHANGED
data/lib/neighbours.rb
CHANGED
@@ -3,13 +3,13 @@ module ByStar
|
|
3
3
|
# Find the previous record to this.
|
4
4
|
def previous(field=nil)
|
5
5
|
field = field || self.class.by_star_field
|
6
|
-
self.class.past(self.send(field.split(".").last)) { { :order => "#{field} DESC" }}.first
|
6
|
+
self.class.past(self.send(field.to_s.split(".").last)) { { :order => "#{field} DESC" }}.first
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
# Find the next record to this.
|
10
10
|
def next(field=nil)
|
11
11
|
field = field || self.class.by_star_field
|
12
|
-
self.class.future(self.send(field.split(".").last)) { { :order => "#{field} ASC" }}.first
|
12
|
+
self.class.future(self.send(field.to_s.split(".").last)) { { :order => "#{field} ASC" }}.first
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/lib/vanilla.rb
CHANGED
@@ -81,19 +81,15 @@ module ByStar
|
|
81
81
|
# Surely there's a method in Rails to do this.
|
82
82
|
start_time = if valid_time_or_date?(time)
|
83
83
|
weeks = time.strftime("%U").to_i
|
84
|
-
|
85
|
-
# Sunday defines the start of the week.
|
86
|
-
# Finds the sunday that defines the starting week of the year.
|
87
|
-
# This is because AS +*.weeks helper works off the given time, which could be any day.
|
88
|
-
# Principle of least surprise and all. Ideally.
|
89
|
-
start_time = Time.now.beginning_of_year
|
90
|
-
start_time.strftime("%w").to_i != 0 ? start_time - (7 - start_time.strftime("%w").to_i).days : start_time
|
84
|
+
Time.zone.now.beginning_of_year
|
91
85
|
elsif time.is_a?(Numeric) && time < 53
|
92
86
|
weeks = time.to_i
|
93
87
|
Time.utc(year, 1, 1)
|
94
88
|
else
|
95
89
|
raise ParseError, "by_week takes only a Time or Date object, a Fixnum (less than or equal to 53) or a Chronicable string."
|
96
90
|
end
|
91
|
+
# Make the start of the week predictably Sunday.
|
92
|
+
start_time.strftime("%w").to_i != 0 ? start_time - (7 - start_time.strftime("%w").to_i).days : start_time
|
97
93
|
start_time += weeks.weeks
|
98
94
|
end_time = start_time + 1.week
|
99
95
|
by_star(start_time, end_time, options, &block)
|
data/spec/by_star_spec.rb
CHANGED
@@ -79,7 +79,7 @@ describe Post do
|
|
79
79
|
it "should be able to use an alternative field (symbol)" do
|
80
80
|
Event.by_year(nil, :field => :start_time).size.should eql(8)
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
it "should not have to specify the field when using by_star_field" do
|
84
84
|
Event.by_year.size.should eql(8)
|
85
85
|
end
|
@@ -644,37 +644,47 @@ describe Post do
|
|
644
644
|
end
|
645
645
|
|
646
646
|
end
|
647
|
-
|
647
|
+
|
648
648
|
describe "directional finders" do
|
649
649
|
subject { Post.today.first }
|
650
|
-
|
650
|
+
let(:event) { Event.last }
|
651
|
+
|
651
652
|
describe "previous" do
|
652
653
|
it "should find the post previous to it" do
|
653
654
|
subject.previous.text.should eql("Yesterday's post")
|
654
655
|
end
|
656
|
+
|
657
|
+
it "should find the previous event" do
|
658
|
+
event.previous.name.should eql("Ryan's birthday, last year!")
|
659
|
+
end
|
655
660
|
end
|
656
|
-
|
657
|
-
|
661
|
+
|
662
|
+
|
658
663
|
describe "next" do
|
664
|
+
let(:event) { Event.first }
|
659
665
|
it "should find the post next to it" do
|
660
666
|
subject.next.text.should eql("Tomorrow's post")
|
661
667
|
end
|
668
|
+
|
669
|
+
it "should find the next event" do
|
670
|
+
event.next.should be_nil
|
671
|
+
end
|
662
672
|
end
|
663
673
|
end
|
664
|
-
|
674
|
+
|
665
675
|
describe "chaining of methods" do
|
666
676
|
# a by_star and a by_direction method, in that order
|
667
677
|
it "should be able to chain today and past" do
|
668
678
|
Post.today.past.size.should eql(4)
|
669
679
|
end
|
670
|
-
|
680
|
+
|
671
681
|
# a by_direction and by_star method, in that order
|
672
682
|
it "should be able to chain together past and today" do
|
673
683
|
Post.past.today.size.should eql(4)
|
674
684
|
end
|
675
|
-
|
685
|
+
|
676
686
|
end
|
677
|
-
|
687
|
+
|
678
688
|
describe "edge cases" do
|
679
689
|
# This method previously generated sql like: `day_entries`.`spent_at`.`spent_at`.`spent_at`.`spent_at`
|
680
690
|
# Which is *obviously* incorrect and #omg worthy.
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 8
|
8
|
+
- 0
|
9
|
+
version: 0.8.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ryan Bigg
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-06-05 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|