JasonKing-good_sort 0.1.7 → 0.1.10

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/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 10
4
+ :major: 0
@@ -12,3 +12,18 @@ module GoodSort
12
12
  end
13
13
  end
14
14
  end
15
+
16
+ class RemoteLinkRenderer < WillPaginate::LinkRenderer
17
+ def prepare(collection, options, template)
18
+ @remote = options.delete(:remote) || {}
19
+ super
20
+ end
21
+
22
+ protected
23
+ def page_link(page, text, attributes = {})
24
+ _url = url_for(page)
25
+ @template.link_to_remote(text, {:url => _url, :method => :get}.merge(@remote), { :href => _url })
26
+ end
27
+ end
28
+
29
+ WillPaginate::ViewHelpers.pagination_options[:renderer] = 'RemoteLinkRenderer'
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+ require 'good_sort/sorter'
3
+ require 'active_support'
4
+
5
+ class GoodSortSorterTest < Test::Unit::TestCase
6
+ include GoodSort::Sorter
7
+ def columns_hash; { 'foo' => true, 'bar' => true }; end
8
+ def class_name; "foobar"; end
9
+
10
+ def reflect_on_association(a)
11
+ ass = mock()
12
+ ass.stubs(:belongs_to?).returns(a.to_sym == :ass_exist)
13
+
14
+ ass.stubs(:class_name).returns('ass_exists')
15
+
16
+ ass_klass = mock()
17
+ ass_klass.stubs(:column_names).returns( %w{name last_name} )
18
+
19
+ ass.stubs(:klass).returns(ass_klass)
20
+
21
+ ass
22
+ end
23
+
24
+ def test_sort_on_our_attributes
25
+ sort_on :foo, :bar
26
+ assert_equal 2, sort_fields.length
27
+ assert_equal( { :order => 'foo' }, sort_fields[:foo])
28
+ assert_equal( { :order => 'bar' }, sort_fields[:bar])
29
+ end
30
+
31
+ def test_association_sort_fields
32
+ sort_on :ass_exist => :last_name
33
+ assert_equal 'ass_exists.last_name', sort_fields[:ass_exist][:order]
34
+ end
35
+
36
+ def test_default_association_sort_field
37
+ sort_on :ass_exist
38
+ assert_equal 'ass_exists.name', sort_fields[:ass_exist][:order]
39
+ end
40
+
41
+ def test_argument_errors
42
+ assert_raise ArgumentError do
43
+ sort_on :ass_imaginary
44
+ end
45
+
46
+ assert_raise ArgumentError do
47
+ sort_on false
48
+ end
49
+
50
+ assert_raise ArgumentError do
51
+ sort_on :ass_exist => :nonexistent
52
+ end
53
+ end
54
+
55
+ def test_multiple_declarations
56
+ sort_on :foo
57
+ sort_on :bar
58
+ assert_equal 2, sort_fields.length
59
+ assert_equal( { :order => 'foo' }, sort_fields[:foo])
60
+ assert_equal( { :order => 'bar' }, sort_fields[:bar])
61
+ end
62
+
63
+ def test_sort_by
64
+ sort_on :foo
65
+ assert_equal( { :order => 'foo' }, sort_by( :field => 'foo', :down => '' ))
66
+ assert_equal( { :order => 'foo DESC'}, sort_by( :field => 'foo', :down => 'true' ))
67
+ assert_raise ArgumentError do
68
+ sort_by( :field => 'bar', :down => '' )
69
+ end
70
+ assert_nil sort_by nil
71
+ assert_nil sort_by( :field => 'foo' )
72
+ assert_nil sort_by( :down => '' )
73
+ assert_nil sort_by( :down => 'true' )
74
+
75
+ sort_on :ass_exist
76
+ assert_equal( { :joins => :ass_exist, :order => 'ass_exists.name' }, sort_by( :field => 'ass_exist', :down => '' ))
77
+ assert_equal( { :joins => :ass_exist, :order => 'ass_exists.name DESC' }, sort_by( :field => 'ass_exist', :down => 'true' ))
78
+ end
79
+
80
+ def teardown
81
+ GoodSort::Sorter.send :class_variable_set, :@@sort_fields, {}
82
+ end
83
+ end
@@ -0,0 +1,159 @@
1
+ require 'test_helper'
2
+ require 'good_sort/view_helpers'
3
+ require 'active_support'
4
+ require 'action_view'
5
+
6
+ class Foo; end
7
+ class Logger; end
8
+ class GoodSortViewHelperTest < Test::Unit::TestCase
9
+ include ActionView::Helpers::TagHelper
10
+ include GoodSort::ViewHelpers
11
+
12
+ def concat(a); @output << a; end
13
+ def params; @p ||= {}; end
14
+ def logger; @l ||= Logger.new; end
15
+
16
+ def setup
17
+ @output = ''
18
+ Foo.stubs(:sort_fields).returns( :name => true, :age => true )
19
+ end
20
+
21
+ def test_default_headers
22
+ %w{name age}.each do |f|
23
+ expects(:url_for).returns( "/foo" )
24
+ expects(:link_to_remote).with(
25
+ f.titleize,
26
+ {
27
+ :update => 'foos',
28
+ :method => :get,
29
+ :url => "/foo",
30
+ :complete => %q{$('spinner').hide()},
31
+ :before => %q{$('spinner').show()}
32
+ },
33
+ {
34
+ :href => "/foo",
35
+ :title => "Sort by #{f.titleize}"
36
+ }).returns( "<link>#{f}</link>" )
37
+ end
38
+
39
+ sort_headers_for :foo, %w{name age bar}
40
+ assert_equal %q{<th id="foo_header_name"><link>name</link></th><th id="foo_header_age"><link>age</link></th><th id="foo_header_bar">Bar</th>},
41
+ @output
42
+ end
43
+
44
+ def test_some_options
45
+ spinner_name = :foobar
46
+ update_id = :sesame
47
+ %w{name age}.each do |f|
48
+ expects(:url_for).returns( "/foo" )
49
+ expects(:link_to_remote).with(
50
+ f.titleize,
51
+ {
52
+ :update => update_id,
53
+ :method => :get,
54
+ :url => "/foo",
55
+ :complete => %Q{$('#{spinner_name}').hide()},
56
+ :before => %Q{$('#{spinner_name}').show()}
57
+ },
58
+ {
59
+ :class => :horton,
60
+ :href => "/foo",
61
+ :title => "Sort by #{f.titleize}"
62
+ }).returns( "<link>#{f}</link>" )
63
+ end
64
+
65
+ bar_text = 'Big bar'
66
+ sort_headers_for :foo, %w{name age bar}, :spinner => spinner_name, :tag => :td, :remote => { :update => update_id }, :html => { :class => :horton } do |f|
67
+ bar_text if f == 'bar'
68
+ end
69
+ assert_equal %Q{<td id="foo_header_name"><link>name</link></td><td id="foo_header_age"><link>age</link></td><td id="foo_header_bar">#{bar_text}</td>},
70
+ @output
71
+ end
72
+
73
+ def test_sorting_name_up
74
+ params[:sort] ||= {}
75
+ params[:sort][:field] = 'name'
76
+ params[:sort][:down] = ''
77
+
78
+ p = { :name => true, :age => nil }
79
+
80
+ %w{name age}.each do |f|
81
+ expects(:url_for).with(:params => {:sort => {:field => f, :down => p[f.to_sym]}, :page => nil}).returns( "/foo" )
82
+ expects(:link_to_remote).with(
83
+ f.titleize,
84
+ {
85
+ :update => 'foos',
86
+ :method => :get,
87
+ :url => "/foo",
88
+ :complete => %q{$('spinner').hide()},
89
+ :before => %q{$('spinner').show()}
90
+ },
91
+ {
92
+ :href => "/foo",
93
+ :title => "Sort by #{f.titleize}"
94
+ }).returns( "<link>#{f}</link>" )
95
+ end
96
+
97
+ sort_headers_for :foo, %w{name age}
98
+ assert_equal %q{<th class="up" id="foo_header_name"><link>name</link></th><th id="foo_header_age"><link>age</link></th>},
99
+ @output
100
+ end
101
+
102
+ def test_sorting_name_down
103
+ params[:sort] ||= {}
104
+ params[:sort][:field] = 'name'
105
+ params[:sort][:down] = 'true'
106
+
107
+ p = { :name => nil, :age => nil }
108
+
109
+ %w{name age}.each do |f|
110
+ expects(:url_for).with(:params => {:sort => {:field => f, :down => p[f.to_sym]}, :page => nil}).returns( "/foo" )
111
+ expects(:link_to_remote).with(
112
+ f.titleize,
113
+ {
114
+ :update => 'foos',
115
+ :method => :get,
116
+ :url => "/foo",
117
+ :complete => %q{$('spinner').hide()},
118
+ :before => %q{$('spinner').show()}
119
+ },
120
+ {
121
+ :href => "/foo",
122
+ :title => "Sort by #{f.titleize}"
123
+ }).returns( "<link>#{f}</link>" )
124
+ end
125
+
126
+ sort_headers_for :foo, %w{name age}
127
+ assert_equal %q{<th class="down" id="foo_header_name"><link>name</link></th><th id="foo_header_age"><link>age</link></th>},
128
+ @output
129
+ end
130
+
131
+ def test_sorting_age_up
132
+ params[:sort] ||= {}
133
+ params[:sort][:field] = 'age'
134
+ params[:sort][:down] = ''
135
+
136
+ p = { :name => nil, :age => true }
137
+
138
+ %w{name age}.each do |f|
139
+ expects(:url_for).with(:params => {:sort => {:field => f, :down => p[f.to_sym]}, :page => nil}).returns( "/foo" )
140
+ expects(:link_to_remote).with(
141
+ f.titleize,
142
+ {
143
+ :update => 'foos',
144
+ :method => :get,
145
+ :url => "/foo",
146
+ :complete => %q{$('spinner').hide()},
147
+ :before => %q{$('spinner').show()}
148
+ },
149
+ {
150
+ :href => "/foo",
151
+ :title => "Sort by #{f.titleize}"
152
+ }).returns( "<link>#{f}</link>" )
153
+ end
154
+
155
+ sort_headers_for :foo, %w{name age}
156
+ assert_equal %q{<th id="foo_header_name"><link>name</link></th><th class="up" id="foo_header_age"><link>age</link></th>},
157
+ @output
158
+ end
159
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: JasonKing-good_sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason King
@@ -30,8 +30,8 @@ files:
30
30
  - lib/good_sort/view_helpers.rb
31
31
  - lib/good_sort/will_paginate.rb
32
32
  - lib/good_sort.rb
33
- - test/good_sort_test.rb
34
- - test/good_sort_view.rb
33
+ - test/sorter_test.rb
34
+ - test/view_helpers_test.rb
35
35
  - test/test_helper.rb
36
36
  - LICENSE
37
37
  has_rdoc: true