sorted 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -9
- data/VERSION +1 -1
- data/lib/sorted.rb +13 -5
- data/lib/sorted/action_view.rb +2 -2
- data/lib/sorted/active_record.rb +3 -1
- data/sorted.gemspec +2 -2
- data/spec/sorted_spec.rb +79 -62
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -30,13 +30,13 @@ You might want to roll your own link_to_sorted method to use jQuery ui css class
|
|
30
30
|
|
31
31
|
def link_to_sorted(name, order)
|
32
32
|
sorter = sorted(order)
|
33
|
-
css_class = case sorter.
|
34
|
-
when "asc"
|
35
|
-
"
|
36
|
-
when "desc"
|
37
|
-
"
|
38
|
-
|
39
|
-
"
|
33
|
+
css_class = case sorter.to_css
|
34
|
+
when "sorted asc"
|
35
|
+
"ui-icon ui-icon-triangle-1-n"
|
36
|
+
when "sorted desc"
|
37
|
+
"ui-icon ui-icon-triangle-1-s"
|
38
|
+
when "sorted"
|
39
|
+
"ui-icon ui-icon-carat-2-n-s"
|
40
40
|
end
|
41
41
|
link_to(name, sorter.params) + content_tag(:span, nil, {:class => css_class})
|
42
42
|
end
|
@@ -48,7 +48,7 @@ This gem works with rails 2.3.x but you will have to roll your own scope and vie
|
|
48
48
|
Here is the named scope for you model(s):
|
49
49
|
|
50
50
|
named_scope :sorted, lambda { |params|
|
51
|
-
|
51
|
+
params.merge(:order => Sorted::Sorter.new(params[:order], {:sort => params.delete(:sort)}).to_sql)
|
52
52
|
}
|
53
53
|
|
54
54
|
and the the application helper methods:
|
@@ -59,5 +59,5 @@ and the the application helper methods:
|
|
59
59
|
|
60
60
|
def link_to_sorted(name, order)
|
61
61
|
sorter = sorted(order)
|
62
|
-
link_to(name, sorter.params, {:class =>
|
62
|
+
link_to(name, sorter.params, {:class => sorter.to_css})
|
63
63
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.3
|
data/lib/sorted.rb
CHANGED
@@ -44,7 +44,11 @@ module Sorted
|
|
44
44
|
order_queue.select do |os|
|
45
45
|
sort_queue.flatten.include?(os[0])
|
46
46
|
end.each do |os|
|
47
|
-
|
47
|
+
if sort_queue[0, order_queue.size].flatten.include?(os[0])
|
48
|
+
@_array << [os[0], (case sort_queue.assoc(os[0])[1]; when "asc"; "desc"; when "desc"; "asc" end)]
|
49
|
+
else
|
50
|
+
@_array << [os[0], sort_queue.assoc(os[0])[1]]
|
51
|
+
end
|
48
52
|
end
|
49
53
|
order_queue.select do |o|
|
50
54
|
!@_array.flatten.include?(o[0])
|
@@ -79,14 +83,18 @@ module Sorted
|
|
79
83
|
def to_a
|
80
84
|
_array
|
81
85
|
end
|
82
|
-
|
83
|
-
def
|
84
|
-
|
86
|
+
|
87
|
+
def to_css
|
88
|
+
if sort_queue.flatten.include?(order_queue.first.first)
|
89
|
+
"sorted #{sort_queue.assoc(order_queue.first.first).last}"
|
90
|
+
else
|
91
|
+
"sorted"
|
92
|
+
end
|
85
93
|
end
|
86
94
|
|
87
95
|
def params
|
88
96
|
@params ||= {}
|
89
|
-
@params[:sort] = to_s
|
97
|
+
@params[:sort] = to_s
|
90
98
|
@params
|
91
99
|
end
|
92
100
|
|
data/lib/sorted/action_view.rb
CHANGED
@@ -8,8 +8,8 @@ module Sorted
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def link_to_sorted(name, order, options = {})
|
11
|
-
sorter
|
12
|
-
options[:class] = options[:class].nil? ?
|
11
|
+
sorter = sorted(order)
|
12
|
+
options[:class] = options[:class].nil? ? sorter.to_css : "#{options[:class]} #{sorter.to_css}"
|
13
13
|
link_to(name, sorter.params, options)
|
14
14
|
end
|
15
15
|
end
|
data/lib/sorted/active_record.rb
CHANGED
@@ -5,7 +5,9 @@ module Sorted
|
|
5
5
|
module ActiveRecord
|
6
6
|
def self.enable!
|
7
7
|
::ActiveRecord::Base.class_eval do
|
8
|
-
scope :sorted, lambda { |params|
|
8
|
+
scope :sorted, lambda { |params|
|
9
|
+
params.merge(:order => ::Sorted::Sorter.new(params[:order], {:sort => params.delete(:sort)}).to_sql)
|
10
|
+
}
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
data/sorted.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sorted}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Rufus Post"]
|
12
|
-
s.date = %q{2010-07-
|
12
|
+
s.date = %q{2010-07-31}
|
13
13
|
s.description = %q{lets you sort large data sets using view helpers and a scope}
|
14
14
|
s.email = %q{rufuspost@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/sorted_spec.rb
CHANGED
@@ -2,6 +2,68 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
require 'action_controller'
|
3
3
|
require 'sorted'
|
4
4
|
|
5
|
+
describe Sorted::Sorter, "parse methods" do
|
6
|
+
it "should return a nice array from the order sql" do
|
7
|
+
sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
8
|
+
sorter.order_queue.should == [["email", "asc"], ["phone", "asc"], ["name", "desc"]]
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return a nice array from the sort params" do
|
12
|
+
sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
13
|
+
sorter.sort_queue.should == [["email", "desc"], ["name", "desc"]]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow underscores and and full stops in" do
|
17
|
+
sorter = Sorted::Sorter.new('users.email ASC, users.phone_number DESC, assessments.name ASC', {:sort => "users.email_desc!users.first_name_desc"})
|
18
|
+
sorter.to_sql.should == "users.email DESC, users.first_name DESC, users.phone_number DESC, assessments.name ASC"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe Sorted::Sorter, "logic:" do
|
23
|
+
it "should not toggle the sort order and include any sql orders not in sort params" do
|
24
|
+
sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
25
|
+
sorter.to_a.should == [["email", "desc"], ["name", "desc"], ["phone", "asc"]]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should toggle the sort order and include any sql orders not in sort params" do
|
29
|
+
sorter = Sorted::Sorter.new("email DESC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
30
|
+
sorter.toggle.to_a.should == [["email", "asc"], ["name", "asc"], ["phone", "asc"]]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return an sql sort string" do
|
34
|
+
Sorted::Sorter.new(:email).to_sql.should == "email ASC"
|
35
|
+
Sorted::Sorter.new(:email, {:sort => "name_desc!email_desc"}).to_sql.should == "name DESC, email DESC"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should handle a large initial order string" do
|
39
|
+
sorter = Sorted::Sorter.new('email ASC, name DESC, phone ASC', {:sort => "email_desc!name_desc"})
|
40
|
+
sorter.to_sql.should == "email DESC, name DESC, phone ASC"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should handle a large initial order string" do
|
44
|
+
sorter = Sorted::Sorter.new('email ASC, phone DESC, name ASC', {:sort => "email_desc!name_desc"})
|
45
|
+
sorter.to_sql.should == "email DESC, name DESC, phone DESC"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should should fail becasue the sort order is incorect" do
|
49
|
+
sorter = Sorted::Sorter.new(:jsci_complete, {:sort => "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"})
|
50
|
+
sorter.toggle
|
51
|
+
sorter.to_s.should_not == "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return css base class for to_css if not in sort params" do
|
55
|
+
sorter = Sorted::Sorter.new(:email)
|
56
|
+
sorter.toggle
|
57
|
+
sorter.to_css.should == "sorted"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return css class for to_css" do
|
61
|
+
sorter = Sorted::Sorter.new(:email, {:sort => "email_desc"})
|
62
|
+
sorter.toggle
|
63
|
+
sorter.to_css.should == "sorted desc"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
5
67
|
describe Sorted::ActiveRecord do
|
6
68
|
before(:each) do
|
7
69
|
Sorted::ActiveRecord.enable!
|
@@ -38,56 +100,41 @@ describe Sorted::ActionView do
|
|
38
100
|
ActionView::Base.new.should respond_to(:sorted)
|
39
101
|
end
|
40
102
|
|
41
|
-
it "should return a nice hash from the order sql" do
|
42
|
-
sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
43
|
-
sorter.order_queue.should == [["email", "asc"], ["phone", "asc"], ["name", "desc"]]
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should return a nice hash from the sort params" do
|
47
|
-
sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
48
|
-
sorter.sort_queue.should == [["email", "desc"], ["name", "desc"]]
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should not toggle the sort order and include any sql orders not in sort params" do
|
52
|
-
sorter = Sorted::Sorter.new("email DESC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
53
|
-
sorter.to_hash.should == {"email" => "desc", "name" => "desc", "phone" => "asc"}
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should toggle the sort order and include any sql orders not in sort params" do
|
57
|
-
sorter = Sorted::Sorter.new("email DESC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
|
58
|
-
sorter.toggle.to_hash.should == {"email" => "asc", "name" => "asc", "phone" => "asc"}
|
59
|
-
end
|
60
|
-
|
61
103
|
it "should not change the direction of name using view helper" do
|
62
104
|
@controller.params = {:sort => "name_desc!email_asc"}
|
63
105
|
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
64
|
-
sorter.toggle.
|
106
|
+
sorter.toggle.to_a.should == [["email", "asc"], ["name", "desc"]]
|
65
107
|
end
|
66
108
|
|
67
109
|
it "should reverse email direction using view helper" do
|
68
110
|
@controller.params = {:sort => "email_asc!name_desc"}
|
69
111
|
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
70
|
-
sorter.toggle.
|
112
|
+
sorter.toggle.to_a.should == [["email", "desc"], ["name", "desc"]]
|
71
113
|
end
|
72
114
|
|
73
115
|
it "should reverse email direction using view helper" do
|
74
|
-
@controller.params = {:sort => "
|
116
|
+
@controller.params = {:sort => "email_desc!name_desc!phone_desc"}
|
75
117
|
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
76
|
-
sorter.toggle.params.should == {:sort => "email_asc!phone_desc
|
118
|
+
sorter.toggle.params.should == {:sort => "email_asc!name_desc!phone_desc"}
|
77
119
|
@controller.params = sorter.params
|
78
120
|
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
121
|
+
sorter.toggle.params.should == {:sort => "email_desc!name_desc!phone_desc"}
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should not reverse order when made first again" do
|
125
|
+
@controller.params = {:sort => "phone_desc!name_desc!email_desc"}
|
126
|
+
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
79
127
|
sorter.toggle.params.should == {:sort => "email_desc!phone_desc!name_desc"}
|
80
|
-
@controller.params = sorter.params
|
81
|
-
sorter = ActionView::Base.new([], {}, @controller).sorted(:phone)
|
82
|
-
sorter.toggle.params.should == {:sort => "phone_asc!email_desc!name_desc"}
|
83
128
|
sorter = ActionView::Base.new([], {}, @controller).sorted(:name)
|
84
|
-
sorter.toggle.params.should == {:sort => "
|
129
|
+
sorter.toggle.params.should == {:sort => "name_desc!phone_desc!email_desc"}
|
85
130
|
end
|
86
131
|
|
87
|
-
it "should
|
88
|
-
sorter =
|
89
|
-
sorter.toggle
|
90
|
-
|
132
|
+
it "should order correctly" do
|
133
|
+
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
134
|
+
sorter.toggle.params.should == {:sort => "email_asc"}
|
135
|
+
@controller.params = sorter.params
|
136
|
+
sorter = ActionView::Base.new([], {}, @controller).sorted(:name)
|
137
|
+
sorter.toggle.params.should == {:sort => "name_asc!email_asc"}
|
91
138
|
end
|
92
139
|
|
93
140
|
it "should return a hash of options for url builder with sorted query string" do
|
@@ -95,38 +142,8 @@ describe Sorted::ActionView do
|
|
95
142
|
ActionView::Base.new([], {}, @controller).sorted(:email).params.should == {:sort => "email_desc!name_desc", :page => 2}
|
96
143
|
end
|
97
144
|
|
98
|
-
it "should return an sql sort string" do
|
99
|
-
Sorted::Sorter.new(:email).to_sql.should == "email ASC"
|
100
|
-
Sorted::Sorter.new(:email, {:sort => "name_desc!email_desc"}).to_sql.should == "name DESC, email DESC"
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should handle a large initial order string" do
|
104
|
-
sorter = Sorted::Sorter.new('email ASC, name DESC, phone ASC', {:sort => "email_desc!name_desc"})
|
105
|
-
sorter.to_sql.should == "email DESC, name DESC, phone ASC"
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should handle a large initial order string" do
|
109
|
-
sorter = Sorted::Sorter.new('email ASC, phone DESC, name ASC', {:sort => "email_desc!name_desc"})
|
110
|
-
sorter.to_sql.should == "email DESC, name DESC, phone DESC"
|
111
|
-
end
|
112
|
-
|
113
145
|
it "should not die if params nil" do
|
114
146
|
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
115
147
|
sorter.params.should == {:sort => "email_asc"}
|
116
148
|
end
|
117
|
-
|
118
|
-
it "should have some roll your own methods" do
|
119
|
-
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
120
|
-
sorter.to_s.should == "email_asc"
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should return the order of the first attribute" do
|
124
|
-
sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
|
125
|
-
sorter.order_first.should == "asc"
|
126
|
-
end
|
127
|
-
|
128
|
-
it "should allow underscores and and full stops in" do
|
129
|
-
sorter = Sorted::Sorter.new('users.email ASC, users.phone_number DESC, assessments.name ASC', {:sort => "users.email_desc!users.first_name_desc"})
|
130
|
-
sorter.to_sql.should == "users.email DESC, users.first_name DESC, users.phone_number DESC, assessments.name ASC"
|
131
|
-
end
|
132
149
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 3
|
9
|
+
version: 0.3.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Rufus Post
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-07-
|
17
|
+
date: 2010-07-31 00:00:00 +10:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|