sorted 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,8 @@ Sorted is a simple object that will take an sql order string and a url sort stri
4
4
 
5
5
  Using Rails master it will create a `sorted` scope and a two view helpers
6
6
 
7
+ gem install sorted
8
+
7
9
  == Example
8
10
 
9
11
  === The view:
@@ -29,7 +31,8 @@ This will initially sort by email ascending:
29
31
  You might want to roll your own link_to_sorted method to use jQuery ui css classes for example, all you need is the sorted object.
30
32
 
31
33
  def link_to_sorted(name, order)
32
- sorter = sorted(order)
34
+ sorter = sorted(order)
35
+ name = name.nil? ? "" : name.to_s
33
36
  css_class = case sorter.to_css
34
37
  when "sorted asc"
35
38
  "ui-icon ui-icon-triangle-1-n"
@@ -38,7 +41,7 @@ You might want to roll your own link_to_sorted method to use jQuery ui css class
38
41
  when "sorted"
39
42
  "ui-icon ui-icon-carat-2-n-s"
40
43
  end
41
- link_to(name, sorter.params) + content_tag(:span, nil, {:class => css_class})
44
+ link_to(content_tag(:span, nil, {:class => css_class}) + name, sorter.params)
42
45
  end
43
46
 
44
47
  == Rails 2.3.x
@@ -48,7 +51,7 @@ This gem works with rails 2.3.x but you will have to roll your own scope and vie
48
51
  Here is the named scope for your model(s):
49
52
 
50
53
  named_scope :sorted, lambda { |params|
51
- params.merge(:order => Sorted::Sorter.new(params[:order], {:sort => params.delete(:sort)}).to_sql)
54
+ { :order => Sorted::Sorter.new(params[:order], {:sort => params[:sort]}).to_sql }
52
55
  }
53
56
 
54
57
  and the the application helper methods:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.3.5
@@ -1,137 +1,7 @@
1
- module Sorted
2
- class Sorter
3
- def initialize(*args)
4
- parse_order(args[0])
5
- unless args[1].nil?
6
- @params = args[1]
7
- unless @params[:sort].nil?
8
- parse_sort(@params[:sort])
9
- end
10
- end
11
- self
12
- end
13
-
14
- def parse_sort(sort)
15
- sort.split(/!/).each do |s|
16
- if ps = parse_query(s)
17
- sort_queue << ps
18
- end
19
- end
20
- end
21
-
22
- def parse_order(order)
23
- order.to_s.split(/,/).each do |o|
24
- if po = parse_sql(o)
25
- order_queue << po
26
- end
27
- end
28
- end
29
-
30
- def parse_query(sort)
31
- if m = sort.match(/([a-zA-Z0-9._]+)_(asc|desc)$/)
32
- [m[1],m[2]]
33
- end
34
- end
35
-
36
- def parse_sql(sql)
37
- if m = sql.match(/(([a-zA-Z._:]+)\s([asc|ASC|desc|DESC]+)|[a-zA-Z._:]+)/)
38
- [(m[2].nil? ? m[1] : m[2]),(m[3].nil? ? "asc" : m[3].downcase)]
39
- end
40
- end
41
-
42
- def toggle
43
- @_array = []
44
- sorts = sort_queue.transpose.first
45
- orders = order_queue.transpose.first
46
- unless sorts.nil?
47
- if orders == sorts.take(orders.size)
48
- orders.select do |order|
49
- sorts.include?(order)
50
- end.each do |order|
51
- @_array << [order, (case sort_queue.assoc(order).last; when "asc"; "desc"; when "desc"; "asc" end)]
52
- end
53
- else
54
- orders.select do |order|
55
- sorts.include?(order)
56
- end.each do |order|
57
- @_array << [order, sort_queue.assoc(order).last]
58
- end
59
- end
60
- sorts.select do |sort|
61
- orders.include?(sort) && !@_array.flatten.include?(sort)
62
- end.each do |sort|
63
- @_array << [sort, sort_queue.assoc(sort).last]
64
- end
65
- end
66
- order_queue.select do |order|
67
- !@_array.flatten.include?(order[0])
68
- end.each do |order|
69
- @_array << order
70
- end
71
- sort_queue.select do |sort|
72
- !@_array.flatten.include?(sort[0])
73
- end.each do |sort|
74
- @_array << sort
75
- end
76
- self
77
- end
78
-
79
- def reset
80
- @_array = default
81
- self
82
- end
83
-
84
- def to_hash
85
- _array.inject({}){|h,a| h.merge(Hash[a[0],a[1]]) }
86
- end
87
-
88
- def to_sql
89
- _array.map{|a| "#{a[0]} #{a[1].upcase}"}.join(', ')
90
- end
1
+ require 'sorted/sorter'
2
+ require 'sorted/toggler'
91
3
 
92
- def to_s
93
- _array.map{|a| a.join('_')}.join('!')
94
- end
95
-
96
- def to_a
97
- _array
98
- end
99
-
100
- def to_css
101
- if sort_queue.flatten.include?(order_queue.first.first)
102
- "sorted #{sort_queue.assoc(order_queue.first.first).last}"
103
- else
104
- "sorted"
105
- end
106
- end
107
-
108
- def params
109
- @params ||= {}
110
- @params[:sort] = to_s
111
- @params
112
- end
113
-
114
- def order_queue
115
- @order_queue ||= []
116
- end
117
-
118
- def sort_queue
119
- @sort_queue ||= []
120
- end
121
-
122
- private
123
- def default
124
- sq = sort_queue.dup
125
- order_queue.each do |o|
126
- sq << o unless sq.flatten.include?(o[0])
127
- end
128
- sq
129
- end
130
-
131
- def _array
132
- @_array ||= default
133
- end
134
- end
4
+ module Sorted
135
5
  end
136
6
 
137
7
  if defined?(::Rails::Railtie)
@@ -0,0 +1,16 @@
1
+ require 'active_record'
2
+ require 'sorted'
3
+
4
+ module Sorted
5
+ module Finders
6
+ module ActiveRecord
7
+ def self.enable!
8
+ ::ActiveRecord::Base.class_eval do
9
+ def self.sorted(params)
10
+ order(::Sorted::Sorter.new(params[:order], :sort => params[:sort]).to_sql)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -3,13 +3,15 @@ require 'sorted'
3
3
  module Sorted
4
4
  class Railtie < Rails::Railtie
5
5
  initializer "sorted.active_record" do |app|
6
- require 'sorted/active_record'
7
- Sorted::ActiveRecord.enable!
6
+ if defined? ::ActiveRecord
7
+ require 'sorted/finders/active_record'
8
+ Sorted::Finders::ActiveRecord.enable!
9
+ end
8
10
  end
9
11
 
10
12
  initializer "sorted.action_view" do |app|
11
- require 'sorted/action_view'
12
- ::ActionView::Base.send(:include, Sorted::ActionView)
13
+ require 'sorted/view_helpers/action_view'
14
+ ::ActionView::Base.send(:include, Sorted::ViewHelpers::ActionView)
13
15
  end
14
16
  end
15
17
  end
@@ -0,0 +1,103 @@
1
+ module Sorted
2
+ class Sorter
3
+ def initialize(*args)
4
+ parse_order(args[0])
5
+ if args[1].is_a?(Hash)
6
+ @params = args[1]
7
+ unless @params[:sort].nil?
8
+ parse_sort @params[:sort]
9
+ end
10
+ end
11
+ self
12
+ end
13
+
14
+ def parse_sort(sort_string)
15
+ sort_string.split(/!/).each do |sort|
16
+ if parsed = parse_query(sort)
17
+ sorts << parsed
18
+ end
19
+ end
20
+ end
21
+
22
+ def parse_order(order_string_or_symbol)
23
+ order_string_or_symbol.to_s.split(/,/).each do |order|
24
+ if parsed = parse_sql(order)
25
+ orders << parsed
26
+ end
27
+ end
28
+ end
29
+
30
+ def parse_query(sort)
31
+ if m = sort.match(/([a-zA-Z0-9._]+)_(asc|desc)$/)
32
+ [m[1],m[2]]
33
+ end
34
+ end
35
+
36
+ def parse_sql(order)
37
+ if m = order.match(/(([a-zA-Z._:]+)\s([asc|ASC|desc|DESC]+)|[a-zA-Z._:]+)/)
38
+ [(m[2].nil? ? m[1] : m[2]),(m[3].nil? ? "asc" : m[3].downcase)]
39
+ end
40
+ end
41
+
42
+ def toggle
43
+ @array = Toggler.new(orders, sorts).to_a
44
+ self
45
+ end
46
+
47
+ def reset
48
+ @array = default
49
+ self
50
+ end
51
+
52
+ def to_hash
53
+ array.inject({}){|h,a| h.merge(Hash[a[0],a[1]])}
54
+ end
55
+
56
+ def to_sql
57
+ array.map{|a| "#{a[0]} #{a[1].upcase}"}.join(', ')
58
+ end
59
+
60
+ def to_s
61
+ array.map{|a| a.join('_')}.join('!')
62
+ end
63
+
64
+ def to_a
65
+ array
66
+ end
67
+
68
+ def to_css
69
+ if sorts.flatten.include?(orders[0][0])
70
+ "sorted #{sorts.assoc(orders[0][0]).last}"
71
+ else
72
+ "sorted"
73
+ end
74
+ end
75
+
76
+ def params
77
+ @params ||= {}
78
+ @params[:sort] = to_s
79
+ @params
80
+ end
81
+
82
+ def orders
83
+ @orders ||= []
84
+ end
85
+
86
+ def sorts
87
+ @sorts ||= []
88
+ end
89
+
90
+ private
91
+ def default
92
+ sorts_new = sorts.dup
93
+ orders.each do |order|
94
+ sorts_new << order unless sorts_new.flatten.include?(order[0])
95
+ end
96
+ sorts_new
97
+ end
98
+
99
+ def array
100
+ @array ||= default
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,55 @@
1
+ module Sorted
2
+ class Toggler
3
+ def initialize(orders, sorts)
4
+ @array = []
5
+ @sorts = sorts
6
+ @orders = orders
7
+ @sort_keys = sorts.transpose.first
8
+ @order_keys = orders.transpose.first
9
+ toggle_sorts unless @sort_keys.nil?
10
+ add_remaining_orders
11
+ add_remaining_sorts
12
+ end
13
+
14
+ def to_a
15
+ @array
16
+ end
17
+
18
+ def toggle_sorts
19
+ if @order_keys == @sort_keys.take(@order_keys.size)
20
+ @order_keys.select do |order|
21
+ @sort_keys.include?(order)
22
+ end.each do |order|
23
+ @array << [order, (case @sorts.assoc(order).last; when "asc"; "desc"; when "desc"; "asc" end)]
24
+ end
25
+ else
26
+ @order_keys.select do |order|
27
+ @sort_keys.include?(order)
28
+ end.each do |order|
29
+ @array << [order, @sorts.assoc(order).last]
30
+ end
31
+ end
32
+ @sort_keys.select do |sort|
33
+ @order_keys.include?(sort) && !@array.flatten.include?(sort)
34
+ end.each do |sort|
35
+ @array << [sort, @sorts.assoc(sort).last]
36
+ end
37
+ end
38
+
39
+ def add_remaining_orders
40
+ @orders.select do |order|
41
+ !@array.flatten.include?(order[0])
42
+ end.each do |order|
43
+ @array << order
44
+ end
45
+ end
46
+
47
+ def add_remaining_sorts
48
+ @sorts.select do |sort|
49
+ !@array.flatten.include?(sort[0])
50
+ end.each do |sort|
51
+ @array << sort
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ require 'action_view'
2
+ require 'sorted'
3
+
4
+ module Sorted
5
+ module ViewHelpers
6
+ module ActionView
7
+ def sorted(order)
8
+ ::Sorted::Sorter.new(order, (request.get? && !params.nil?) ? params.dup : nil).toggle
9
+ end
10
+
11
+ def link_to_sorted(name, order, options = {})
12
+ sorter = sorted(order)
13
+ name = name.nil? ? "" : name.to_s
14
+ options[:class] = options[:class].nil? ? sorter.to_css : "#{options[:class]} #{sorter.to_css}"
15
+ link_to(name, sorter.params, options)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sorted}
8
- s.version = "0.3.4"
8
+ s.version = "0.3.5"
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-08-09}
12
+ s.date = %q{2010-09-10}
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 = [
@@ -24,12 +24,16 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/sorted.rb",
27
- "lib/sorted/action_view.rb",
28
- "lib/sorted/active_record.rb",
27
+ "lib/sorted/finders/active_record.rb",
29
28
  "lib/sorted/railtie.rb",
29
+ "lib/sorted/sorter.rb",
30
+ "lib/sorted/toggler.rb",
31
+ "lib/sorted/view_helpers/action_view.rb",
30
32
  "sorted.gemspec",
31
33
  "spec/sorted_spec.rb",
32
- "spec/spec_helper.rb"
34
+ "spec/sorter_spec.rb",
35
+ "spec/spec_helper.rb",
36
+ "spec/toggler_spec.rb"
33
37
  ]
34
38
  s.homepage = %q{http://github.com/mynameisrufus/sorted}
35
39
  s.rdoc_options = ["--charset=UTF-8"]
@@ -38,6 +42,8 @@ Gem::Specification.new do |s|
38
42
  s.summary = %q{sort a table}
39
43
  s.test_files = [
40
44
  "spec/spec_helper.rb",
45
+ "spec/sorter_spec.rb",
46
+ "spec/toggler_spec.rb",
41
47
  "spec/sorted_spec.rb"
42
48
  ]
43
49
 
@@ -2,85 +2,9 @@ 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, full stops and colons in" do
17
- sorter = Sorted::Sorter.new('users.email ASC, users.phone_number DESC, assessments.name ASC, assessments.number_as_string::BigInt', {: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, assessments.number_as_string::BigInt 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", "desc"], ["name", "desc"], ["phone", "asc"]]
31
- end
32
-
33
- it "should toggle the sort order and include any sql orders not in sort params" do
34
- sorter = Sorted::Sorter.new("email DESC, phone ASC, name DESC", {:sort => "mobile_asc!email_desc!phone_asc!name_desc"})
35
- sorter.toggle.to_a.should == [["email", "desc"], ["phone", "asc"], ["name", "desc"], ["mobile", "asc"]]
36
- end
37
-
38
- it "should return an sql sort string" do
39
- Sorted::Sorter.new(:email).to_sql.should == "email ASC"
40
- Sorted::Sorter.new(:email, {:sort => "name_desc!email_desc"}).to_sql.should == "name DESC, email DESC"
41
- end
42
-
43
- it "should handle a large initial order string" do
44
- sorter = Sorted::Sorter.new('email ASC, name DESC, phone ASC', {:sort => "email_desc!name_desc"})
45
- sorter.to_sql.should == "email DESC, name DESC, phone ASC"
46
- end
47
-
48
- it "should handle a large initial order string" do
49
- sorter = Sorted::Sorter.new('email ASC, phone DESC, name ASC', {:sort => "email_desc!name_desc"})
50
- sorter.to_sql.should == "email DESC, name DESC, phone DESC"
51
- end
52
-
53
- it "should should fail becasue the sort order is incorect" do
54
- sorter = Sorted::Sorter.new(:jsci_complete, {:sort => "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"})
55
- sorter.toggle
56
- sorter.to_s.should_not == "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"
57
- end
58
-
59
- it "should return css base class for to_css if not in sort params" do
60
- sorter = Sorted::Sorter.new(:email)
61
- sorter.toggle
62
- sorter.to_css.should == "sorted"
63
- end
64
-
65
- it "should return css class for to_css" do
66
- sorter = Sorted::Sorter.new(:email, {:sort => "email_desc"})
67
- sorter.toggle
68
- sorter.to_css.should == "sorted desc"
69
- end
70
-
71
- it "should toggle two order params at once" do
72
- first = Sorted::Sorter.new("email ASC, phone ASC")
73
- first.toggle.to_a.should == [["email", "asc"],["phone", "asc"]]
74
- second = Sorted::Sorter.new(:name, {:sort => first.to_s})
75
- second.toggle.to_a.should == [["name", "asc"], ["email", "asc"],["phone", "asc"]]
76
- third = Sorted::Sorter.new("email ASC, phone ASC", {:sort => second.to_s})
77
- third.toggle.to_a.should == [["email", "asc"],["phone", "asc"], ["name", "asc"]]
78
- end
79
- end
80
-
81
- describe Sorted::ActiveRecord do
5
+ describe Sorted::Finders::ActiveRecord do
82
6
  before(:each) do
83
- Sorted::ActiveRecord.enable!
7
+ Sorted::Finders::ActiveRecord.enable!
84
8
  end
85
9
 
86
10
  it "should integrate with ActiveRecord::Base" do
@@ -88,7 +12,7 @@ describe Sorted::ActiveRecord do
88
12
  end
89
13
  end
90
14
 
91
- describe Sorted::ActionView do
15
+ describe Sorted::ViewHelpers::ActionView do
92
16
  before(:each) do
93
17
  class TestController
94
18
  def params
@@ -107,13 +31,17 @@ describe Sorted::ActionView do
107
31
  def get?; true end
108
32
  end
109
33
  @controller = TestController.new
110
- ActionView::Base.send(:include, Sorted::ActionView)
34
+ ActionView::Base.send(:include, Sorted::ViewHelpers::ActionView)
111
35
  end
112
36
 
113
37
  it "should integrate with ActionView::Base" do
114
38
  ActionView::Base.new.should respond_to(:sorted)
115
39
  end
116
40
 
41
+ it "should integrate with ActionView::Base" do
42
+ ActionView::Base.new.should respond_to(:link_to_sorted)
43
+ end
44
+
117
45
  it "should not change the direction of name using view helper" do
118
46
  @controller.params = {:sort => "name_desc!email_asc"}
119
47
  sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'action_controller'
3
+ require 'sorted'
4
+
5
+ describe Sorted::Sorter, "parse methods" do
6
+ it "should not die if pased dumb things" do
7
+ lambda { Sorted::Sorter.new(false, Time.now).should_not }.should_not raise_error
8
+ end
9
+
10
+ it "should return a nice array from the order sql" do
11
+ sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
12
+ sorter.orders.should == [["email", "asc"], ["phone", "asc"], ["name", "desc"]]
13
+ end
14
+
15
+ it "should return a nice array from the sort params" do
16
+ sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
17
+ sorter.sorts.should == [["email", "desc"], ["name", "desc"]]
18
+ end
19
+
20
+ it "should allow underscores, full stops and colons in" do
21
+ sorter = Sorted::Sorter.new('users.email ASC, users.phone_number DESC, assessments.name ASC, assessments.number_as_string::BigInt', {:sort => "users.email_desc!users.first_name_desc"})
22
+ sorter.to_sql.should == "users.email DESC, users.first_name DESC, users.phone_number DESC, assessments.name ASC, assessments.number_as_string::BigInt ASC"
23
+ end
24
+ end
25
+
26
+ describe Sorted::Sorter, "logic:" do
27
+ it "should not toggle the sort order and include any sql orders not in sort params" do
28
+ sorter = Sorted::Sorter.new("email ASC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
29
+ sorter.to_a.should == [["email", "desc"], ["name", "desc"], ["phone", "asc"]]
30
+ end
31
+
32
+ it "should return an sql sort string" do
33
+ Sorted::Sorter.new(:email).to_sql.should == "email ASC"
34
+ Sorted::Sorter.new(:email, {:sort => "name_desc!email_desc"}).to_sql.should == "name DESC, email DESC"
35
+ end
36
+
37
+ it "should handle a large initial order string" do
38
+ sorter = Sorted::Sorter.new('email ASC, name DESC, phone ASC', {:sort => "email_desc!name_desc"})
39
+ sorter.to_sql.should == "email DESC, name DESC, phone ASC"
40
+ end
41
+
42
+ it "should handle a large initial order string" do
43
+ sorter = Sorted::Sorter.new('email ASC, phone DESC, name ASC', {:sort => "email_desc!name_desc"})
44
+ sorter.to_sql.should == "email DESC, name DESC, phone DESC"
45
+ end
46
+ end
47
+
48
+ describe Sorted::Sorter, "to_css" do
49
+ it "should return css base class for to_css if not in sort params" do
50
+ sorter = Sorted::Sorter.new(:email)
51
+ sorter.to_css.should == "sorted"
52
+ end
53
+
54
+ it "should return css class for to_css" do
55
+ sorter = Sorted::Sorter.new(:email, {:sort => "email_desc"})
56
+ sorter.to_css.should == "sorted desc"
57
+ end
58
+ end
@@ -1,6 +1,6 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'sorted/active_record'
4
- require 'sorted/action_view'
3
+ require 'sorted/finders/active_record'
4
+ require 'sorted/view_helpers/action_view'
5
5
  require 'rspec'
6
6
  require 'rspec/autorun'
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'action_controller'
3
+ require 'sorted'
4
+
5
+ describe Sorted::Toggler, "toggle sorts" do
6
+ it "should should fail becasue the sort order is incorect" do
7
+ sorter = Sorted::Sorter.new(:jsci_complete, {:sort => "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"})
8
+ sorter.toggle
9
+ sorter.to_s.should_not == "parent_id_desc!non_vocational_complete_desc!jsci_complete_desc"
10
+ end
11
+
12
+ it "should toggle two order params at once" do
13
+ first = Sorted::Sorter.new("email ASC, phone ASC")
14
+ first.toggle.to_a.should == [["email", "asc"],["phone", "asc"]]
15
+ second = Sorted::Sorter.new(:name, {:sort => first.to_s})
16
+ second.toggle.to_a.should == [["name", "asc"], ["email", "asc"],["phone", "asc"]]
17
+ third = Sorted::Sorter.new("email ASC, phone ASC", {:sort => second.to_s})
18
+ third.toggle.to_a.should == [["email", "asc"],["phone", "asc"], ["name", "asc"]]
19
+ end
20
+ end
21
+
22
+ describe Sorted::Toggler, "add remaining orders" do
23
+ it "should toggle the sort order and include any sql orders not in sort params" do
24
+ sorter = Sorted::Sorter.new("email DESC, phone ASC, name DESC", {:sort => "email_desc!name_desc"})
25
+ sorter.toggle.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 => "mobile_asc!email_desc!phone_asc!name_desc"})
30
+ sorter.toggle.to_a.should == [["email", "desc"], ["phone", "asc"], ["name", "desc"], ["mobile", "asc"]]
31
+ end
32
+ end
33
+
34
+ describe Sorted::Toggler, "add remaining sorts" do
35
+ it "should toggle the sort order and include any sort orders not in order params" do
36
+ sorter = Sorted::Sorter.new("email DESC", {:sort => "email_desc!name_desc"})
37
+ sorter.toggle.to_a.should == [["email", "asc"], ["name", "desc"]]
38
+ end
39
+ end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorted
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 25
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 3
8
- - 4
9
- version: 0.3.4
9
+ - 5
10
+ version: 0.3.5
10
11
  platform: ruby
11
12
  authors:
12
13
  - Rufus Post
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-08-09 00:00:00 +10:00
18
+ date: 2010-09-10 00:00:00 +10:00
18
19
  default_executable:
19
20
  dependencies: []
20
21
 
@@ -35,12 +36,16 @@ files:
35
36
  - Rakefile
36
37
  - VERSION
37
38
  - lib/sorted.rb
38
- - lib/sorted/action_view.rb
39
- - lib/sorted/active_record.rb
39
+ - lib/sorted/finders/active_record.rb
40
40
  - lib/sorted/railtie.rb
41
+ - lib/sorted/sorter.rb
42
+ - lib/sorted/toggler.rb
43
+ - lib/sorted/view_helpers/action_view.rb
41
44
  - sorted.gemspec
42
45
  - spec/sorted_spec.rb
46
+ - spec/sorter_spec.rb
43
47
  - spec/spec_helper.rb
48
+ - spec/toggler_spec.rb
44
49
  has_rdoc: true
45
50
  homepage: http://github.com/mynameisrufus/sorted
46
51
  licenses: []
@@ -55,6 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
60
  requirements:
56
61
  - - ">="
57
62
  - !ruby/object:Gem::Version
63
+ hash: 3
58
64
  segments:
59
65
  - 0
60
66
  version: "0"
@@ -63,6 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
69
  requirements:
64
70
  - - ">="
65
71
  - !ruby/object:Gem::Version
72
+ hash: 3
66
73
  segments:
67
74
  - 0
68
75
  version: "0"
@@ -75,4 +82,6 @@ specification_version: 3
75
82
  summary: sort a table
76
83
  test_files:
77
84
  - spec/spec_helper.rb
85
+ - spec/sorter_spec.rb
86
+ - spec/toggler_spec.rb
78
87
  - spec/sorted_spec.rb
@@ -1,16 +0,0 @@
1
- require 'action_view'
2
- require 'sorted'
3
-
4
- module Sorted
5
- module ActionView
6
- def sorted(order)
7
- ::Sorted::Sorter.new(order, (request.get? && !params.nil?) ? params.dup : nil).toggle
8
- end
9
-
10
- def link_to_sorted(name, order, options = {})
11
- sorter = sorted(order)
12
- options[:class] = options[:class].nil? ? sorter.to_css : "#{options[:class]} #{sorter.to_css}"
13
- link_to(name, sorter.params, options)
14
- end
15
- end
16
- end
@@ -1,14 +0,0 @@
1
- require 'active_record'
2
- require 'sorted'
3
-
4
- module Sorted
5
- module ActiveRecord
6
- def self.enable!
7
- ::ActiveRecord::Base.class_eval do
8
- scope :sorted, lambda { |params|
9
- params.merge(:order => ::Sorted::Sorter.new(params[:order], {:sort => params.delete(:sort)}).to_sql)
10
- }
11
- end
12
- end
13
- end
14
- end