yfactorial-utility_scopes 0.2.1 → 0.2.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.
- data/CHANGELOG +4 -0
- data/README.textile +11 -5
- data/Rakefile +6 -1
- data/lib/utility_scopes/ordered.rb +42 -13
- data/spec/ordered_spec.rb +34 -2
- data/spec/spec_helper.rb +1 -1
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README.textile
CHANGED
@@ -110,11 +110,17 @@ h3. Ordered
|
|
110
110
|
_Note: the @ordered@ scope cannot be chained with any other @order@ clauses_
|
111
111
|
|
112
112
|
@ordered@ lets you dynamically specify the ordering of your result set. If no
|
113
|
-
arguments are given it will default to @created_at DESC
|
114
|
-
|
115
|
-
|
116
|
-
Article.ordered
|
117
|
-
Article.ordered(
|
113
|
+
arguments are given it will default to @created_at DESC@. (@ordered@ is also
|
114
|
+
available as @order_by@ and @sort_by@)
|
115
|
+
|
116
|
+
Article.ordered # Get all articles ordered by "created_at DESC"
|
117
|
+
Article.ordered(:id) # Get all articles ordered by "id"
|
118
|
+
Article.ordered("rank ASC") # Get all articles ordered by "rank ASC"
|
119
|
+
Article.order_by(:id) # order_by and sort_by are alias to ordered
|
120
|
+
Article.order_by([:id, :desc], :popularity) # can take a two-element array as parameter
|
121
|
+
Article.sort_by(:id => :desc, :popularity => :asc) # can take a hash as parameter
|
122
|
+
# only available for jruby/ruby 1.9
|
123
|
+
Article.order_by_id # can be set as a sentence
|
118
124
|
|
119
125
|
If you would like to specify a different default sort order you can do so on a per class basis
|
120
126
|
using @ordered_by@:
|
data/Rakefile
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
4
5
|
|
5
|
-
|
6
|
+
desc 'Run the specs'
|
7
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
8
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
9
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
10
|
+
end
|
6
11
|
|
7
12
|
desc 'Generate RDoc documentation.'
|
8
13
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
@@ -2,20 +2,32 @@
|
|
2
2
|
module UtilityScopes
|
3
3
|
module Ordered
|
4
4
|
|
5
|
-
def self.included(
|
6
|
-
|
7
|
-
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
|
8
|
+
base.class_eval do
|
8
9
|
# Provide an ordered scope
|
9
|
-
named_scope
|
10
|
-
{ :order =>
|
11
|
-
|
10
|
+
named_scope(:ordered, lambda { |*order|
|
11
|
+
{ :order => case
|
12
|
+
when order.empty?
|
13
|
+
self.default_ordering
|
14
|
+
# allow hash support for jruby or ruby 1.9
|
15
|
+
when order.size == 1 && order[0].is_a?(Hash) && (PLATFORM=~/java/ || RUBY_VERSION=~/1\.9.*/)
|
16
|
+
order.first.collect{|(k,v)| "#{k} #{v.to_s.upcase}"}.join(', ')
|
17
|
+
else
|
18
|
+
order.collect{|e| e.is_a?(Array) ? "#{e[0]} #{e[1].to_s.upcase}" : e}.join(', ')
|
19
|
+
end
|
20
|
+
}
|
21
|
+
})
|
12
22
|
|
13
|
-
# Set the default order
|
14
23
|
class << self
|
24
|
+
# Set alias order_by
|
25
|
+
alias_method :order_by, :ordered
|
26
|
+
# Set alias sort_by
|
27
|
+
alias_method :sort_by, :ordered
|
28
|
+
# Set the default order
|
15
29
|
define_method(:default_ordering) { 'created_at DESC' }
|
16
30
|
end
|
17
|
-
|
18
|
-
extend ClassMethods
|
19
31
|
end
|
20
32
|
end
|
21
33
|
|
@@ -33,18 +45,35 @@ module UtilityScopes
|
|
33
45
|
# Article.default_ordering #=> "published_at DESC"
|
34
46
|
#
|
35
47
|
def ordered_by(clause)
|
36
|
-
|
37
48
|
# Override named scope on AR::Base so we can access default_ordering
|
38
49
|
# on subclass
|
39
|
-
named_scope
|
40
|
-
{ :order =>
|
41
|
-
|
50
|
+
named_scope(:ordered, lambda { |*order|
|
51
|
+
{ :order => case
|
52
|
+
when order.empty?
|
53
|
+
self.default_ordering
|
54
|
+
# allow hash support for jruby or ruby 1.9
|
55
|
+
when order.size == 1 && order[0].is_a?(Hash) && (PLATFORM=~/java/ || RUBY_VERSION=~/1\.9.*/)
|
56
|
+
order.first.collect{|(k,v)| "#{k} #{v.to_s.upcase}"}.join(', ')
|
57
|
+
else
|
58
|
+
order.collect{|e| e.is_a?(Array) ? "#{e[0]} #{e[1].to_s.upcase}" : e}.join(', ')
|
59
|
+
end
|
60
|
+
}
|
61
|
+
})
|
42
62
|
|
43
63
|
metaclass.instance_eval do
|
44
64
|
define_method(:default_ordering) { clause }
|
45
65
|
end
|
46
66
|
end
|
47
67
|
|
68
|
+
def method_missing(method, *args, &block)
|
69
|
+
col = method.to_s.match(/^(order_by_|sort_by_)(.*)$/)[2] rescue false
|
70
|
+
if col && self.columns.collect{ |c| c.name }.include?(col)
|
71
|
+
return self.order_by(col, *args, &block)
|
72
|
+
else
|
73
|
+
super
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
48
77
|
private
|
49
78
|
|
50
79
|
def metaclass; class << self; self end; end
|
data/spec/ordered_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), *%w[abstract_spec])
|
|
2
2
|
|
3
3
|
describe "Ordered scope" do
|
4
4
|
|
5
|
-
before do
|
5
|
+
before(:each) do
|
6
6
|
uses_fixture(:article)
|
7
7
|
end
|
8
8
|
|
@@ -15,6 +15,38 @@ describe "Ordered scope" do
|
|
15
15
|
Article.ordered('created_at ASC').proxy_options.should == {:order => 'created_at ASC'}
|
16
16
|
end
|
17
17
|
|
18
|
+
it "should allow the order to use arrays as arg" do
|
19
|
+
Article.ordered([:popularity, :asc], [:second_param, :desc]).proxy_options.should == {:order => 'popularity ASC, second_param DESC'}
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should allow the order hash as arg" do
|
23
|
+
if (PLATFORM=~/java/ || RUBY_VERSION=~/1\.9.*/)
|
24
|
+
Article.ordered(:popularity => :asc, :second_param => :desc).proxy_options.should == {:order => 'popularity ASC, second_param DESC'}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should allow the order with several args" do
|
29
|
+
Article.ordered(:popularity, :updated_at).proxy_options.should == {:order => 'popularity, updated_at'}
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should sort by column popularity when calling order_by_popularity" do
|
33
|
+
require 'ostruct'
|
34
|
+
columns = [OpenStruct.new({:name => 'popularity'})]
|
35
|
+
Article.stub!(:columns).and_return(columns)
|
36
|
+
Article.order_by_popularity.proxy_options.should == {:order => 'popularity'}
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should raise an error when column does not exist" do
|
40
|
+
require 'ostruct'
|
41
|
+
columns = [OpenStruct.new({:name => 'popularity'})]
|
42
|
+
Article.stub!(:columns).and_return(columns)
|
43
|
+
lambda{ Article.order_by_unknown_columns }.should raise_error
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should have an alias" do
|
47
|
+
Article.order_by([:popularity, :asc]).proxy_options.should == {:order => 'popularity ASC'}
|
48
|
+
end
|
49
|
+
|
18
50
|
it "should allow the default to be overidden by using ordered_by" do
|
19
51
|
Article.ordered_by 'published_at DESC'
|
20
52
|
Article.default_ordering.should == 'published_at DESC'
|
@@ -27,6 +59,6 @@ describe "Ordered scope" do
|
|
27
59
|
end
|
28
60
|
|
29
61
|
it "should be able to handle symbol order criteria" do
|
30
|
-
Article.ordered(:id).proxy_options.should == { :order =>
|
62
|
+
Article.ordered(:id).proxy_options.should == { :order => 'id' }
|
31
63
|
end
|
32
64
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yfactorial-utility_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Daigle
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-10-28 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|