priority_order_scopes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +39 -0
- data/Rakefile +34 -0
- data/lib/priority_order_scopes.rb +70 -0
- metadata +74 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Nick Ragaz
|
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,39 @@
|
|
1
|
+
PriorityOrderScopes
|
2
|
+
===================
|
3
|
+
|
4
|
+
Flip through Active Record models in order, using next and previous scopes and instance methods.
|
5
|
+
|
6
|
+
Example
|
7
|
+
-------
|
8
|
+
|
9
|
+
create_table "jobs" do |t|
|
10
|
+
t.string "name"
|
11
|
+
t.datetime "started_at"
|
12
|
+
end
|
13
|
+
|
14
|
+
class Job < ActiveRecord::Base
|
15
|
+
include PriorityOrderScopes
|
16
|
+
|
17
|
+
priority_order_scopes "started_at"
|
18
|
+
end
|
19
|
+
|
20
|
+
Job.priority_order # => Job.order("started_at")
|
21
|
+
|
22
|
+
# by default, next and previous use the priority order (unless there
|
23
|
+
# is a default_scope with an order defined)
|
24
|
+
|
25
|
+
Job.next(Job.priority_order.first) # => the job that starts second
|
26
|
+
Job.previous(Job.priority_order.first) # => nil
|
27
|
+
Job.previous(second_job) # => Job.priority_order.first
|
28
|
+
|
29
|
+
# next and previous class methods respect the current scope
|
30
|
+
|
31
|
+
Job.order('name').next(Job.first) # => the job with the next name
|
32
|
+
|
33
|
+
# next and previous instance methods always use priority order
|
34
|
+
|
35
|
+
job = Job.priority_order.first
|
36
|
+
job.next # => the job that starts second
|
37
|
+
|
38
|
+
job = Job.order('name').first
|
39
|
+
job.previous # => the job that starts before the first job in alpha order
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'PriorityOrderScopes'
|
18
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task :default => :test
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module PriorityOrderScopes
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
# Include PriorityOrderScopes and call 'priority_order_scopes' with a column
|
6
|
+
# name (and optional direction) to create `priority_order`, `next` and
|
7
|
+
# `previous` scopes.
|
8
|
+
#
|
9
|
+
# `next and `previous` require a record to start from. They will use the
|
10
|
+
# current scope if passed as part of a chain (or if a default scope with
|
11
|
+
# an order clause is defined), or default to the priority order.
|
12
|
+
#
|
13
|
+
# You can also call instance.next or instance.previous directly. This will
|
14
|
+
# always use the default scope or the priority order.
|
15
|
+
def priority_order_scopes(col, dir=:asc)
|
16
|
+
@priority_order_column = col
|
17
|
+
@priority_order_direction = dir
|
18
|
+
|
19
|
+
class << self
|
20
|
+
define_method :priority_order do
|
21
|
+
reorder("#{table_name}.#{@priority_order_column} #{@priority_order_direction}")
|
22
|
+
end
|
23
|
+
|
24
|
+
define_method :next do |current|
|
25
|
+
return unless current
|
26
|
+
|
27
|
+
# use the current scope's order clause, if any, or the priority order
|
28
|
+
order = scoped.order_clauses[0] || priority_order.order_clauses[0]
|
29
|
+
# turn ["last_name DESC, first_name", "id"] into "last_name"
|
30
|
+
order = order.split(", ")[0]
|
31
|
+
operator = order =~ /desc/i ? '<' : '>'
|
32
|
+
order_column = order.split(" ")[0].split(".").last
|
33
|
+
|
34
|
+
where(
|
35
|
+
"#{table_name}.#{order_column} #{operator} ?",
|
36
|
+
current.send(order_column)
|
37
|
+
).
|
38
|
+
limit(1)
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method :previous do |current|
|
42
|
+
return unless current
|
43
|
+
|
44
|
+
# use the current scope's order clause, if any, or the priority order
|
45
|
+
order = scoped.order_clauses[0] || priority_order.order_clauses[0]
|
46
|
+
# turn ["last_name DESC, first_name", "id"] into "last_name"
|
47
|
+
order = order.split(", ")[0]
|
48
|
+
|
49
|
+
operator = order =~ /desc/i ? '>' : '<'
|
50
|
+
order_column = order.split(" ")[0].split(".").last
|
51
|
+
|
52
|
+
where(
|
53
|
+
"#{table_name}.#{order_column} #{operator} ?",
|
54
|
+
current.send(order_column)
|
55
|
+
).
|
56
|
+
reverse_order.
|
57
|
+
limit(1)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
define_method :next do
|
62
|
+
self.class.priority_order.next(self).first
|
63
|
+
end
|
64
|
+
|
65
|
+
define_method :previous do
|
66
|
+
self.class.priority_order.previous(self).first
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: priority_order_scopes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nick Ragaz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-06-29 00:00:00.000000000 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
requirement: &2161869500 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2161869500
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: sqlite3
|
28
|
+
requirement: &2161869100 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2161869100
|
37
|
+
description: Flip through Active Record models in order using next and previous scopes
|
38
|
+
and instance methods.
|
39
|
+
email: nick.ragaz@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- lib/priority_order_scopes.rb
|
45
|
+
- MIT-LICENSE
|
46
|
+
- Rakefile
|
47
|
+
- README.md
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/nragaz/priority_order_scopes
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.6.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Flip through Active Record models in order using next and previous scopes
|
73
|
+
and instance methods.
|
74
|
+
test_files: []
|