sorted 0.3.1 → 0.3.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/README.rdoc CHANGED
@@ -14,7 +14,7 @@ This will make a url like this:
14
14
 
15
15
  http://myapp/users?sort=email_asc
16
16
 
17
- Or on the next page load when you then sort by something else....
17
+ or on the next page load when you then sort by something else....
18
18
 
19
19
  http://myapp/users?sort=name_asc!email_asc
20
20
 
@@ -24,17 +24,34 @@ This will initially sort by email ascending:
24
24
 
25
25
  @users = User.sorted(:order => "email ASC", :sort => params[:sort]).paginate(:page => params[:page])
26
26
 
27
+ == Advanced
28
+
29
+ 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
+
31
+ def link_to_sorted(name, order)
32
+ sorter = sorted(order)
33
+ css_class = case sorter.order_first
34
+ when "asc"
35
+ "css_right ui-icon ui-icon-triangle-1-n"
36
+ when "desc"
37
+ "css_right ui-icon ui-icon-triangle-1-s"
38
+ else
39
+ "css_right ui-icon ui-icon-carat-2-n-s"
40
+ end
41
+ link_to(name, sorter.params) + content_tag(:span, nil, {:class => css_class})
42
+ end
43
+
27
44
  == Rails 2.3.x
28
45
 
29
- This gem works with rails 2.3.x but you will have to roll your own scope and view helper
46
+ This gem works with rails 2.3.x but you will have to roll your own scope and view helper.
30
47
 
31
- Here is the named scope:
48
+ Here is the named scope for you model(s):
32
49
 
33
50
  named_scope :sorted, lambda { |params|
34
51
  { :order => Sorted::Sorter.new(params[:order], {:sort => params[:sort]}).to_sql }
35
52
  }
36
53
 
37
- And this in your application helper:
54
+ and the the application helper methods:
38
55
 
39
56
  def sorted(order)
40
57
  Sorted::Sorter.new(order, (request.get? && !params.nil?) ? params.dup : nil).toggle
@@ -42,5 +59,5 @@ And this in your application helper:
42
59
 
43
60
  def link_to_sorted(name, order)
44
61
  sorter = sorted(order)
45
- link_to(name, sorter.params, {:class => sorter.css_class})
62
+ link_to(name, sorter.params, {:class => "sorted-#{sorter.order_first}"})
46
63
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -9,7 +9,7 @@ module Sorted
9
9
 
10
10
  def link_to_sorted(name, order, options = {})
11
11
  sorter = sorted(order)
12
- options[:class] = options[:class].nil? ? sorter.css_class : "#{options[:class]} #{sorter.css_class}"
12
+ options[:class] = options[:class].nil? ? "sorted-#{sorter.order_first}" : "#{options[:class]} sorted-#{sorter.order_first}"
13
13
  link_to(name, sorter.params, options)
14
14
  end
15
15
  end
data/lib/sorted.rb CHANGED
@@ -28,7 +28,7 @@ module Sorted
28
28
  end
29
29
 
30
30
  def parse_query(sort)
31
- if m = sort.match(/([a-zA-Z0-9._]+)_(asc|desc)/)
31
+ if m = sort.match(/([a-zA-Z0-9._]+)_(asc|desc)$/)
32
32
  [m[1],m[2]]
33
33
  end
34
34
  end
@@ -49,17 +49,17 @@ module Sorted
49
49
  order_queue.select do |o|
50
50
  !@_array.flatten.include?(o[0])
51
51
  end.each do |o|
52
- @_array << [o[0], o[1]]
52
+ @_array << o
53
53
  end
54
54
  sort_queue.select do |s|
55
55
  !@_array.flatten.include?(s[0])
56
56
  end.each do |s|
57
- @_array << [s[0], s[1]]
57
+ @_array << s
58
58
  end
59
59
  self
60
60
  end
61
61
 
62
- def un_toggle
62
+ def reset
63
63
  @_array = default
64
64
  self
65
65
  end
@@ -76,8 +76,12 @@ module Sorted
76
76
  _array.map{|a| a.join('_')}.join('!')
77
77
  end
78
78
 
79
- def css_class
80
- "sorted-#{_array[0][1]}"
79
+ def to_a
80
+ _array
81
+ end
82
+
83
+ def order_first
84
+ _array.empty? ? nil : _array[0][1]
81
85
  end
82
86
 
83
87
  def params
@@ -96,13 +100,11 @@ module Sorted
96
100
 
97
101
  private
98
102
  def default
99
- _default = sort_queue.dup
103
+ sq = sort_queue.dup
100
104
  order_queue.each do |o|
101
- unless _default.flatten.include?(o[0])
102
- _default << [o[0], o[1]]
103
- end
105
+ sq << o unless sq.flatten.include?(o[0])
104
106
  end
105
- _default
107
+ sq
106
108
  end
107
109
 
108
110
  def _array
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.1"
8
+ s.version = "0.3.2"
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-23}
12
+ s.date = %q{2010-07-26}
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
@@ -120,9 +120,9 @@ describe Sorted::ActionView do
120
120
  sorter.to_s.should == "email_asc"
121
121
  end
122
122
 
123
- it "should return css class" do
123
+ it "should return the order of the first attribute" do
124
124
  sorter = ActionView::Base.new([], {}, @controller).sorted(:email)
125
- sorter.css_class.should == "sorted-asc"
125
+ sorter.order_first.should == "asc"
126
126
  end
127
127
 
128
128
  it "should allow underscores and and full stops in" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
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-23 00:00:00 +10:00
17
+ date: 2010-07-26 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies: []
20
20