sorted 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -43
- data/Rakefile +12 -18
- data/lib/sorted.rb +19 -6
- data/lib/sorted/version.rb +1 -1
- data/sorted.gemspec +3 -5
- data/spec/json_query_spec.rb +20 -0
- data/spec/set_spec.rb +74 -0
- data/spec/spec_helper.rb +1 -4
- data/spec/sql_query_spec.rb +61 -0
- data/spec/uri_query_spec.rb +20 -0
- metadata +7 -48
- data/gemfiles/active_record_40.gemfile +0 -9
- data/gemfiles/active_record_40.gemfile.lock +0 -109
- data/gemfiles/mongoid_30.gemfile +0 -7
- data/gemfiles/mongoid_30.gemfile.lock +0 -113
- data/lib/sorted/orms/active_record.rb +0 -34
- data/lib/sorted/orms/mongoid.rb +0 -18
- data/lib/sorted/parser.rb +0 -68
- data/lib/sorted/railtie.rb +0 -24
- data/lib/sorted/toggler.rb +0 -27
- data/lib/sorted/view_helpers/action_view.rb +0 -86
- data/spec/sorted/orms/active_record_spec.rb +0 -33
- data/spec/sorted/orms/mongoid_spec.rb +0 -32
- data/spec/sorted/parser_spec.rb +0 -117
- data/spec/sorted/toggler_spec.rb +0 -75
- data/spec/sorted/view_helpers/action_view_spec.rb +0 -63
@@ -1,9 +0,0 @@
|
|
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: '../'
|
@@ -1,109 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: /Users/rufuspost/Projects/ruby/gems/sorted
|
3
|
-
specs:
|
4
|
-
sorted (1.1.1)
|
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
|
-
ast (2.0.0)
|
37
|
-
astrolabe (1.3.0)
|
38
|
-
parser (>= 2.2.0.pre.3, < 3.0)
|
39
|
-
atomic (1.1.14)
|
40
|
-
atomic (1.1.14-java)
|
41
|
-
builder (3.1.4)
|
42
|
-
diff-lcs (1.2.4)
|
43
|
-
erubis (2.7.0)
|
44
|
-
i18n (0.6.5)
|
45
|
-
jdbc-sqlite3 (3.7.2.1)
|
46
|
-
minitest (4.7.5)
|
47
|
-
multi_json (1.8.2)
|
48
|
-
parser (2.2.0.pre.8)
|
49
|
-
ast (>= 1.1, < 3.0)
|
50
|
-
slop (~> 3.4, >= 3.4.5)
|
51
|
-
powerpack (0.0.9)
|
52
|
-
rack (1.5.2)
|
53
|
-
rack-test (0.6.2)
|
54
|
-
rack (>= 1.0)
|
55
|
-
railties (4.0.0)
|
56
|
-
actionpack (= 4.0.0)
|
57
|
-
activesupport (= 4.0.0)
|
58
|
-
rake (>= 0.8.7)
|
59
|
-
thor (>= 0.18.1, < 2.0)
|
60
|
-
rainbow (2.0.0)
|
61
|
-
rake (10.1.0)
|
62
|
-
rspec (2.14.1)
|
63
|
-
rspec-core (~> 2.14.0)
|
64
|
-
rspec-expectations (~> 2.14.0)
|
65
|
-
rspec-mocks (~> 2.14.0)
|
66
|
-
rspec-core (2.14.6)
|
67
|
-
rspec-expectations (2.14.3)
|
68
|
-
diff-lcs (>= 1.1.3, < 2.0)
|
69
|
-
rspec-mocks (2.14.4)
|
70
|
-
rspec-rails (2.14.0)
|
71
|
-
actionpack (>= 3.0)
|
72
|
-
activesupport (>= 3.0)
|
73
|
-
railties (>= 3.0)
|
74
|
-
rspec-core (~> 2.14.0)
|
75
|
-
rspec-expectations (~> 2.14.0)
|
76
|
-
rspec-mocks (~> 2.14.0)
|
77
|
-
rubocop (0.28.0)
|
78
|
-
astrolabe (~> 1.3)
|
79
|
-
parser (>= 2.2.0.pre.7, < 3.0)
|
80
|
-
powerpack (~> 0.0.6)
|
81
|
-
rainbow (>= 1.99.1, < 3.0)
|
82
|
-
ruby-progressbar (~> 1.4)
|
83
|
-
ruby-progressbar (1.7.0)
|
84
|
-
slop (3.6.0)
|
85
|
-
sqlite3 (1.3.8)
|
86
|
-
thor (0.18.1)
|
87
|
-
thread_safe (0.1.3)
|
88
|
-
atomic
|
89
|
-
thread_safe (0.1.3-java)
|
90
|
-
atomic
|
91
|
-
tzinfo (0.3.38)
|
92
|
-
|
93
|
-
PLATFORMS
|
94
|
-
java
|
95
|
-
ruby
|
96
|
-
|
97
|
-
DEPENDENCIES
|
98
|
-
actionpack (>= 3.0.0)
|
99
|
-
activerecord (>= 4.0.0)
|
100
|
-
activerecord-jdbcsqlite3-adapter
|
101
|
-
activesupport (>= 3.0.0)
|
102
|
-
bundler (>= 1.0.0)
|
103
|
-
railties (>= 4.0.0)
|
104
|
-
rake
|
105
|
-
rspec (>= 2.0.0)
|
106
|
-
rspec-rails (>= 2.0)
|
107
|
-
rubocop (>= 0.28)
|
108
|
-
sorted!
|
109
|
-
sqlite3 (>= 1.3.5)
|
data/gemfiles/mongoid_30.gemfile
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: ../
|
3
|
-
specs:
|
4
|
-
sorted (1.1.1)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
actionpack (4.1.4)
|
10
|
-
actionview (= 4.1.4)
|
11
|
-
activesupport (= 4.1.4)
|
12
|
-
rack (~> 1.5.2)
|
13
|
-
rack-test (~> 0.6.2)
|
14
|
-
actionview (4.1.4)
|
15
|
-
activesupport (= 4.1.4)
|
16
|
-
builder (~> 3.1)
|
17
|
-
erubis (~> 2.7.0)
|
18
|
-
activemodel (4.1.4)
|
19
|
-
activesupport (= 4.1.4)
|
20
|
-
builder (~> 3.1)
|
21
|
-
activesupport (4.1.4)
|
22
|
-
i18n (~> 0.6, >= 0.6.9)
|
23
|
-
json (~> 1.7, >= 1.7.7)
|
24
|
-
minitest (~> 5.1)
|
25
|
-
thread_safe (~> 0.1)
|
26
|
-
tzinfo (~> 1.1)
|
27
|
-
ast (2.0.0)
|
28
|
-
astrolabe (1.3.0)
|
29
|
-
parser (>= 2.2.0.pre.3, < 3.0)
|
30
|
-
bson (2.3.0)
|
31
|
-
bson (2.3.0-java)
|
32
|
-
builder (3.2.2)
|
33
|
-
connection_pool (2.0.0)
|
34
|
-
diff-lcs (1.2.5)
|
35
|
-
erubis (2.7.0)
|
36
|
-
i18n (0.6.9)
|
37
|
-
json (1.8.1)
|
38
|
-
json (1.8.1-java)
|
39
|
-
minitest (5.4.0)
|
40
|
-
mongoid (4.0.0)
|
41
|
-
activemodel (~> 4.0)
|
42
|
-
moped (~> 2.0.0)
|
43
|
-
origin (~> 2.1)
|
44
|
-
tzinfo (>= 0.3.37)
|
45
|
-
moped (2.0.0)
|
46
|
-
bson (~> 2.2)
|
47
|
-
connection_pool (~> 2.0)
|
48
|
-
optionable (~> 0.2.0)
|
49
|
-
optionable (0.2.0)
|
50
|
-
origin (2.1.1)
|
51
|
-
parser (2.2.0.pre.8)
|
52
|
-
ast (>= 1.1, < 3.0)
|
53
|
-
slop (~> 3.4, >= 3.4.5)
|
54
|
-
powerpack (0.0.9)
|
55
|
-
rack (1.5.2)
|
56
|
-
rack-test (0.6.2)
|
57
|
-
rack (>= 1.0)
|
58
|
-
railties (4.1.4)
|
59
|
-
actionpack (= 4.1.4)
|
60
|
-
activesupport (= 4.1.4)
|
61
|
-
rake (>= 0.8.7)
|
62
|
-
thor (>= 0.18.1, < 2.0)
|
63
|
-
rainbow (2.0.0)
|
64
|
-
rake (10.3.2)
|
65
|
-
rspec (3.0.0)
|
66
|
-
rspec-core (~> 3.0.0)
|
67
|
-
rspec-expectations (~> 3.0.0)
|
68
|
-
rspec-mocks (~> 3.0.0)
|
69
|
-
rspec-core (3.0.2)
|
70
|
-
rspec-support (~> 3.0.0)
|
71
|
-
rspec-expectations (3.0.2)
|
72
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
73
|
-
rspec-support (~> 3.0.0)
|
74
|
-
rspec-mocks (3.0.2)
|
75
|
-
rspec-support (~> 3.0.0)
|
76
|
-
rspec-rails (3.0.1)
|
77
|
-
actionpack (>= 3.0)
|
78
|
-
activesupport (>= 3.0)
|
79
|
-
railties (>= 3.0)
|
80
|
-
rspec-core (~> 3.0.0)
|
81
|
-
rspec-expectations (~> 3.0.0)
|
82
|
-
rspec-mocks (~> 3.0.0)
|
83
|
-
rspec-support (~> 3.0.0)
|
84
|
-
rspec-support (3.0.2)
|
85
|
-
rubocop (0.28.0)
|
86
|
-
astrolabe (~> 1.3)
|
87
|
-
parser (>= 2.2.0.pre.7, < 3.0)
|
88
|
-
powerpack (~> 0.0.6)
|
89
|
-
rainbow (>= 1.99.1, < 3.0)
|
90
|
-
ruby-progressbar (~> 1.4)
|
91
|
-
ruby-progressbar (1.7.0)
|
92
|
-
slop (3.6.0)
|
93
|
-
thor (0.19.1)
|
94
|
-
thread_safe (0.3.4)
|
95
|
-
thread_safe (0.3.4-java)
|
96
|
-
tzinfo (1.2.1)
|
97
|
-
thread_safe (~> 0.1)
|
98
|
-
|
99
|
-
PLATFORMS
|
100
|
-
java
|
101
|
-
ruby
|
102
|
-
|
103
|
-
DEPENDENCIES
|
104
|
-
actionpack (>= 3.0.0)
|
105
|
-
activesupport (>= 3.0.0)
|
106
|
-
bundler (>= 1.0.0)
|
107
|
-
mongoid (~> 4)
|
108
|
-
railties (>= 4)
|
109
|
-
rake
|
110
|
-
rspec (>= 2.0.0)
|
111
|
-
rspec-rails (>= 2.0)
|
112
|
-
rubocop (>= 0.28)
|
113
|
-
sorted!
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'sorted'
|
2
|
-
require 'active_support/concern'
|
3
|
-
|
4
|
-
module Sorted
|
5
|
-
module Orms
|
6
|
-
module ActiveRecord
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
|
9
|
-
included do
|
10
|
-
def self.sorted(sort, default_order = nil)
|
11
|
-
order sort_sql(sort, default_order)
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.resorted(sort, default_order = nil)
|
15
|
-
reorder sort_sql(sort, default_order)
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def self.sort_sql(sort, default_order)
|
21
|
-
sorter(sort, default_order).to_sql(quoter)
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.sorter(sort, default_order)
|
25
|
-
::Sorted::Parser.new(sort, default_order)
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.quoter
|
29
|
-
->(frag) { connection.quote_column_name(frag) }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
data/lib/sorted/orms/mongoid.rb
DELETED
@@ -1,18 +0,0 @@
|
|
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
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'sorted/toggler'
|
2
|
-
|
3
|
-
module Sorted
|
4
|
-
##
|
5
|
-
# Takes a sort query string and an SQL order string and parses the
|
6
|
-
#
|
7
|
-
# values to produce key value pairs.
|
8
|
-
#
|
9
|
-
# Example:
|
10
|
-
# Sorted::Parser.new('phone_desc', 'name ASC').to_s #=> "phone_desc!name_asc"
|
11
|
-
#
|
12
|
-
# TODO A more helpfull name than `Parser` because it only deals with URI and
|
13
|
-
# SQL. Shoud be refactored before 2.x
|
14
|
-
|
15
|
-
Parser = Struct.new(:sort, :order) do
|
16
|
-
def uri
|
17
|
-
URIQuery.parse(sort)
|
18
|
-
end
|
19
|
-
|
20
|
-
def sql
|
21
|
-
SQLQuery.parse(order)
|
22
|
-
end
|
23
|
-
|
24
|
-
def sorts
|
25
|
-
uri.to_a
|
26
|
-
end
|
27
|
-
|
28
|
-
def orders
|
29
|
-
sql.to_a
|
30
|
-
end
|
31
|
-
|
32
|
-
def to_hash
|
33
|
-
set.to_hash
|
34
|
-
end
|
35
|
-
|
36
|
-
def to_sql(quote_proc = ->(f) { f })
|
37
|
-
SQLQuery.encode(set, quote_proc)
|
38
|
-
end
|
39
|
-
|
40
|
-
def to_s
|
41
|
-
URIQuery.encode(set)
|
42
|
-
end
|
43
|
-
|
44
|
-
def to_a
|
45
|
-
set.to_a
|
46
|
-
end
|
47
|
-
|
48
|
-
def toggle
|
49
|
-
@set = Toggler.new(sorts, orders).toggle
|
50
|
-
self
|
51
|
-
end
|
52
|
-
|
53
|
-
def reset
|
54
|
-
@set = default
|
55
|
-
self
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
def set
|
61
|
-
@set ||= default
|
62
|
-
end
|
63
|
-
|
64
|
-
def default
|
65
|
-
uri + (sql - uri)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
data/lib/sorted/railtie.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'sorted'
|
2
|
-
|
3
|
-
module Sorted
|
4
|
-
class Railtie < Rails::Railtie
|
5
|
-
initializer 'sorted.configure' do |_app|
|
6
|
-
if defined? ::ActiveRecord
|
7
|
-
ActiveSupport.on_load :active_record do
|
8
|
-
require 'sorted/orms/active_record'
|
9
|
-
include Sorted::Orms::ActiveRecord
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
if defined? ::Mongoid
|
14
|
-
require 'sorted/orms/mongoid'
|
15
|
-
::Mongoid::Document.send :include, Sorted::Orms::Mongoid
|
16
|
-
end
|
17
|
-
|
18
|
-
ActiveSupport.on_load :action_view do
|
19
|
-
require 'sorted/view_helpers/action_view'
|
20
|
-
include Sorted::ViewHelpers::ActionView
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/lib/sorted/toggler.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Sorted
|
2
|
-
##
|
3
|
-
# Takes a parsed arrays of sorts and orders, it then will reorder the pairs
|
4
|
-
# and flip the ascendance of the first sort pair.
|
5
|
-
#
|
6
|
-
# Example:
|
7
|
-
# sorts = [['name', 'asc'], ['phone', 'desc']]
|
8
|
-
# orders = [['name', 'asc']]
|
9
|
-
# Sorted::Toggler.new(sorts, orders).to_a
|
10
|
-
#
|
11
|
-
# TODO Remove this in 2.x, it's only here for backwards compatibility.
|
12
|
-
|
13
|
-
class Toggler
|
14
|
-
def initialize(sorts, orders)
|
15
|
-
@sorts = Set.new(sorts)
|
16
|
-
@orders = Set.new(orders)
|
17
|
-
end
|
18
|
-
|
19
|
-
def toggle
|
20
|
-
@orders.direction_intersect(@sorts)
|
21
|
-
end
|
22
|
-
|
23
|
-
def to_a
|
24
|
-
toggle.to_a
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,86 +0,0 @@
|
|
1
|
-
require 'action_view'
|
2
|
-
require 'sorted'
|
3
|
-
|
4
|
-
module Sorted
|
5
|
-
module ViewHelpers
|
6
|
-
module ActionView
|
7
|
-
class SortedViewHelper
|
8
|
-
attr_reader :params
|
9
|
-
|
10
|
-
def initialize(order, params = {})
|
11
|
-
sort = params.delete :sort
|
12
|
-
@params = params
|
13
|
-
@parser = ::Sorted::Parser.new(sort, order).toggle
|
14
|
-
@params[:sort] = @parser.to_s
|
15
|
-
end
|
16
|
-
|
17
|
-
def css
|
18
|
-
if @parser.sorts.flatten.include? @parser.orders[0][0]
|
19
|
-
"sorted #{@parser.sorts.assoc(@parser.orders[0][0]).last}"
|
20
|
-
else
|
21
|
-
'sorted'
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Creates a link tag of the given +name+ and +attribute+ creating
|
27
|
-
# a url using a set of +options+.
|
28
|
-
#
|
29
|
-
# ==== Examples
|
30
|
-
#
|
31
|
-
# Basic usage
|
32
|
-
#
|
33
|
-
# link_to_sorted "Email", :email
|
34
|
-
# # => <a href="/profiles?sort=email_asc" class="desc">Email</a>
|
35
|
-
#
|
36
|
-
# Or use a block
|
37
|
-
#
|
38
|
-
# link_to_sorted :email do
|
39
|
-
# <strong>Sort by email</strong> -- <span></span>
|
40
|
-
# end
|
41
|
-
# # => <a href="/profiles?sort=email_asc" class="desc"><strong>Sort by email</strong> -- <span></span></a>
|
42
|
-
#
|
43
|
-
def link_to_sorted(*args, &block)
|
44
|
-
if block_given?
|
45
|
-
order = args[0]
|
46
|
-
options = args[1] || {}
|
47
|
-
html_options = args[2] || {}
|
48
|
-
else
|
49
|
-
block = proc { args[0].to_s }
|
50
|
-
order = args[1]
|
51
|
-
options = args[2] || {}
|
52
|
-
html_options = args[3] || {}
|
53
|
-
end
|
54
|
-
|
55
|
-
sorter = SortedViewHelper.new(order, ((request.get? && !params.nil?) ? params.dup : {}))
|
56
|
-
options[:class] = [options[:class], sorter.css].join(' ').strip
|
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'
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|