next_record 0.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/lib/extensions/active_record_extension.rb +45 -0
- data/lib/next_record.rb +3 -0
- data/lib/next_record/version.rb +3 -0
- data/next_record.gemspec +24 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 72501e3a6bc75d5de540d3c273c315c156f30e88
|
4
|
+
data.tar.gz: 7103c2ab609c090d7cd1d7394c53aafd042ac6d4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d5e354ed83b525fb2f0e380029e8916e532f39eadb3fbcb554869b1230230449dd15fad65b43cc80498ba2f279ecb4ac6ab3cdd4ff637ce88e50123294d118a1
|
7
|
+
data.tar.gz: e46a4443529c17980e0aa17f2fb95d28bf58f4568e1844623eeeef0c0a89c71a28f9233fe7452fd88be06a5678b6786744db920c35c1c907d6fb94e9e18b4d76
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Helge Rausch
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# NextRecord
|
2
|
+
|
3
|
+
Extends ActiveRecord by adding `next` and `previous` instance methods, returning
|
4
|
+
the next and previous record according to a predefined order.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'next_record'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install next_record
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Define an order in your model, using `next_record_order`. It takes the same
|
23
|
+
arguments as ActiveRecord's `order` method:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Person < ActiveRecord::Base
|
27
|
+
next_record_order 'lower(trim(last_name))', :id
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Find the previous and next records:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
e = Person.first
|
35
|
+
e.next # => next record according to the defined order
|
36
|
+
e.previous # => previous record according to the defined order
|
37
|
+
```
|
38
|
+
|
39
|
+
## TODO
|
40
|
+
|
41
|
+
* Write tests...
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module NextRecord
|
2
|
+
module ActiveRecordExtension
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
def next
|
8
|
+
next_record
|
9
|
+
end
|
10
|
+
|
11
|
+
def previous
|
12
|
+
next_record(-1)
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
attr_reader :next_record_options
|
17
|
+
|
18
|
+
def next_record_order(*args)
|
19
|
+
args.flatten!
|
20
|
+
@next_record_options = (args.last.is_a?(Hash) ? args.pop : {})
|
21
|
+
define_singleton_method(:next_record_ordered) do
|
22
|
+
order(args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def next_record(offset=1)
|
30
|
+
relation = self.class.next_record_ordered
|
31
|
+
scope = self.class.next_record_options[:scope]
|
32
|
+
if scope
|
33
|
+
relation = relation.where(scope => send(scope))
|
34
|
+
end
|
35
|
+
ids = relation.pluck(:id)
|
36
|
+
index = ids.index(id) + offset
|
37
|
+
return nil if index < 0 || index >= ids.size
|
38
|
+
next_id = ids[index]
|
39
|
+
relation.where(id: next_id).first
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ActiveRecord::Base.send(:include, NextRecord::ActiveRecordExtension)
|
45
|
+
|
data/lib/next_record.rb
ADDED
data/next_record.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'next_record/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "next_record"
|
8
|
+
spec.version = NextRecord::VERSION
|
9
|
+
spec.authors = ["Helge Rausch"]
|
10
|
+
spec.email = ["helge@rausch.io"]
|
11
|
+
spec.description = %q{adds `next` and `previous` methods to your ActiveRecord instances}
|
12
|
+
spec.summary = %q{adds `next` and `previous` methods to your ActiveRecord instances}
|
13
|
+
spec.homepage = "https://github.com/mobileeventguide/next_record"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", "~> 3.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: next_record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Helge Rausch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: adds `next` and `previous` methods to your ActiveRecord instances
|
56
|
+
email:
|
57
|
+
- helge@rausch.io
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/extensions/active_record_extension.rb
|
68
|
+
- lib/next_record.rb
|
69
|
+
- lib/next_record/version.rb
|
70
|
+
- next_record.gemspec
|
71
|
+
homepage: https://github.com/mobileeventguide/next_record
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.0.3
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: adds `next` and `previous` methods to your ActiveRecord instances
|
95
|
+
test_files: []
|