sort_this 1.0.0 → 1.0.1

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/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ gemfile:
6
+ - Gemfile
7
+ - Gemfile.rails3.1
8
+ - Gemfile.rails3.0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
1
  ## v1.0.0
2
2
 
3
- * Initial Release
3
+ * Initial Release
4
+
5
+ ## v1.0.1
6
+
7
+ * column_name is now optional
8
+ * added a table_name option to use when using joins
9
+ * added default_sort class method
10
+ * sort and default_sort are memoized
data/Gemfile CHANGED
@@ -1,4 +1,11 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in sortable.gemspec
4
- gemspec
3
+ gem 'rails', '~> 3.2.0'
4
+ gem 'memoist', '0.2.0'
5
+
6
+ gem 'rspec', '2.12.0'
7
+ gem 'sqlite3', '1.3.6'
8
+ gem 'factory_girl', '4.1.0'
9
+ gem 'database_cleaner', '0.9.1'
10
+ gem 'capybara', '2.0.1'
11
+ gem 'rake'
data/Gemfile.rails3.0 ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 3.0.0'
4
+ gem 'memoist', '0.2.0'
5
+
6
+ gem 'rspec', '2.12.0'
7
+ gem 'sqlite3', '1.3.6'
8
+ gem 'factory_girl', '4.1.0'
9
+ gem 'database_cleaner', '0.9.1'
10
+ gem 'capybara', '2.0.1'
11
+ gem 'rake'
@@ -0,0 +1,122 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.17)
6
+ actionpack (= 3.0.17)
7
+ mail (~> 2.2.19)
8
+ actionpack (3.0.17)
9
+ activemodel (= 3.0.17)
10
+ activesupport (= 3.0.17)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.5)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.17)
19
+ activesupport (= 3.0.17)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.17)
23
+ activemodel (= 3.0.17)
24
+ activesupport (= 3.0.17)
25
+ arel (~> 2.0.10)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.17)
28
+ activemodel (= 3.0.17)
29
+ activesupport (= 3.0.17)
30
+ activesupport (3.0.17)
31
+ addressable (2.3.2)
32
+ arel (2.0.10)
33
+ builder (2.1.2)
34
+ capybara (2.0.1)
35
+ mime-types (>= 1.16)
36
+ nokogiri (>= 1.3.3)
37
+ rack (>= 1.0.0)
38
+ rack-test (>= 0.5.4)
39
+ selenium-webdriver (~> 2.0)
40
+ xpath (~> 1.0.0)
41
+ childprocess (0.3.6)
42
+ ffi (~> 1.0, >= 1.0.6)
43
+ database_cleaner (0.9.1)
44
+ diff-lcs (1.1.3)
45
+ erubis (2.6.6)
46
+ abstract (>= 1.0.0)
47
+ factory_girl (4.1.0)
48
+ activesupport (>= 3.0.0)
49
+ ffi (1.2.0)
50
+ i18n (0.5.0)
51
+ json (1.7.5)
52
+ libwebsocket (0.1.7.1)
53
+ addressable
54
+ websocket
55
+ mail (2.2.19)
56
+ activesupport (>= 2.3.6)
57
+ i18n (>= 0.4.0)
58
+ mime-types (~> 1.16)
59
+ treetop (~> 1.4.8)
60
+ memoist (0.2.0)
61
+ mime-types (1.19)
62
+ multi_json (1.5.0)
63
+ nokogiri (1.5.6)
64
+ polyglot (0.3.3)
65
+ rack (1.2.5)
66
+ rack-mount (0.6.14)
67
+ rack (>= 1.0.0)
68
+ rack-test (0.5.7)
69
+ rack (>= 1.0)
70
+ rails (3.0.17)
71
+ actionmailer (= 3.0.17)
72
+ actionpack (= 3.0.17)
73
+ activerecord (= 3.0.17)
74
+ activeresource (= 3.0.17)
75
+ activesupport (= 3.0.17)
76
+ bundler (~> 1.0)
77
+ railties (= 3.0.17)
78
+ railties (3.0.17)
79
+ actionpack (= 3.0.17)
80
+ activesupport (= 3.0.17)
81
+ rake (>= 0.8.7)
82
+ rdoc (~> 3.4)
83
+ thor (~> 0.14.4)
84
+ rake (10.0.3)
85
+ rdoc (3.12)
86
+ json (~> 1.4)
87
+ rspec (2.12.0)
88
+ rspec-core (~> 2.12.0)
89
+ rspec-expectations (~> 2.12.0)
90
+ rspec-mocks (~> 2.12.0)
91
+ rspec-core (2.12.2)
92
+ rspec-expectations (2.12.1)
93
+ diff-lcs (~> 1.1.3)
94
+ rspec-mocks (2.12.1)
95
+ rubyzip (0.9.9)
96
+ selenium-webdriver (2.27.2)
97
+ childprocess (>= 0.2.5)
98
+ libwebsocket (~> 0.1.3)
99
+ multi_json (~> 1.0)
100
+ rubyzip
101
+ sqlite3 (1.3.6)
102
+ thor (0.14.6)
103
+ treetop (1.4.12)
104
+ polyglot
105
+ polyglot (>= 0.3.1)
106
+ tzinfo (0.3.35)
107
+ websocket (1.0.6)
108
+ xpath (1.0.0)
109
+ nokogiri (~> 1.3)
110
+
111
+ PLATFORMS
112
+ ruby
113
+
114
+ DEPENDENCIES
115
+ capybara (= 2.0.1)
116
+ database_cleaner (= 0.9.1)
117
+ factory_girl (= 4.1.0)
118
+ memoist (= 0.2.0)
119
+ rails (~> 3.0.0)
120
+ rake
121
+ rspec (= 2.12.0)
122
+ sqlite3 (= 1.3.6)
data/Gemfile.rails3.1 ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 3.1.0'
4
+ gem 'memoist', '0.2.0'
5
+
6
+ gem 'rspec', '2.12.0'
7
+ gem 'sqlite3', '1.3.6'
8
+ gem 'factory_girl', '4.1.0'
9
+ gem 'database_cleaner', '0.9.1'
10
+ gem 'capybara', '2.0.1'
11
+ gem 'rake'
@@ -0,0 +1,132 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ actionmailer (3.1.8)
5
+ actionpack (= 3.1.8)
6
+ mail (~> 2.3.3)
7
+ actionpack (3.1.8)
8
+ activemodel (= 3.1.8)
9
+ activesupport (= 3.1.8)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ i18n (~> 0.6)
13
+ rack (~> 1.3.6)
14
+ rack-cache (~> 1.2)
15
+ rack-mount (~> 0.8.2)
16
+ rack-test (~> 0.6.1)
17
+ sprockets (~> 2.0.4)
18
+ activemodel (3.1.8)
19
+ activesupport (= 3.1.8)
20
+ builder (~> 3.0.0)
21
+ i18n (~> 0.6)
22
+ activerecord (3.1.8)
23
+ activemodel (= 3.1.8)
24
+ activesupport (= 3.1.8)
25
+ arel (~> 2.2.3)
26
+ tzinfo (~> 0.3.29)
27
+ activeresource (3.1.8)
28
+ activemodel (= 3.1.8)
29
+ activesupport (= 3.1.8)
30
+ activesupport (3.1.8)
31
+ multi_json (>= 1.0, < 1.3)
32
+ addressable (2.3.2)
33
+ arel (2.2.3)
34
+ builder (3.0.4)
35
+ capybara (2.0.1)
36
+ mime-types (>= 1.16)
37
+ nokogiri (>= 1.3.3)
38
+ rack (>= 1.0.0)
39
+ rack-test (>= 0.5.4)
40
+ selenium-webdriver (~> 2.0)
41
+ xpath (~> 1.0.0)
42
+ childprocess (0.3.6)
43
+ ffi (~> 1.0, >= 1.0.6)
44
+ database_cleaner (0.9.1)
45
+ diff-lcs (1.1.3)
46
+ erubis (2.7.0)
47
+ factory_girl (4.1.0)
48
+ activesupport (>= 3.0.0)
49
+ ffi (1.2.0)
50
+ hike (1.2.1)
51
+ i18n (0.6.1)
52
+ json (1.7.5)
53
+ libwebsocket (0.1.7.1)
54
+ addressable
55
+ websocket
56
+ mail (2.3.3)
57
+ i18n (>= 0.4.0)
58
+ mime-types (~> 1.16)
59
+ treetop (~> 1.4.8)
60
+ memoist (0.2.0)
61
+ mime-types (1.19)
62
+ multi_json (1.2.0)
63
+ nokogiri (1.5.6)
64
+ polyglot (0.3.3)
65
+ rack (1.3.6)
66
+ rack-cache (1.2)
67
+ rack (>= 0.4)
68
+ rack-mount (0.8.3)
69
+ rack (>= 1.0.0)
70
+ rack-ssl (1.3.2)
71
+ rack
72
+ rack-test (0.6.2)
73
+ rack (>= 1.0)
74
+ rails (3.1.8)
75
+ actionmailer (= 3.1.8)
76
+ actionpack (= 3.1.8)
77
+ activerecord (= 3.1.8)
78
+ activeresource (= 3.1.8)
79
+ activesupport (= 3.1.8)
80
+ bundler (~> 1.0)
81
+ railties (= 3.1.8)
82
+ railties (3.1.8)
83
+ actionpack (= 3.1.8)
84
+ activesupport (= 3.1.8)
85
+ rack-ssl (~> 1.3.2)
86
+ rake (>= 0.8.7)
87
+ rdoc (~> 3.4)
88
+ thor (~> 0.14.6)
89
+ rake (10.0.3)
90
+ rdoc (3.12)
91
+ json (~> 1.4)
92
+ rspec (2.12.0)
93
+ rspec-core (~> 2.12.0)
94
+ rspec-expectations (~> 2.12.0)
95
+ rspec-mocks (~> 2.12.0)
96
+ rspec-core (2.12.2)
97
+ rspec-expectations (2.12.1)
98
+ diff-lcs (~> 1.1.3)
99
+ rspec-mocks (2.12.1)
100
+ rubyzip (0.9.9)
101
+ selenium-webdriver (2.27.2)
102
+ childprocess (>= 0.2.5)
103
+ libwebsocket (~> 0.1.3)
104
+ multi_json (~> 1.0)
105
+ rubyzip
106
+ sprockets (2.0.4)
107
+ hike (~> 1.2)
108
+ rack (~> 1.0)
109
+ tilt (~> 1.1, != 1.3.0)
110
+ sqlite3 (1.3.6)
111
+ thor (0.14.6)
112
+ tilt (1.3.3)
113
+ treetop (1.4.12)
114
+ polyglot
115
+ polyglot (>= 0.3.1)
116
+ tzinfo (0.3.35)
117
+ websocket (1.0.6)
118
+ xpath (1.0.0)
119
+ nokogiri (~> 1.3)
120
+
121
+ PLATFORMS
122
+ ruby
123
+
124
+ DEPENDENCIES
125
+ capybara (= 2.0.1)
126
+ database_cleaner (= 0.9.1)
127
+ factory_girl (= 4.1.0)
128
+ memoist (= 0.2.0)
129
+ rails (~> 3.1.0)
130
+ rake
131
+ rspec (= 2.12.0)
132
+ sqlite3 (= 1.3.6)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # SortThis
1
+ # SortThis [![Build Status](https://secure.travis-ci.org/spullen/sort_this.png)](http://travis-ci.org/spullen/sort_this)
2
2
 
3
3
  SortThis provides a way to sort.
4
4
 
@@ -10,7 +10,7 @@ Here is an [example app](https://github.com/spullen/sort_example) that demonstra
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- gem 'sort_this', :git => 'git://github.com/spullen/sort_this.git'
13
+ gem 'sort_this'
14
14
 
15
15
  And then execute:
16
16
 
@@ -24,7 +24,7 @@ Add a call to the `sortable` method with a hash of sorts
24
24
 
25
25
  class YourModel < ActiveRecord::Base
26
26
 
27
- sort_this :sort_name => {:column_name => :name, :default => 'ASC', :joins => :association, :clause => "some.clause"}
27
+ sort_this :sort_name => {:column_name => :name, :default => 'ASC', :table_name => 'some_other_table_name', :joins => :association, :clause => "some.clause"}
28
28
 
29
29
  end
30
30
 
@@ -34,14 +34,16 @@ The options are
34
34
 
35
35
  Sort Options:
36
36
 
37
- column_name: (Required) The name of the column to sort on.
37
+ column_name: (Optional) The name of the column to sort on. If left blank it will use the sort name.
38
38
  default: (Optional) Defines a default sort if provided. The valid options are 'ASC' or 'DESC'.
39
- joins: (Optional) Defines an association to join on, this should be provided if the column is in another table.
39
+ table_name (Optional) Overrides the table name used, should usually be used in coordination with joins.
40
+ joins: (Optional) Defines an association to join on, this should be provided if the column is in another table. ** Requires table name to be set. **
41
+ This is what you would usually pass into a joins or includes clause for ActiveRecord.
40
42
  clause: (Optional) Overrides the clause used for the sort.
41
43
 
42
44
  Then to sort
43
45
 
44
- YourModel.sort("sort_name", "asc|desc") => sorted list of YourModel objects
46
+ YourModel.sort("sort_name", "asc|desc|ASC|DESC") => sorted list of YourModel objects
45
47
 
46
48
  ### Controller
47
49
 
@@ -67,11 +69,26 @@ In you controller define a default sort
67
69
 
68
70
  ## TODO:
69
71
 
70
- - Add error handling to active record sortable
71
- - Add the ability to customize the sort and direction parameters
72
- - Define scopes for each sort defined (individual sort scopes)
72
+ Defined Highest to Lowest priority
73
73
 
74
+ - Throw error when the table_name option is not set and joins is set.
75
+ - Being able to define multiple column sorts
76
+
77
+ Something like:
78
+ sort_this :some_crazy_sort => {
79
+ [
80
+ {:column_name => :some_column, other options},
81
+ ...
82
+ ]
83
+ }
84
+
85
+ not sure how that'd be called though...
86
+
87
+ - Define a default scope and remove from the sort method, gives more control to developer.
88
+ - Define scopes for each sort defined (individual sort scopes)
89
+ - Add the ability to customize the sort and direction parameters
74
90
  - Testing on different databases (should probably hit postgresql and mysql unless the way SQLite handles it in the same way)
91
+ - Define rails 3.0 with ruby 1.8.7 gemfile for travis ci tests as 1.8.7 is still prevelant for 3.0
75
92
 
76
93
  ## Contributing
77
94
 
@@ -7,8 +7,7 @@ module SortThis
7
7
  base.class_eval do
8
8
  extend ClassMethods
9
9
 
10
- class << self; extend Memoist; self; end.memoize :sort
11
-
10
+ class << self; extend Memoist; self; end.memoize :sort, :default_sort
12
11
 
13
12
  class_attribute :sort_columns, :default_sort_columns
14
13
  end
@@ -19,10 +18,10 @@ module SortThis
19
18
  #
20
19
  # Input is a hash of sort_name => sort_options pairs
21
20
  #
22
- # :column_name => (required) Column name to sort on
21
+ # :column_name => (optional) Column name to sort on, if left blank it will use the sort_name
23
22
  # :default => (optional) 'ASC'|'DESC'
23
+ # :table_name => (optional) Override the table name, required if using joins
24
24
  # :joins => (optional) Association to join on. Note: must be an association of the model being sorted.
25
- # Prefixes the column_name with the table name to prevent collisions
26
25
  # :clause => (optional) Override the clause of the sort
27
26
  #
28
27
  # ex.
@@ -33,34 +32,43 @@ module SortThis
33
32
  #
34
33
  # :sort_name1 => { :column_name => :quantity }
35
34
  # :sort_name2 => { :column_name => :price, :default => 'DESC' }
36
- # :sort_name3 => { :column_name => :name, :joins => :product }
35
+ # :sort_name3 => { :column_name => :name, :table_name => 'products', :joins => :product }
37
36
  #
38
37
  def sort_this(sorts = {})
39
38
  self.sort_columns = {}
40
39
  self.default_sort_columns = {}
41
40
 
42
- empty_sort_options = { :column_name => nil, :default => nil, :joins => nil, :clause => nil }
41
+ empty_sort_options = { :column_name => nil, :default => nil, :table_name => nil, :joins => nil, :clause => nil }
43
42
 
44
43
  sorts.each do |sort_name, sort_options|
45
44
  sort_options = empty_sort_options.merge(sort_options)
46
45
 
47
46
  self.sort_columns[sort_name] = sort_options
48
47
 
49
- column = sort_options[:column_name] # TODO: raise exception if this is nil
50
- table = (sort_options[:joins].blank?) ? table_name : sort_options[:joins].to_s.pluralize
51
- clause = (sort_options[:clause].blank?) ? "#{table}.#{column}" : sort_options[:clause]
48
+ column = (sort_options[:column_name].blank?) ? sort_name : sort_options[:column_name]
49
+ table = (sort_options[:table_name].blank?) ? table_name : sort_options[:table_name].to_s.pluralize
50
+ clause = (sort_options[:clause].blank?) ? "#{table}.#{column}" : sort_options[:clause]
52
51
 
53
- self.sort_columns[sort_name][:clause] = clause if sort_options[:clause].blank?
52
+ self.sort_columns[sort_name][:column_name] = column
53
+ self.sort_columns[sort_name][:table_name] = table
54
+ self.sort_columns[sort_name][:clause] = clause
54
55
 
55
56
  unless sort_options[:default].blank?
56
- # TODO: raise exception if the default is not ASC|DESC
57
57
  default_sort_direction = sort_options[:default].upcase
58
+
59
+ unless VALID_SORT_DIRECTIONS.include?(default_sort_direction)
60
+ raise SortThisError, "Invalid sort direction for: #{sort_name}. Must be 'ASC'/'asc' or 'DESC'/'desc'."
61
+ end
62
+
58
63
  self.default_sort_columns[sort_name] = "#{clause} #{default_sort_direction}"
59
64
  end
60
-
61
65
  end
62
66
  end
63
67
 
68
+ def default_sort
69
+ order(self.default_sort_columns.values.join(', '))
70
+ end
71
+
64
72
  def sort(sort_column = nil, sort_direction = DEFAULT_SORT_DIRECTION)
65
73
  query = scoped
66
74
 
@@ -0,0 +1,4 @@
1
+ module SortThis
2
+ class SortThisError < StandardError
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module SortThis
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/lib/sort_this.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'sort_this/version'
2
+ require 'sort_this/sort_this_error'
2
3
  require 'sort_this/active_record'
3
4
  require 'sort_this/action_controller'
4
5
  require 'sort_this/view_helpers/action_view'
data/sort_this.gemspec CHANGED
@@ -19,11 +19,4 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_dependency 'rails', '>= 3.0'
21
21
  gem.add_dependency 'memoist', '0.2.0'
22
-
23
- gem.add_development_dependency 'debugger', '1.2.2'
24
- gem.add_development_dependency 'rspec', '2.12.0'
25
- gem.add_development_dependency 'sqlite3', '1.3.6'
26
- gem.add_development_dependency 'factory_girl', '4.1.0'
27
- gem.add_development_dependency 'database_cleaner', '0.9.1'
28
- gem.add_development_dependency 'capybara', '2.0.1'
29
22
  end
@@ -43,6 +43,10 @@ describe SortThis::ActiveRecord do
43
43
  Quote.sort_columns[sort_name][:default].should == default_option
44
44
  end
45
45
 
46
+ it 'should properly set the table_name' do
47
+ Quote.sort_columns[sort_name][:table_name].should == table_name_option
48
+ end
49
+
46
50
  it 'should properly set the joins option' do
47
51
  Quote.sort_columns[sort_name][:joins].should == joins_option
48
52
  end
@@ -67,15 +71,16 @@ describe SortThis::ActiveRecord do
67
71
  end
68
72
  end
69
73
 
70
- context 'given a sort column with just the required sort options' do
74
+ context 'given a sort column with no options specified' do
71
75
  let!(:sort_name) { :price }
72
76
  let!(:column_name_option) { :price }
77
+ let!(:table_name_option) { 'quotes' }
73
78
  let!(:default_option) { nil }
74
79
  let!(:joins_option) { nil }
75
80
  let!(:clause_option) { "quotes.price" }
76
-
81
+
77
82
  before(:each) do
78
- Quote.sort_this sort_name => {:column_name => column_name_option}
83
+ Quote.sort_this sort_name => {}
79
84
  end
80
85
 
81
86
  it_should_behave_like 'sort_columns_defined'
@@ -85,37 +90,75 @@ describe SortThis::ActiveRecord do
85
90
  end
86
91
  end
87
92
 
88
- context 'given a sort column with a default specified in the sort options' do
93
+ context 'given a sort column with just the required sort options' do
89
94
  let!(:sort_name) { :price }
90
95
  let!(:column_name_option) { :price }
91
- let!(:default_option) { 'DESC' }
96
+ let!(:table_name_option) { 'quotes' }
97
+ let!(:default_option) { nil }
92
98
  let!(:joins_option) { nil }
93
99
  let!(:clause_option) { "quotes.price" }
94
100
 
95
101
  before(:each) do
96
- Quote.sort_this sort_name => {:column_name => column_name_option, :default => default_option}
102
+ Quote.sort_this sort_name => {:column_name => column_name_option}
97
103
  end
98
104
 
99
105
  it_should_behave_like 'sort_columns_defined'
100
106
 
101
- it 'should set the default_sort_columns options for the specified sort name' do
102
- Quote.default_sort_columns.should have_key(sort_name)
107
+ it 'should set default_sort_columns to an empty hash' do
108
+ Quote.default_sort_columns.should == {}
103
109
  end
110
+ end
111
+
112
+ context 'given a sort column with a default specified in the sort options' do
113
+ context 'when the default is valid' do
114
+ let!(:sort_name) { :price }
115
+ let!(:column_name_option) { :price }
116
+ let!(:table_name_option) { 'quotes' }
117
+ let!(:default_option) { 'DESC' }
118
+ let!(:joins_option) { nil }
119
+ let!(:clause_option) { "quotes.price" }
104
120
 
105
- it 'should set the clause of the default_sort_columns for the specified sort name' do
106
- Quote.default_sort_columns[sort_name].should == "#{clause_option} #{default_option}"
121
+ before(:each) do
122
+ Quote.sort_this sort_name => {:column_name => column_name_option, :default => default_option}
123
+ end
124
+
125
+ it_should_behave_like 'sort_columns_defined'
126
+
127
+ it 'should set the default_sort_columns options for the specified sort name' do
128
+ Quote.default_sort_columns.should have_key(sort_name)
129
+ end
130
+
131
+ it 'should set the clause of the default_sort_columns for the specified sort name' do
132
+ Quote.default_sort_columns[sort_name].should == "#{clause_option} #{default_option}"
133
+ end
134
+ end
135
+
136
+ context 'when the default is not ASC or DESC' do
137
+ let!(:sort_name) { :quantity }
138
+ let!(:column_name_option) { :quantity }
139
+ let!(:table_name_option) { 'quotes' }
140
+ let!(:default_option) { 'GARBAGE' }
141
+ let!(:joins_option) { nil }
142
+ let!(:clause_option) { "custom.joins_clause" }
143
+
144
+ it 'should raise a SortThisError' do
145
+ lambda {
146
+ Quote.sort_this sort_name => {:column_name => column_name_option, :default => default_option, :clause => clause_option}
147
+ }.should raise_error(SortThis::SortThisError, "Invalid sort direction for: #{sort_name}. Must be 'ASC'/'asc' or 'DESC'/'desc'.")
148
+ end
107
149
  end
108
150
  end
109
151
 
110
152
  context 'given a sort column with a joins specified in the sort options' do
111
153
  let!(:sort_name) { :product_name }
112
154
  let!(:column_name_option) { :name }
155
+ let!(:table_name_option) { 'products' }
113
156
  let!(:default_option) { nil }
114
157
  let!(:joins_option) { :product }
115
158
  let!(:clause_option) { "products.name" }
116
159
 
117
160
  before(:each) do
118
- Quote.sort_this sort_name => {:column_name => column_name_option, :joins => joins_option}
161
+ Quote.sort_this sort_name => {:column_name => column_name_option, :table_name => table_name_option, :joins => joins_option}
119
162
  end
120
163
 
121
164
  it_should_behave_like 'sort_columns_defined'
@@ -128,6 +171,7 @@ describe SortThis::ActiveRecord do
128
171
  context 'given a sort column with a custom clause specified in the sort options' do
129
172
  let!(:sort_name) { :price }
130
173
  let!(:column_name_option) { :price }
174
+ let!(:table_name_option) { 'quotes' }
131
175
  let!(:default_option) { nil }
132
176
  let!(:joins_option) { nil }
133
177
  let!(:clause_option) { "custom.joins_clause" }
@@ -146,6 +190,7 @@ describe SortThis::ActiveRecord do
146
190
  context 'given a sort column with a custom clause and default specified in the sort options' do
147
191
  let!(:sort_name) { :quantity }
148
192
  let!(:column_name_option) { :quantity }
193
+ let!(:table_name_option) { 'quotes' }
149
194
  let!(:default_option) { 'DESC' }
150
195
  let!(:joins_option) { nil }
151
196
  let!(:clause_option) { "custom.joins_clause" }
@@ -164,6 +209,52 @@ describe SortThis::ActiveRecord do
164
209
  Quote.default_sort_columns[sort_name].should == "#{clause_option} #{default_option}"
165
210
  end
166
211
  end
212
+
213
+ context 'given a sort column where the table name is overridden' do
214
+ context 'when the table name option is already pluralized' do
215
+ let!(:sort_name) { :price }
216
+ let!(:column_name_option) { :price }
217
+ let!(:table_name_option) { 'some_other_tables' }
218
+ let!(:default_option) { nil }
219
+ let!(:joins_option) { nil }
220
+ let!(:clause_option) { "some_other_tables.price" }
221
+
222
+ before(:each) do
223
+ Quote.sort_this sort_name => {:table_name => table_name_option}
224
+ end
225
+
226
+ it_should_behave_like 'sort_columns_defined'
227
+ end
228
+
229
+ context 'when the table name option is not pluralized' do
230
+ let!(:table_name_singular) { 'another_table' }
231
+
232
+ let!(:sort_name) { :price }
233
+ let!(:column_name_option) { :price }
234
+ let!(:table_name_option) { 'another_tables' }
235
+ let!(:default_option) { nil }
236
+ let!(:joins_option) { nil }
237
+ let!(:clause_option) { "another_tables.price" }
238
+
239
+ before(:each) do
240
+ Quote.sort_this sort_name => {:table_name => table_name_option}
241
+ end
242
+
243
+ it_should_behave_like 'sort_columns_defined'
244
+ end
245
+ end
246
+ end
247
+
248
+ describe '.default_sort' do
249
+ let!(:product1) { create(:product, :name => 'D Name') }
250
+ let!(:product2) { create(:product, :name => 'B Name') }
251
+ let!(:product3) { create(:product, :name => 'A Name') }
252
+ let!(:product4) { create(:product, :name => 'C Name') }
253
+
254
+ it 'should return a sorted collection given by the specified default sort' do
255
+ Product.sort_this(:name => { :column_name => :name, :default => 'DESC' })
256
+ Product.default_sort.should == [product1, product4, product2, product3]
257
+ end
167
258
  end
168
259
 
169
260
  describe '.sort' do
@@ -185,8 +276,12 @@ describe SortThis::ActiveRecord do
185
276
  before(:each) do
186
277
  Quote.sort_this :price => {:column_name => :price, :default => 'ASC'},
187
278
  :quantity => {:column_name => :quantity},
188
- :product_name => {:column_name => :name, :joins => :product},
189
- :vendor_name => {:column_name => :name, :joins => :vendor}
279
+ :product_name => {:column_name => :name, :table_name => 'products', :joins => :product},
280
+ :vendor_name => {:column_name => :name, :table_name => 'vendors', :joins => :vendor}
281
+ end
282
+
283
+ it 'should return a relation' do
284
+ Quote.sort.class.should == ActiveRecord::Relation
190
285
  end
191
286
 
192
287
  context 'default parameters' do
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'debugger'
2
1
  require 'sqlite3'
3
2
  require 'active_record'
4
3
  require 'factory_girl'
@@ -30,10 +30,14 @@ def teardown_db
30
30
  end
31
31
 
32
32
  class Product < ActiveRecord::Base
33
+ include SortThis::ActiveRecord
34
+
33
35
  has_many :quotes
34
36
  end
35
37
 
36
38
  class Vendor < ActiveRecord::Base
39
+ include SortThis::ActiveRecord
40
+
37
41
  has_many :quotes
38
42
  end
39
43
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sort_this
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-28 00:00:00.000000000 Z
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -43,102 +43,6 @@ dependencies:
43
43
  - - '='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.2.0
46
- - !ruby/object:Gem::Dependency
47
- name: debugger
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - '='
52
- - !ruby/object:Gem::Version
53
- version: 1.2.2
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - '='
60
- - !ruby/object:Gem::Version
61
- version: 1.2.2
62
- - !ruby/object:Gem::Dependency
63
- name: rspec
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - '='
68
- - !ruby/object:Gem::Version
69
- version: 2.12.0
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - '='
76
- - !ruby/object:Gem::Version
77
- version: 2.12.0
78
- - !ruby/object:Gem::Dependency
79
- name: sqlite3
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - '='
84
- - !ruby/object:Gem::Version
85
- version: 1.3.6
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - '='
92
- - !ruby/object:Gem::Version
93
- version: 1.3.6
94
- - !ruby/object:Gem::Dependency
95
- name: factory_girl
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - '='
100
- - !ruby/object:Gem::Version
101
- version: 4.1.0
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - '='
108
- - !ruby/object:Gem::Version
109
- version: 4.1.0
110
- - !ruby/object:Gem::Dependency
111
- name: database_cleaner
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - '='
116
- - !ruby/object:Gem::Version
117
- version: 0.9.1
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - '='
124
- - !ruby/object:Gem::Version
125
- version: 0.9.1
126
- - !ruby/object:Gem::Dependency
127
- name: capybara
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - '='
132
- - !ruby/object:Gem::Version
133
- version: 2.0.1
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - '='
140
- - !ruby/object:Gem::Version
141
- version: 2.0.1
142
46
  description: SortThis
143
47
  email:
144
48
  - s.pullen05@gmail.com
@@ -149,8 +53,13 @@ files:
149
53
  - .gitignore
150
54
  - .rspec
151
55
  - .rvmrc
56
+ - .travis.yml
152
57
  - CHANGELOG.md
153
58
  - Gemfile
59
+ - Gemfile.rails3.0
60
+ - Gemfile.rails3.0.lock
61
+ - Gemfile.rails3.1
62
+ - Gemfile.rails3.1.lock
154
63
  - LICENSE.txt
155
64
  - README.md
156
65
  - Rakefile
@@ -158,6 +67,7 @@ files:
158
67
  - lib/sort_this/action_controller.rb
159
68
  - lib/sort_this/active_record.rb
160
69
  - lib/sort_this/railtie.rb
70
+ - lib/sort_this/sort_this_error.rb
161
71
  - lib/sort_this/version.rb
162
72
  - lib/sort_this/view_helpers/action_view.rb
163
73
  - sort_this.gemspec
@@ -182,16 +92,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
92
  version: '0'
183
93
  segments:
184
94
  - 0
185
- hash: 2103404262135696406
95
+ hash: -2410530305062139336
186
96
  required_rubygems_version: !ruby/object:Gem::Requirement
187
97
  none: false
188
98
  requirements:
189
99
  - - ! '>='
190
100
  - !ruby/object:Gem::Version
191
101
  version: '0'
192
- segments:
193
- - 0
194
- hash: 2103404262135696406
195
102
  requirements: []
196
103
  rubyforge_project:
197
104
  rubygems_version: 1.8.24