by_star 0.6.4 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +21 -1
- data/VERSION +1 -1
- data/by_star.gemspec +2 -1
- data/lib/by_star.rb +6 -0
- data/lib/neighbours.rb +13 -0
- data/spec/by_star_spec.rb +18 -1
- metadata +4 -3
data/README.markdown
CHANGED
@@ -32,7 +32,7 @@ You can treat all `by_*` methods exactly how you would treat `named_scope`s: the
|
|
32
32
|
|
33
33
|
Where `my_special_scope` is a `named_scope` you have specified.
|
34
34
|
|
35
|
-
All the `by_*` methods 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:
|
35
|
+
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:
|
36
36
|
|
37
37
|
Post.by_month(1) { { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] } }
|
38
38
|
|
@@ -361,6 +361,26 @@ To find records up to a certain time from the current time:
|
|
361
361
|
|
362
362
|
Post.up_to_6_weeks_from_now
|
363
363
|
|
364
|
+
## Previous (`previous`)
|
365
|
+
|
366
|
+
To find the record prior to this one call `previous` on any model instance:
|
367
|
+
|
368
|
+
Post.last.previous
|
369
|
+
|
370
|
+
You can specify a field also:
|
371
|
+
|
372
|
+
Post.last.previous("published_at")
|
373
|
+
|
374
|
+
## Next (`next`)
|
375
|
+
|
376
|
+
To find the record after this one call `next` on any model instance:
|
377
|
+
|
378
|
+
Post.last.next
|
379
|
+
|
380
|
+
You can specify a field also:
|
381
|
+
|
382
|
+
Post.last.next("published_at")
|
383
|
+
|
364
384
|
## Not using created_at? No worries!
|
365
385
|
|
366
386
|
If your database uses something other than `created_at` for storing a timestamp, you can specify the field option like this:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.7.0
|
data/by_star.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{by_star}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.7.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"]
|
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/calculations.rb",
|
29
29
|
"lib/calculations/count.rb",
|
30
30
|
"lib/calculations/sum.rb",
|
31
|
+
"lib/neighbours.rb",
|
31
32
|
"lib/range_calculations.rb",
|
32
33
|
"lib/shared.rb",
|
33
34
|
"lib/time_ext.rb",
|
data/lib/by_star.rb
CHANGED
@@ -3,6 +3,7 @@ require 'shared'
|
|
3
3
|
require 'range_calculations'
|
4
4
|
require 'time_ext'
|
5
5
|
require 'vanilla'
|
6
|
+
require 'neighbours'
|
6
7
|
|
7
8
|
Dir[File.dirname(__FILE__) + '/calculations/*.rb'].each { |file| require file }
|
8
9
|
require 'calculations'
|
@@ -10,6 +11,7 @@ module ByStar
|
|
10
11
|
|
11
12
|
def self.included(base)
|
12
13
|
base.extend ClassMethods
|
14
|
+
base.send(:include, InstanceMethods)
|
13
15
|
end
|
14
16
|
|
15
17
|
module ClassMethods
|
@@ -18,6 +20,10 @@ module ByStar
|
|
18
20
|
include Vanilla
|
19
21
|
include Calculations
|
20
22
|
end
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
include Neighbours
|
26
|
+
end
|
21
27
|
|
22
28
|
class ParseError < Exception; end
|
23
29
|
class MonthNotFound < Exception; end
|
data/lib/neighbours.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module ByStar
|
2
|
+
module Neighbours
|
3
|
+
# Find the previous record to this.
|
4
|
+
def previous(field="created_at")
|
5
|
+
self.class.past(self.send(field)) { { :order => "#{field} DESC" }}.first
|
6
|
+
end
|
7
|
+
|
8
|
+
# Find the next record to this.
|
9
|
+
def next(field="created_at")
|
10
|
+
self.class.future(self.send(field)) { { :order => "#{field} ASC" }}.first
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/by_star_spec.rb
CHANGED
@@ -641,6 +641,23 @@ describe Post do
|
|
641
641
|
|
642
642
|
end
|
643
643
|
|
644
|
+
describe "directional finders" do
|
645
|
+
subject { Post.today.first }
|
646
|
+
|
647
|
+
describe "previous" do
|
648
|
+
it "should find the post previous to it" do
|
649
|
+
subject.previous.text.should eql("Yesterday's post")
|
650
|
+
end
|
651
|
+
end
|
652
|
+
|
653
|
+
|
654
|
+
describe "next" do
|
655
|
+
it "should find the post next to it" do
|
656
|
+
subject.next.text.should eql("Tomorrow's post")
|
657
|
+
end
|
658
|
+
end
|
659
|
+
end
|
660
|
+
|
644
661
|
describe "chaining of methods" do
|
645
662
|
# a by_star and a by_direction method, in that order
|
646
663
|
it "should be able to chain today and past" do
|
@@ -653,7 +670,7 @@ describe Post do
|
|
653
670
|
end
|
654
671
|
|
655
672
|
end
|
656
|
-
|
673
|
+
|
657
674
|
describe "edge cases" do
|
658
675
|
# This method previously generated sql like: `day_entries`.`spent_at`.`spent_at`.`spent_at`.`spent_at`
|
659
676
|
# 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
|
+
- 7
|
8
|
+
- 0
|
9
|
+
version: 0.7.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ryan Bigg
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/calculations.rb
|
66
66
|
- lib/calculations/count.rb
|
67
67
|
- lib/calculations/sum.rb
|
68
|
+
- lib/neighbours.rb
|
68
69
|
- lib/range_calculations.rb
|
69
70
|
- lib/shared.rb
|
70
71
|
- lib/time_ext.rb
|