scoped_traversal 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/LICENSE +20 -0
- data/README.md +59 -0
- data/Rakefile +5 -0
- data/lib/scoped_traversal.rb +3 -0
- data/lib/scoped_traversal/model_additions.rb +30 -0
- data/lib/scoped_traversal/railtie.rb +9 -0
- data/lib/scoped_traversal/version.rb +3 -0
- data/scoped_traversal.gemspec +26 -0
- data/spec/scoped_traversal/model_additions_spec.rb +26 -0
- data/spec/spec_helper.rb +6 -0
- metadata +91 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
scoped_traversal (0.1.alpha)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rspec (2.8.0)
|
12
|
+
rspec-core (~> 2.8.0)
|
13
|
+
rspec-expectations (~> 2.8.0)
|
14
|
+
rspec-mocks (~> 2.8.0)
|
15
|
+
rspec-core (2.8.0)
|
16
|
+
rspec-expectations (2.8.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.8.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
rake
|
25
|
+
rspec
|
26
|
+
scoped_traversal!
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Wil Gieseler
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Scoped Traversal
|
2
|
+
|
3
|
+
A simple way to define next and previous relationships in an ActiveRecord model.
|
4
|
+
|
5
|
+
For example, if you have a list of `articles`, and you want to be able to traverse them with `article.next` and `article.previous`.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add to your Gemfile and run the `bundle` command to install it.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "scoped_traversal"
|
13
|
+
```
|
14
|
+
|
15
|
+
**Requires Ruby 1.9.2 or later.**
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
In your model, call rank_by and pass in a column name.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
traverse_by :score
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
You can then traverse them with the following methods:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
@user.next
|
31
|
+
@user.previous
|
32
|
+
```
|
33
|
+
|
34
|
+
### Namespaces
|
35
|
+
|
36
|
+
You can also add an optional namespace, like so:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class User < ActiveRecord::Base
|
40
|
+
traverse_by :score, :namespace => true
|
41
|
+
traverse_by :created_at, :namespace => :oldest
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
You can then traverse them with the following methods:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
@user.next_score
|
49
|
+
@user.previous_score
|
50
|
+
|
51
|
+
@user.next_oldest
|
52
|
+
@user.previous_oldest
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
Questions or problems? Please post them on the [issue tracker](https://github.com/supapuerco/scoped_traversal/issues). You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running `bundle` and `rake`.
|
58
|
+
|
59
|
+
This gem is created by Wil Gieseler and is under the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module ScopedTraversal
|
2
|
+
module ModelAdditions
|
3
|
+
|
4
|
+
def traverse_by(scope, options = {})
|
5
|
+
|
6
|
+
if options[:namespace] == true
|
7
|
+
next_method_name = "next_#{scope}"
|
8
|
+
previous_method_name = "previous_#{scope}"
|
9
|
+
elsif options[:namespace]
|
10
|
+
next_method_name = "next_#{options[:namespace]}"
|
11
|
+
previous_method_name = "next_#{options[:namespace]}"
|
12
|
+
else
|
13
|
+
next_method_name = "next"
|
14
|
+
previous_method_name = "previous"
|
15
|
+
end
|
16
|
+
|
17
|
+
define_method next_method_name do
|
18
|
+
list = self.class.try(scope.to_sym).all
|
19
|
+
list[list.index(self) + 1]
|
20
|
+
end
|
21
|
+
|
22
|
+
define_method previous_method_name do
|
23
|
+
list = self.class.try(scope.to_sym).all
|
24
|
+
list[list.index(self) - 1]
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "scoped_traversal/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "scoped_traversal"
|
7
|
+
s.version = ScopedTraversal::VERSION
|
8
|
+
s.authors = ["Wil Gieseler"]
|
9
|
+
s.email = ["supapuerco@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/supapuerco/scoped_traversal"
|
11
|
+
s.summary = %q{A simple way to traverse ActiveRecord models by an ordered column.}
|
12
|
+
s.description = %q{A simple way to traverse ActiveRecord models by an ordered column.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "scoped_traversal"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
# s.add_development_dependency "with_model"
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
# s.add_runtime_dependency "rest-client"
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
# class User < SuperModel::Base
|
5
|
+
# attributes :position
|
6
|
+
#
|
7
|
+
# scope :position, order(:position)
|
8
|
+
#
|
9
|
+
# ## Put in the shit!
|
10
|
+
# extend ScopedTraversal::ModelAdditions
|
11
|
+
# traverse_by :position
|
12
|
+
# end
|
13
|
+
|
14
|
+
describe ScopedTraversal::ModelAdditions do
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
@user1 = User.create!(position: 1)
|
18
|
+
@user2 = User.create!(position: 2)
|
19
|
+
@user3 = User.create!(position: 3)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "adds object#next to traverse forward" do
|
23
|
+
@user1.next.should eq(@user2)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scoped_traversal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.1"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Wil Gieseler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-01-21 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rake
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: A simple way to traverse ActiveRecord models by an ordered column.
|
38
|
+
email:
|
39
|
+
- supapuerco@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- .rspec
|
49
|
+
- CHANGELOG.md
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/scoped_traversal.rb
|
56
|
+
- lib/scoped_traversal/model_additions.rb
|
57
|
+
- lib/scoped_traversal/railtie.rb
|
58
|
+
- lib/scoped_traversal/version.rb
|
59
|
+
- scoped_traversal.gemspec
|
60
|
+
- spec/scoped_traversal/model_additions_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
homepage: https://github.com/supapuerco/scoped_traversal
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "0"
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project: scoped_traversal
|
85
|
+
rubygems_version: 1.8.15
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: A simple way to traverse ActiveRecord models by an ordered column.
|
89
|
+
test_files:
|
90
|
+
- spec/scoped_traversal/model_additions_spec.rb
|
91
|
+
- spec/spec_helper.rb
|