sortable-by 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +60 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/lib/sortable-by.rb +1 -0
- data/lib/sortable_by.rb +42 -0
- data/sortable-by.gemspec +24 -0
- data/spec/sortable_by_spec.rb +22 -0
- data/spec/spec_helper.rb +29 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9a8f7b12ede76157e3c47f2aa2e3448830ec5040
|
4
|
+
data.tar.gz: 32a5f7e54e4be50111e8d9fe40b5c469d37a14b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f9fe1dec1e74a71f2d73c5b488047a56f5eba5615dcdcf72648b5acd05655467022da4fde1f8dd6aff62ea191ccaeaa3bcbbbbba6b46f7b5ce11926ea486486
|
7
|
+
data.tar.gz: 1a0a02ae09216da95b603fddecbf07bb5e3578961a9f81004e09f4aa9e5976732f1919dd0fc8797b119efdd8a787b5df455bd6be705aea7b3c42a4072279df29
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sortable-by (0.8.0)
|
5
|
+
activerecord
|
6
|
+
activesupport
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (4.2.3)
|
12
|
+
activesupport (= 4.2.3)
|
13
|
+
builder (~> 3.1)
|
14
|
+
activerecord (4.2.3)
|
15
|
+
activemodel (= 4.2.3)
|
16
|
+
activesupport (= 4.2.3)
|
17
|
+
arel (~> 6.0)
|
18
|
+
activesupport (4.2.3)
|
19
|
+
i18n (~> 0.7)
|
20
|
+
json (~> 1.7, >= 1.7.7)
|
21
|
+
minitest (~> 5.1)
|
22
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
23
|
+
tzinfo (~> 1.1)
|
24
|
+
arel (6.0.2)
|
25
|
+
builder (3.2.2)
|
26
|
+
diff-lcs (1.2.5)
|
27
|
+
i18n (0.7.0)
|
28
|
+
json (1.8.3)
|
29
|
+
minitest (5.7.0)
|
30
|
+
rake (10.4.2)
|
31
|
+
rspec (3.3.0)
|
32
|
+
rspec-core (~> 3.3.0)
|
33
|
+
rspec-expectations (~> 3.3.0)
|
34
|
+
rspec-mocks (~> 3.3.0)
|
35
|
+
rspec-core (3.3.2)
|
36
|
+
rspec-support (~> 3.3.0)
|
37
|
+
rspec-expectations (3.3.1)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.3.0)
|
40
|
+
rspec-mocks (3.3.2)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.3.0)
|
43
|
+
rspec-support (3.3.0)
|
44
|
+
sqlite3 (1.3.10)
|
45
|
+
thread_safe (0.3.5)
|
46
|
+
tzinfo (1.2.2)
|
47
|
+
thread_safe (~> 0.1)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
bundler
|
54
|
+
rake
|
55
|
+
rspec
|
56
|
+
sortable-by!
|
57
|
+
sqlite3
|
58
|
+
|
59
|
+
BUNDLED WITH
|
60
|
+
1.10.6
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Sortable By
|
2
|
+
|
3
|
+
ActiveRecord plugin to parse the sort order from a query parameter, match against a white-list and generate a scope. Useful for [JSON-API][jsonapi] compatibility.
|
4
|
+
|
5
|
+
[jsonapi]: http://jsonapi.org/format/#fetching-sorting
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add `gem 'sortable-by'` to your Gemfile.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Simple use cases:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class Foo < ActiveRecord::Base
|
17
|
+
sortable_by :title, :updated_at, default: { updated_at: :desc }
|
18
|
+
end
|
19
|
+
|
20
|
+
Foo.sort_by "-updated_at,title" # => ORDER BY updated_at DESC, title ASC
|
21
|
+
Foo.sort_by "bad,title" # => ORDER BY title ASC
|
22
|
+
Foo.sort_by nil # => ORDER BY updated_at DESC
|
23
|
+
```
|
24
|
+
|
25
|
+
## LICENCE
|
26
|
+
|
27
|
+
```
|
28
|
+
Copyright (c) 2015 Black Square Media
|
29
|
+
|
30
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
31
|
+
a copy of this software and associated documentation files (the
|
32
|
+
"Software"), to deal in the Software without restriction, including
|
33
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
34
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
35
|
+
permit persons to whom the Software is furnished to do so, subject to
|
36
|
+
the following conditions:
|
37
|
+
|
38
|
+
The above copyright notice and this permission notice shall be
|
39
|
+
included in all copies or substantial portions of the Software.
|
40
|
+
|
41
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
42
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
43
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
44
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
45
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
46
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
47
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
48
|
+
```
|
data/Rakefile
ADDED
data/lib/sortable-by.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'sortable_by'
|
data/lib/sortable_by.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module SortableByHelper
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
class_attribute :_sortable_by_scope_options, instance_accessor: false
|
11
|
+
sortable_by
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
|
16
|
+
def sortable_by(*whitelist)
|
17
|
+
opts = whitelist.extract_options!
|
18
|
+
opts[:default] ||= {id: :asc}
|
19
|
+
self._sortable_by_scope_options = opts.merge(whitelist: whitelist.map(&:to_s).to_set)
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param [String] expr the sort expr
|
23
|
+
# @return [ActiveRecord::Relation] the scoped relation
|
24
|
+
def sort_by(expr)
|
25
|
+
options = {}
|
26
|
+
expr.to_s.split(',').each do |name|
|
27
|
+
name.strip!
|
28
|
+
rank = :asc
|
29
|
+
rank, name = :desc, name[1..-1] if name[0] == '-'
|
30
|
+
options[name.to_sym] = rank if self._sortable_by_scope_options[:whitelist].include?(name)
|
31
|
+
end
|
32
|
+
options = self._sortable_by_scope_options[:default] if options.empty?
|
33
|
+
order(options)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Base
|
40
|
+
include SortableByHelper
|
41
|
+
end
|
42
|
+
end
|
data/sortable-by.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = 'sortable-by'
|
4
|
+
s.version = '0.8.0'
|
5
|
+
s.authors = ['Dimitrij Denissenko']
|
6
|
+
s.email = ['dimitrij@blacksquaremedia.com']
|
7
|
+
s.summary = 'Generate white-listed sort scopes from URL parameter values'
|
8
|
+
s.description = 'ActiveRecord plugin'
|
9
|
+
s.homepage = 'https://github.com/bsm/sortable-by'
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^spec/}) }
|
13
|
+
s.test_files = `git ls-files -z -- spec/*`.split("\x0")
|
14
|
+
s.require_paths = ['lib']
|
15
|
+
s.required_ruby_version = '>= 1.9.3'
|
16
|
+
|
17
|
+
s.add_dependency 'activesupport'
|
18
|
+
s.add_dependency 'activerecord'
|
19
|
+
|
20
|
+
s.add_development_dependency 'bundler'
|
21
|
+
s.add_development_dependency 'rake'
|
22
|
+
s.add_development_dependency 'rspec'
|
23
|
+
s.add_development_dependency 'sqlite3'
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::SortableByHelper do
|
4
|
+
|
5
|
+
it 'should have config' do
|
6
|
+
expect(Foo._sortable_by_scope_options).to eq(default: {title: :asc}, whitelist: Set.new(["title", "age"]))
|
7
|
+
expect(Bar._sortable_by_scope_options).to eq(default: {id: :asc}, whitelist: Set.new)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should generate scopes' do
|
11
|
+
expect(Foo.sort_by("title").pluck(:title)).to eq(["A", "B", "B", "C"])
|
12
|
+
expect(Foo.sort_by("-title").pluck(:title)).to eq(["C", "B", "B", "A"])
|
13
|
+
expect(Foo.sort_by("age,title").pluck(:title)).to eq(["B", "A", "B", "C"])
|
14
|
+
expect(Foo.sort_by("invalid , -age").pluck(:title)).to eq(["C", "B", "A", "B"])
|
15
|
+
expect(Foo.sort_by("").pluck(:title)).to eq(["A", "B", "B", "C"])
|
16
|
+
expect(Foo.sort_by(nil).pluck(:title)).to eq(["A", "B", "B", "C"])
|
17
|
+
|
18
|
+
expect(Bar.sort_by("").pluck(:title)).to eq(["Y", "X"])
|
19
|
+
expect(Bar.sort_by("title").pluck(:title)).to eq(["Y", "X"])
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
ENV['RACK_ENV'] ||= "test"
|
2
|
+
require 'sortable-by'
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
ActiveRecord::Base.configurations["test"] = { 'adapter' => 'sqlite3', 'database' => ":memory:" }
|
6
|
+
ActiveRecord::Base.establish_connection :test
|
7
|
+
ActiveRecord::Base.connection.create_table :foos do |t|
|
8
|
+
t.string :title
|
9
|
+
t.integer :age
|
10
|
+
end
|
11
|
+
|
12
|
+
ActiveRecord::Base.connection.create_table :bars do |t|
|
13
|
+
t.string :title
|
14
|
+
end
|
15
|
+
|
16
|
+
class Foo < ActiveRecord::Base
|
17
|
+
sortable_by :title, :age, default: {title: :asc}
|
18
|
+
end
|
19
|
+
|
20
|
+
class Bar < ActiveRecord::Base
|
21
|
+
end
|
22
|
+
|
23
|
+
Foo.create! title: 'A', age: 25
|
24
|
+
Foo.create! title: 'B', age: 24
|
25
|
+
Foo.create! title: 'B', age: 26
|
26
|
+
Foo.create! title: 'C', age: 27
|
27
|
+
|
28
|
+
Bar.create! title: 'Y'
|
29
|
+
Bar.create! title: 'X'
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sortable-by
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dimitrij Denissenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: ActiveRecord plugin
|
98
|
+
email:
|
99
|
+
- dimitrij@blacksquaremedia.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
|
+
- Gemfile
|
107
|
+
- Gemfile.lock
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/sortable-by.rb
|
111
|
+
- lib/sortable_by.rb
|
112
|
+
- sortable-by.gemspec
|
113
|
+
- spec/sortable_by_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
homepage: https://github.com/bsm/sortable-by
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 1.9.3
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.4.8
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Generate white-listed sort scopes from URL parameter values
|
139
|
+
test_files:
|
140
|
+
- spec/sortable_by_spec.rb
|
141
|
+
- spec/spec_helper.rb
|