sorted 0.4.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.travis.yml +3 -1
- data/Gemfile +1 -1
- data/README.md +43 -0
- data/Rakefile +12 -2
- data/gemfiles/active_record_40.gemfile +9 -0
- data/gemfiles/active_record_40.gemfile.lock +92 -0
- data/gemfiles/mongoid_30.gemfile +12 -0
- data/gemfiles/mongoid_30.gemfile.lock +106 -0
- data/lib/sorted/orms/active_record.rb +7 -4
- data/lib/sorted/orms/mongoid.rb +18 -0
- data/lib/sorted/parser.rb +9 -6
- data/lib/sorted/railtie.rb +11 -3
- data/lib/sorted/version.rb +1 -1
- data/lib/sorted/view_helpers/action_view.rb +26 -1
- data/sorted.gemspec +8 -6
- data/spec/sorted/orms/active_record_spec.rb +19 -19
- data/spec/sorted/orms/mongoid_spec.rb +32 -0
- data/spec/sorted/parser_spec.rb +22 -4
- data/spec/spec_helper.rb +10 -0
- metadata +69 -38
- data/README.rdoc +0 -27
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0262798feaab6b58860df0dd56c6eda414263e58
|
4
|
+
data.tar.gz: dde3ec5870721ddc59198f98132ea9ca03c7c763
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8dc76069eeb8b172e3c6096588f9bc12404d44c747800fc3332f5e82344d476e1d22cab0a8bcc2110aa0c3292cc8c72fe3e6839cd28d0e92e2bb663696ab01d6
|
7
|
+
data.tar.gz: 18aec9e23e623f26be39632328d190c4213d7f960abb162b65337cfd2a9e7191775e303cee573ed63c471ae814b5368bd2d2d634c9ead24e8582df3c2db02b80
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# sorted
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/mynameisrufus/sorted.png?branch=master)](https://travis-ci.org/mynameisrufus/sorted)
|
4
|
+
|
5
|
+
Sorted is a simple object that will take an sql order string and a url
|
6
|
+
sort string to let you sort large datasets over many pages (using
|
7
|
+
[will_paginate](https://github.com/mislav/will_paginate) or
|
8
|
+
[kaminari](https://github.com/amatsuda/kaminari)) without losing state.
|
9
|
+
|
10
|
+
### View
|
11
|
+
|
12
|
+
Generate a sorted link with the email attribute:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
link_to_sorted "Email", :email
|
16
|
+
```
|
17
|
+
|
18
|
+
Works the same as the `link_to` method except a second argument for the
|
19
|
+
sort attribute is needed.
|
20
|
+
|
21
|
+
### Ruby 1.8.7 Rails 3.x
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'sorted', '~> 0.4.3'
|
25
|
+
```
|
26
|
+
|
27
|
+
### Model
|
28
|
+
|
29
|
+
Using the `sorted` method with the optional default order argument:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
@users = User.sorted(params[:sort], "email ASC").page(params[:page])
|
33
|
+
```
|
34
|
+
|
35
|
+
### Rubies
|
36
|
+
|
37
|
+
* MRI 1.9.3, 2.0.0.
|
38
|
+
* JRuby 1.9 mode
|
39
|
+
|
40
|
+
### ORMs
|
41
|
+
|
42
|
+
* ActiveRecord
|
43
|
+
* Mongoid
|
data/Rakefile
CHANGED
@@ -7,14 +7,24 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
7
7
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
8
8
|
end
|
9
9
|
|
10
|
-
task :default => :
|
10
|
+
task :default => "spec:all"
|
11
11
|
|
12
12
|
require 'rdoc/task'
|
13
13
|
Rake::RDocTask.new do |rdoc|
|
14
14
|
require 'sorted/version'
|
15
15
|
|
16
16
|
rdoc.rdoc_dir = 'rdoc'
|
17
|
-
rdoc.title = "
|
17
|
+
rdoc.title = "sorted #{Sorted::VERSION}"
|
18
18
|
rdoc.rdoc_files.include('README*')
|
19
19
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
20
20
|
end
|
21
|
+
|
22
|
+
namespace :spec do
|
23
|
+
desc "Run Tests against all ORMs"
|
24
|
+
task :all do
|
25
|
+
%w(active_record_40 mongoid_30).each do |gemfile|
|
26
|
+
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
|
27
|
+
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake -t spec"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'railties', '>= 4.0.0'
|
4
|
+
gem 'activerecord', '>= 4.0.0', require: 'active_record'
|
5
|
+
gem 'rspec-rails', '>= 2.0'
|
6
|
+
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
|
7
|
+
gem 'sqlite3', '>= 1.3.5', platforms: :ruby
|
8
|
+
|
9
|
+
gemspec path: '../'
|
@@ -0,0 +1,92 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/rufuspost/Projects/ruby/gems/sorted
|
3
|
+
specs:
|
4
|
+
sorted (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (4.0.0)
|
10
|
+
activesupport (= 4.0.0)
|
11
|
+
builder (~> 3.1.0)
|
12
|
+
erubis (~> 2.7.0)
|
13
|
+
rack (~> 1.5.2)
|
14
|
+
rack-test (~> 0.6.2)
|
15
|
+
activemodel (4.0.0)
|
16
|
+
activesupport (= 4.0.0)
|
17
|
+
builder (~> 3.1.0)
|
18
|
+
activerecord (4.0.0)
|
19
|
+
activemodel (= 4.0.0)
|
20
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
21
|
+
activesupport (= 4.0.0)
|
22
|
+
arel (~> 4.0.0)
|
23
|
+
activerecord-deprecated_finders (1.0.3)
|
24
|
+
activerecord-jdbc-adapter (1.3.3)
|
25
|
+
activerecord (>= 2.2)
|
26
|
+
activerecord-jdbcsqlite3-adapter (1.3.3)
|
27
|
+
activerecord-jdbc-adapter (~> 1.3.3)
|
28
|
+
jdbc-sqlite3 (~> 3.7.2)
|
29
|
+
activesupport (4.0.0)
|
30
|
+
i18n (~> 0.6, >= 0.6.4)
|
31
|
+
minitest (~> 4.2)
|
32
|
+
multi_json (~> 1.3)
|
33
|
+
thread_safe (~> 0.1)
|
34
|
+
tzinfo (~> 0.3.37)
|
35
|
+
arel (4.0.1)
|
36
|
+
atomic (1.1.14)
|
37
|
+
atomic (1.1.14-java)
|
38
|
+
builder (3.1.4)
|
39
|
+
diff-lcs (1.2.4)
|
40
|
+
erubis (2.7.0)
|
41
|
+
i18n (0.6.5)
|
42
|
+
jdbc-sqlite3 (3.7.2.1)
|
43
|
+
minitest (4.7.5)
|
44
|
+
multi_json (1.8.2)
|
45
|
+
rack (1.5.2)
|
46
|
+
rack-test (0.6.2)
|
47
|
+
rack (>= 1.0)
|
48
|
+
railties (4.0.0)
|
49
|
+
actionpack (= 4.0.0)
|
50
|
+
activesupport (= 4.0.0)
|
51
|
+
rake (>= 0.8.7)
|
52
|
+
thor (>= 0.18.1, < 2.0)
|
53
|
+
rake (10.1.0)
|
54
|
+
rspec (2.14.1)
|
55
|
+
rspec-core (~> 2.14.0)
|
56
|
+
rspec-expectations (~> 2.14.0)
|
57
|
+
rspec-mocks (~> 2.14.0)
|
58
|
+
rspec-core (2.14.6)
|
59
|
+
rspec-expectations (2.14.3)
|
60
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
61
|
+
rspec-mocks (2.14.4)
|
62
|
+
rspec-rails (2.14.0)
|
63
|
+
actionpack (>= 3.0)
|
64
|
+
activesupport (>= 3.0)
|
65
|
+
railties (>= 3.0)
|
66
|
+
rspec-core (~> 2.14.0)
|
67
|
+
rspec-expectations (~> 2.14.0)
|
68
|
+
rspec-mocks (~> 2.14.0)
|
69
|
+
sqlite3 (1.3.8)
|
70
|
+
thor (0.18.1)
|
71
|
+
thread_safe (0.1.3)
|
72
|
+
atomic
|
73
|
+
thread_safe (0.1.3-java)
|
74
|
+
atomic
|
75
|
+
tzinfo (0.3.38)
|
76
|
+
|
77
|
+
PLATFORMS
|
78
|
+
java
|
79
|
+
ruby
|
80
|
+
|
81
|
+
DEPENDENCIES
|
82
|
+
actionpack (>= 3.0.0)
|
83
|
+
activerecord (>= 4.0.0)
|
84
|
+
activerecord-jdbcsqlite3-adapter
|
85
|
+
activesupport (>= 3.0.0)
|
86
|
+
bundler (>= 1.0.0)
|
87
|
+
railties (>= 4.0.0)
|
88
|
+
rake
|
89
|
+
rspec (>= 2.0.0)
|
90
|
+
rspec-rails (>= 2.0)
|
91
|
+
sorted!
|
92
|
+
sqlite3 (>= 1.3.5)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'railties', '>= 4'
|
4
|
+
gem 'mongoid', '~> 4', github: 'mongoid/mongoid', require: 'mongoid'
|
5
|
+
gem 'rspec-rails', '>= 2.0'
|
6
|
+
gem 'origin'
|
7
|
+
gem 'moped'
|
8
|
+
gem 'nokogiri'
|
9
|
+
gem 'xpath'
|
10
|
+
gem 'mime-types'
|
11
|
+
|
12
|
+
gemspec path: '../'
|
@@ -0,0 +1,106 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/mongoid/mongoid.git
|
3
|
+
revision: 9b3bc1264032209b7a6c0e82d0ca656f401e476b
|
4
|
+
specs:
|
5
|
+
mongoid (4.0.0)
|
6
|
+
activemodel (~> 4.0.0)
|
7
|
+
moped (~> 2.0.beta3)
|
8
|
+
origin (~> 1.0)
|
9
|
+
tzinfo (~> 0.3.37)
|
10
|
+
|
11
|
+
PATH
|
12
|
+
remote: /Users/rufuspost/Projects/ruby/gems/sorted
|
13
|
+
specs:
|
14
|
+
sorted (1.0.0)
|
15
|
+
|
16
|
+
GEM
|
17
|
+
remote: https://rubygems.org/
|
18
|
+
specs:
|
19
|
+
actionpack (4.0.0)
|
20
|
+
activesupport (= 4.0.0)
|
21
|
+
builder (~> 3.1.0)
|
22
|
+
erubis (~> 2.7.0)
|
23
|
+
rack (~> 1.5.2)
|
24
|
+
rack-test (~> 0.6.2)
|
25
|
+
activemodel (4.0.0)
|
26
|
+
activesupport (= 4.0.0)
|
27
|
+
builder (~> 3.1.0)
|
28
|
+
activesupport (4.0.0)
|
29
|
+
i18n (~> 0.6, >= 0.6.4)
|
30
|
+
minitest (~> 4.2)
|
31
|
+
multi_json (~> 1.3)
|
32
|
+
thread_safe (~> 0.1)
|
33
|
+
tzinfo (~> 0.3.37)
|
34
|
+
atomic (1.1.14)
|
35
|
+
atomic (1.1.14-java)
|
36
|
+
bson (2.0.0.rc3)
|
37
|
+
bson (2.0.0.rc3-java)
|
38
|
+
builder (3.1.4)
|
39
|
+
diff-lcs (1.2.4)
|
40
|
+
erubis (2.7.0)
|
41
|
+
i18n (0.6.5)
|
42
|
+
mime-types (2.0)
|
43
|
+
mini_portile (0.5.2)
|
44
|
+
minitest (4.7.5)
|
45
|
+
moped (2.0.0.beta3)
|
46
|
+
bson (~> 2.0.0.rc3)
|
47
|
+
optionable (~> 0.1.1)
|
48
|
+
multi_json (1.8.2)
|
49
|
+
nokogiri (1.6.0)
|
50
|
+
mini_portile (~> 0.5.0)
|
51
|
+
nokogiri (1.6.0-java)
|
52
|
+
mini_portile (~> 0.5.0)
|
53
|
+
optionable (0.1.1)
|
54
|
+
origin (1.1.0)
|
55
|
+
rack (1.5.2)
|
56
|
+
rack-test (0.6.2)
|
57
|
+
rack (>= 1.0)
|
58
|
+
railties (4.0.0)
|
59
|
+
actionpack (= 4.0.0)
|
60
|
+
activesupport (= 4.0.0)
|
61
|
+
rake (>= 0.8.7)
|
62
|
+
thor (>= 0.18.1, < 2.0)
|
63
|
+
rake (10.1.0)
|
64
|
+
rspec (2.14.1)
|
65
|
+
rspec-core (~> 2.14.0)
|
66
|
+
rspec-expectations (~> 2.14.0)
|
67
|
+
rspec-mocks (~> 2.14.0)
|
68
|
+
rspec-core (2.14.7)
|
69
|
+
rspec-expectations (2.14.3)
|
70
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
71
|
+
rspec-mocks (2.14.4)
|
72
|
+
rspec-rails (2.14.0)
|
73
|
+
actionpack (>= 3.0)
|
74
|
+
activesupport (>= 3.0)
|
75
|
+
railties (>= 3.0)
|
76
|
+
rspec-core (~> 2.14.0)
|
77
|
+
rspec-expectations (~> 2.14.0)
|
78
|
+
rspec-mocks (~> 2.14.0)
|
79
|
+
thor (0.18.1)
|
80
|
+
thread_safe (0.1.3)
|
81
|
+
atomic
|
82
|
+
thread_safe (0.1.3-java)
|
83
|
+
atomic
|
84
|
+
tzinfo (0.3.38)
|
85
|
+
xpath (2.0.0)
|
86
|
+
nokogiri (~> 1.3)
|
87
|
+
|
88
|
+
PLATFORMS
|
89
|
+
java
|
90
|
+
ruby
|
91
|
+
|
92
|
+
DEPENDENCIES
|
93
|
+
actionpack (>= 3.0.0)
|
94
|
+
activesupport (>= 3.0.0)
|
95
|
+
bundler (>= 1.0.0)
|
96
|
+
mime-types
|
97
|
+
mongoid (~> 4)!
|
98
|
+
moped
|
99
|
+
nokogiri
|
100
|
+
origin
|
101
|
+
railties (>= 4)
|
102
|
+
rake
|
103
|
+
rspec (>= 2.0.0)
|
104
|
+
rspec-rails (>= 2.0)
|
105
|
+
sorted!
|
106
|
+
xpath
|
@@ -1,13 +1,16 @@
|
|
1
|
-
require 'active_record'
|
2
1
|
require 'sorted'
|
2
|
+
require 'active_support/concern'
|
3
3
|
|
4
4
|
module Sorted
|
5
5
|
module Orms
|
6
6
|
module ActiveRecord
|
7
|
-
|
8
|
-
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
def self.sorted(sort, default_order = nil)
|
9
11
|
sorter = ::Sorted::Parser.new(sort, default_order)
|
10
|
-
|
12
|
+
quoter = ->(frag) { connection.quote_column_name(frag) }
|
13
|
+
order sorter.to_sql(quoter)
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'sorted'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module Sorted
|
5
|
+
module Orms
|
6
|
+
module Mongoid
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
SQL_TO_MONGO = { "asc" => 1, "desc" => -1 }
|
9
|
+
|
10
|
+
included do
|
11
|
+
def self.sorted(sort, default_order = nil)
|
12
|
+
sorter = ::Sorted::Parser.new(sort, default_order)
|
13
|
+
order_by sorter.to_hash.merge(sorter) { |key, val| SQL_TO_MONGO[val] }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/sorted/parser.rb
CHANGED
@@ -35,13 +35,16 @@ module Sorted
|
|
35
35
|
end
|
36
36
|
end.compact
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def to_hash
|
40
40
|
array.inject({}){|h,a| h.merge(Hash[a[0],a[1]])}
|
41
41
|
end
|
42
|
-
|
43
|
-
def to_sql
|
44
|
-
array.map
|
42
|
+
|
43
|
+
def to_sql(quoter = ->(frag) { frag })
|
44
|
+
array.map do |a|
|
45
|
+
column = a[0].split('.').map{ |frag| quoter.call(frag) }.join('.')
|
46
|
+
"#{column} #{a[1].upcase}"
|
47
|
+
end.join(', ')
|
45
48
|
end
|
46
49
|
|
47
50
|
def to_s
|
@@ -51,12 +54,12 @@ module Sorted
|
|
51
54
|
def to_a
|
52
55
|
array
|
53
56
|
end
|
54
|
-
|
57
|
+
|
55
58
|
def toggle
|
56
59
|
@array = Toggler.new(sorts, orders).to_a
|
57
60
|
self
|
58
61
|
end
|
59
|
-
|
62
|
+
|
60
63
|
def reset
|
61
64
|
@array = default
|
62
65
|
self
|
data/lib/sorted/railtie.rb
CHANGED
@@ -3,10 +3,18 @@ require 'sorted'
|
|
3
3
|
module Sorted
|
4
4
|
class Railtie < Rails::Railtie
|
5
5
|
initializer "sorted.configure" do |app|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
if defined? ::ActiveRecord
|
7
|
+
ActiveSupport.on_load :active_record do
|
8
|
+
require 'sorted/orms/active_record'
|
9
|
+
include Sorted::Orms::ActiveRecord
|
10
|
+
end
|
9
11
|
end
|
12
|
+
|
13
|
+
if defined? ::Mongoid
|
14
|
+
require 'sorted/orms/mongoid'
|
15
|
+
::Mongoid::Document.send :include, Sorted::Orms::Mongoid
|
16
|
+
end
|
17
|
+
|
10
18
|
ActiveSupport.on_load :action_view do
|
11
19
|
require 'sorted/view_helpers/action_view'
|
12
20
|
include Sorted::ViewHelpers::ActionView
|
data/lib/sorted/version.rb
CHANGED
@@ -54,7 +54,32 @@ module Sorted
|
|
54
54
|
|
55
55
|
sorter = SortedViewHelper.new(order, ((request.get? && !params.nil?) ? params.dup : {}))
|
56
56
|
options[:class] = [options[:class], sorter.css].join(' ').strip
|
57
|
-
link_to(sorter.params, options, html_options, &block)
|
57
|
+
link_to(url_for(sorter.params), options, html_options, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Convenience method for quickly spitting out a sorting menu.
|
61
|
+
#
|
62
|
+
# ==== Examples
|
63
|
+
#
|
64
|
+
# Basic usage
|
65
|
+
#
|
66
|
+
# sort_by :first_name, :last_name
|
67
|
+
#
|
68
|
+
# To provide a string to use instead of a column name, pass an array composed
|
69
|
+
# of your label string and the column name (symbol):
|
70
|
+
#
|
71
|
+
# sortable_by :author_name, :title, ["Date of Publication", :published_at]
|
72
|
+
#
|
73
|
+
def sortable_by(*columns)
|
74
|
+
links = content_tag :span, "Sort by: "
|
75
|
+
columns.each do |c|
|
76
|
+
if c.is_a? Array
|
77
|
+
links += link_to_sorted(c[0],c[1].to_sym)
|
78
|
+
else
|
79
|
+
links += link_to_sorted(c.to_s.titleize, c.to_sym)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
content_tag :div, links, class: 'sortable'
|
58
83
|
end
|
59
84
|
end
|
60
85
|
end
|
data/sorted.gemspec
CHANGED
@@ -5,19 +5,21 @@ Gem::Specification.new do |s|
|
|
5
5
|
s.name = "sorted"
|
6
6
|
s.version = Sorted::VERSION
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
|
-
s.authors = ["Rufus Post"]
|
9
|
-
s.email = ["rufuspost@gmail.com"]
|
8
|
+
s.authors = ["Rufus Post", "Daniel Leavitt"]
|
9
|
+
s.email = ["rufuspost@gmail.com", "daniel.leavitt@gmail.com"]
|
10
10
|
s.homepage = "http://rubygems.org/gems/sorted"
|
11
|
-
s.summary =
|
12
|
-
s.description =
|
11
|
+
s.summary = %q{Data sorting library}
|
12
|
+
s.description = %q{Allows you to sort large datasets over many pages, without losing state.}
|
13
|
+
s.license = "MIT"
|
13
14
|
|
14
15
|
s.required_rubygems_version = ">= 1.3.6"
|
15
16
|
s.rubyforge_project = "sorted"
|
16
17
|
|
18
|
+
s.add_development_dependency "rake", ">= 0"
|
17
19
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
18
|
-
s.add_development_dependency "rails", ">= 3.1.2"
|
19
20
|
s.add_development_dependency "rspec", ">= 2.0.0"
|
20
|
-
s.add_development_dependency "
|
21
|
+
s.add_development_dependency "activesupport", ">= 3.0.0"
|
22
|
+
s.add_development_dependency "actionpack", ">= 3.0.0"
|
21
23
|
|
22
24
|
s.files = `git ls-files`.split("\n")
|
23
25
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
@@ -1,27 +1,27 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
class SortedActiveRecordTest < ActiveRecord::Base
|
7
|
-
establish_connection :adapter => 'sqlite3', :database => '/tmp/foobar.db'
|
3
|
+
if defined? ActiveRecord
|
4
|
+
describe Sorted::Orms::ActiveRecord do
|
5
|
+
ActiveRecord::Base.send(:include, Sorted::Orms::ActiveRecord)
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
7
|
+
class SortedActiveRecordTest < ActiveRecord::Base
|
8
|
+
establish_connection adapter: 'sqlite3', database: ':memory:'
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
10
|
+
connection.create_table table_name, force: true do |t|
|
11
|
+
t.string :name
|
12
|
+
end
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
end
|
14
|
+
scope :page, -> { limit(50) }
|
15
|
+
end
|
21
16
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
17
|
+
it "should integrate with ActiveRecord::Base" do
|
18
|
+
SortedActiveRecordTest.should respond_to(:sorted)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should play nice with other scopes" do
|
22
|
+
sql = "SELECT \"sorted_active_record_tests\".* FROM \"sorted_active_record_tests\" WHERE \"sorted_active_record_tests\".\"name\" = 'bob' ORDER BY \"name\" ASC LIMIT 50"
|
23
|
+
SortedActiveRecordTest.where(:name => 'bob').page.sorted(nil, 'name ASC').to_sql.should == sql
|
24
|
+
SortedActiveRecordTest.page.sorted(nil, 'name ASC').where(:name => 'bob').to_sql.should == sql
|
25
|
+
end
|
26
26
|
end
|
27
27
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? Mongoid
|
4
|
+
describe Sorted::Orms::Mongoid do
|
5
|
+
::Mongoid::Criteria.send :include, Sorted::Orms::Mongoid
|
6
|
+
::Mongoid::Document.send :include, Sorted::Orms::Mongoid
|
7
|
+
|
8
|
+
class SortedMongoidTest
|
9
|
+
include ::Mongoid::Document
|
10
|
+
|
11
|
+
field :name, type: String
|
12
|
+
|
13
|
+
def self.page
|
14
|
+
limit(50)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should integrate with Mongoid::Document" do
|
19
|
+
SortedMongoidTest.should respond_to(:sorted)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should integrate with Mongoid::Criteria" do
|
23
|
+
SortedMongoidTest.page.should respond_to(:sorted)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should play nice with other scopes" do
|
27
|
+
query_options = { limit: 50, sort: { "name" => 1 } }
|
28
|
+
SortedMongoidTest.page.sorted(nil, 'name ASC').options.should == query_options
|
29
|
+
SortedMongoidTest.page.sorted(nil, 'name ASC').options.should == query_options
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/sorted/parser_spec.rb
CHANGED
@@ -61,13 +61,31 @@ describe Sorted::Parser, "params parsing" do
|
|
61
61
|
end
|
62
62
|
|
63
63
|
describe Sorted::Parser, "return types" do
|
64
|
+
module FakeConnection
|
65
|
+
def self.quote_column_name(column_name)
|
66
|
+
"`#{column_name}`"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
let(:quoter) {
|
71
|
+
->(frag) { FakeConnection.quote_column_name(frag) }
|
72
|
+
}
|
73
|
+
|
74
|
+
it "should properly escape sql column names" do
|
75
|
+
order = "users.name DESC"
|
76
|
+
result = "`users`.`name` DESC"
|
77
|
+
|
78
|
+
sorter = Sorted::Parser.new(nil, order)
|
79
|
+
sorter.to_sql(quoter).should eq result
|
80
|
+
end
|
81
|
+
|
64
82
|
it "should return an sql sort string" do
|
65
83
|
sort = "email_desc!name_desc"
|
66
84
|
order = "email ASC, phone ASC, name DESC"
|
67
|
-
result = "email DESC, name DESC, phone ASC"
|
85
|
+
result = "`email` DESC, `name` DESC, `phone` ASC"
|
68
86
|
|
69
87
|
sorter = Sorted::Parser.new(sort, order)
|
70
|
-
sorter.to_sql.should eq result
|
88
|
+
sorter.to_sql(quoter).should eq result
|
71
89
|
end
|
72
90
|
|
73
91
|
it "should return an hash" do
|
@@ -91,9 +109,9 @@ describe Sorted::Parser, "return types" do
|
|
91
109
|
it "sql injection using order by clause should not work" do
|
92
110
|
sort = "(case+when+((ASCII(SUBSTR((select+table_name+from+all_tables+where+rownum%3d1),1))>%3D128))+then+id+else+something+end)"
|
93
111
|
order = "email ASC, phone ASC, name DESC"
|
94
|
-
result = "email ASC, phone ASC, name DESC"
|
112
|
+
result = "`email` ASC, `phone` ASC, `name` DESC"
|
95
113
|
|
96
114
|
sorter = Sorted::Parser.new(sort, order)
|
97
|
-
sorter.to_sql.should eq result
|
115
|
+
sorter.to_sql(quoter).should eq result
|
98
116
|
end
|
99
117
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rails'
|
6
|
+
rescue LoadError
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'bundler/setup'
|
10
|
+
Bundler.require
|
11
|
+
|
3
12
|
require 'sorted'
|
13
|
+
require 'sorted/orms/mongoid'
|
4
14
|
require 'sorted/orms/active_record'
|
5
15
|
require 'sorted/view_helpers/action_view'
|
6
16
|
require 'rspec'
|
metadata
CHANGED
@@ -1,75 +1,108 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sorted
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Rufus Post
|
8
|
+
- Daniel Leavitt
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
18
17
|
requirements:
|
19
|
-
- -
|
18
|
+
- - '>='
|
20
19
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
20
|
+
version: '0'
|
22
21
|
type: :development
|
23
22
|
prerelease: false
|
24
|
-
version_requirements:
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
25
28
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
29
31
|
requirements:
|
30
|
-
- -
|
32
|
+
- - '>='
|
31
33
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
34
|
+
version: 1.0.0
|
33
35
|
type: :development
|
34
36
|
prerelease: false
|
35
|
-
version_requirements:
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.0.0
|
36
42
|
- !ruby/object:Gem::Dependency
|
37
43
|
name: rspec
|
38
|
-
requirement:
|
39
|
-
none: false
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
40
45
|
requirements:
|
41
|
-
- -
|
46
|
+
- - '>='
|
42
47
|
- !ruby/object:Gem::Version
|
43
48
|
version: 2.0.0
|
44
49
|
type: :development
|
45
50
|
prerelease: false
|
46
|
-
version_requirements:
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.0.0
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: activesupport
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.0.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.0.0
|
47
70
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement:
|
50
|
-
none: false
|
71
|
+
name: actionpack
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
51
73
|
requirements:
|
52
|
-
- -
|
74
|
+
- - '>='
|
53
75
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
76
|
+
version: 3.0.0
|
55
77
|
type: :development
|
56
78
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 3.0.0
|
84
|
+
description: Allows you to sort large datasets over many pages, without losing state.
|
59
85
|
email:
|
60
86
|
- rufuspost@gmail.com
|
87
|
+
- daniel.leavitt@gmail.com
|
61
88
|
executables: []
|
62
89
|
extensions: []
|
63
90
|
extra_rdoc_files: []
|
64
91
|
files:
|
65
92
|
- .gitignore
|
93
|
+
- .rspec
|
66
94
|
- .travis.yml
|
67
95
|
- Gemfile
|
68
96
|
- LICENSE
|
69
|
-
- README.
|
97
|
+
- README.md
|
70
98
|
- Rakefile
|
99
|
+
- gemfiles/active_record_40.gemfile
|
100
|
+
- gemfiles/active_record_40.gemfile.lock
|
101
|
+
- gemfiles/mongoid_30.gemfile
|
102
|
+
- gemfiles/mongoid_30.gemfile.lock
|
71
103
|
- lib/sorted.rb
|
72
104
|
- lib/sorted/orms/active_record.rb
|
105
|
+
- lib/sorted/orms/mongoid.rb
|
73
106
|
- lib/sorted/parser.rb
|
74
107
|
- lib/sorted/railtie.rb
|
75
108
|
- lib/sorted/toggler.rb
|
@@ -77,35 +110,33 @@ files:
|
|
77
110
|
- lib/sorted/view_helpers/action_view.rb
|
78
111
|
- sorted.gemspec
|
79
112
|
- spec/sorted/orms/active_record_spec.rb
|
113
|
+
- spec/sorted/orms/mongoid_spec.rb
|
80
114
|
- spec/sorted/parser_spec.rb
|
81
115
|
- spec/sorted/toggler_spec.rb
|
82
116
|
- spec/sorted/view_helpers/action_view_spec.rb
|
83
117
|
- spec/spec_helper.rb
|
84
118
|
homepage: http://rubygems.org/gems/sorted
|
85
|
-
licenses:
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
86
122
|
post_install_message:
|
87
123
|
rdoc_options: []
|
88
124
|
require_paths:
|
89
125
|
- lib
|
90
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
-
none: false
|
92
127
|
requirements:
|
93
|
-
- -
|
128
|
+
- - '>='
|
94
129
|
- !ruby/object:Gem::Version
|
95
130
|
version: '0'
|
96
|
-
segments:
|
97
|
-
- 0
|
98
|
-
hash: -4246959904636690833
|
99
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
-
none: false
|
101
132
|
requirements:
|
102
|
-
- -
|
133
|
+
- - '>='
|
103
134
|
- !ruby/object:Gem::Version
|
104
135
|
version: 1.3.6
|
105
136
|
requirements: []
|
106
137
|
rubyforge_project: sorted
|
107
|
-
rubygems_version:
|
138
|
+
rubygems_version: 2.0.0
|
108
139
|
signing_key:
|
109
|
-
specification_version:
|
110
|
-
summary:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Data sorting library
|
111
142
|
test_files: []
|
data/README.rdoc
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
= sorted
|
2
|
-
|
3
|
-
Sorted is a simple object that will take an sql order string and a url
|
4
|
-
sort string to let you sort large datasets over many pages (using
|
5
|
-
{will_paginate}[http://github.com/mislav/will_paginatea] or
|
6
|
-
{kaminari}[https://github.com/amatsuda/kaminari]) without loosing state.
|
7
|
-
|
8
|
-
{<img src="https://secure.travis-ci.org/mynameisrufus/sorted.png" />}[http://travis-ci.org/mynameisrufus/sorted]
|
9
|
-
|
10
|
-
=== Gemfile
|
11
|
-
|
12
|
-
gem 'sorted', '~> 0.4.3'
|
13
|
-
|
14
|
-
=== View
|
15
|
-
|
16
|
-
Generate a sorted link with the email attribute:
|
17
|
-
|
18
|
-
link_to_sorted "Email", :email
|
19
|
-
|
20
|
-
Works the same as the +link_to+ method except a second argument for the
|
21
|
-
sort attribute is needed.
|
22
|
-
|
23
|
-
=== Model
|
24
|
-
|
25
|
-
Using the +sorted+ method with the optional default order argument:
|
26
|
-
|
27
|
-
@users = User.sorted(params[:sort], "email ASC").page(params[:page])
|