sorty_sorter 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +2 -0
- data/lib/sorty_sorter/declaration.rb +22 -0
- data/lib/sorty_sorter/errors.rb +26 -0
- data/lib/sorty_sorter/params.rb +55 -0
- data/lib/sorty_sorter/relation.rb +22 -0
- data/lib/sorty_sorter/sort.rb +11 -0
- data/lib/sorty_sorter/version.rb +3 -0
- data/lib/sorty_sorter.rb +11 -0
- data/sorty_sorter.gemspec +27 -0
- data/spec/params_spec.rb +24 -0
- data/spec/sort_spec.rb +70 -0
- data/spec/spec_helper.rb +116 -0
- data/spec/support/active_record.rb +26 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aade2910e523945eca5d57adf919cf95dd9a9bfd
|
4
|
+
data.tar.gz: 75a902aa3ccc4b99278d224ec968cf8442b2bd89
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2251200c2272bfe44cba9e643f8dafe6065640868196c8390bff331f16d77e33b007bafc072d9b438d1fc4ee1fc6e1ba445670ece2f29dc7cb7d7691b20966fe
|
7
|
+
data.tar.gz: 87da5e17107b9b5c7f3d0008170c4715e510532cd0bbd598fd9ed4792f184c69bfce83d86750a7e8fe6633d1cf283ec24d712b94d840a0586c5b344e33140a45
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Kat Padilla
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Sorty
|
2
|
+
|
3
|
+
This simple gem sorts collection in a Rails controller/API controller given a set of parameters based on the declared columns in the model. An ActiveRecord::Relation method `sorty_sort` is added for convenience.
|
4
|
+
|
5
|
+
When defining valid columns that are "sortable", you can choose to mask the attributes so the DB columns will not be announced to the world. In other words, you can choose to name your exposed attribute differently than your DB column name. See Usage #1 as reference.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'sorty', :git => 'git://github.com/katpadi/sorty.git'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install sorty-sorter
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
1. Add declaration of sorting columns to model:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
sort_with update_date: { updated_at: :desc },
|
28
|
+
name: { name: :asc }
|
29
|
+
```
|
30
|
+
|
31
|
+
In the example above, `update_date` is the exposed attribute and it represents the `updated_at` column in DB.
|
32
|
+
The declaration will serve as the "valid" attributes that may be used for sorting.
|
33
|
+
|
34
|
+
2. Call sort method:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
@collection.sorty_sort('name', 'asc')
|
38
|
+
```
|
39
|
+
|
40
|
+
There is also a bang method `sorty_sort!` that will raise an exception if you're doing anything wrong.
|
41
|
+
|
42
|
+
## Example
|
43
|
+
|
44
|
+
An example when used in API:
|
45
|
+
|
46
|
+
```sh
|
47
|
+
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://katpadi/drones?sort=update_date&dir=desc
|
48
|
+
```
|
49
|
+
|
50
|
+
Say you want to sort the collection based on the column and direction passed in your API, you can use the gem's ActiveRecord::Relation method `sorty_sort` to sort the collection:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
Drone.available.sorty_sort(params[:sort], params[:dir])
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it ( https://github.com/katpadi/sorty/fork )
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module SortySorter
|
2
|
+
module Declaration
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
attr_reader :sort_columns
|
11
|
+
def sort_with(columns)
|
12
|
+
@sort_columns = columns
|
13
|
+
end
|
14
|
+
|
15
|
+
def sorty?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::Base.send(:include, SortySorter::Declaration)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SortySorter
|
2
|
+
module Errors
|
3
|
+
class SortyError < StandardError; end
|
4
|
+
|
5
|
+
class InvalidDirectionDefined < SortyError
|
6
|
+
end
|
7
|
+
|
8
|
+
class InvalidColumnDefined < SortyError
|
9
|
+
def initialize(col)
|
10
|
+
super "Invalid value for column #{col}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class InvalidExposedAttribute < SortyError
|
15
|
+
def initialize(col)
|
16
|
+
super "No attribute #{col} defined in model"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class ColumnDoesNotExist < SortyError
|
21
|
+
def initialize(col)
|
22
|
+
super "Column #{col} does not exist in the database."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module SortySorter
|
2
|
+
class Params
|
3
|
+
attr_reader :column, :direction, :defined_cols, :collection
|
4
|
+
|
5
|
+
DEFAULT_SORT_KEY = :updated_at
|
6
|
+
DEFAULT_SORT_DIR = :desc
|
7
|
+
|
8
|
+
# TODO: Maybe set some default sort columns
|
9
|
+
|
10
|
+
def initialize(col, dir, collection)
|
11
|
+
@column = :"#{col}"
|
12
|
+
@direction = :"#{dir}"
|
13
|
+
@collection = collection
|
14
|
+
@defined_cols = collection.sort_columns
|
15
|
+
end
|
16
|
+
|
17
|
+
def options
|
18
|
+
Hash[sort_col, sort_dir]
|
19
|
+
end
|
20
|
+
|
21
|
+
def options!
|
22
|
+
Hash[sort_col!, sort_dir!]
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def sort_col
|
28
|
+
sort_col!
|
29
|
+
rescue
|
30
|
+
DEFAULT_SORT_KEY
|
31
|
+
end
|
32
|
+
|
33
|
+
def sort_dir
|
34
|
+
sort_dir!
|
35
|
+
rescue
|
36
|
+
DEFAULT_SORT_DIR
|
37
|
+
end
|
38
|
+
|
39
|
+
def sort_col!
|
40
|
+
fail SortySorter::Errors::InvalidColumnDefined.new(column) if column.blank?
|
41
|
+
col = defined_cols.fetch(:"#{column}").keys.first
|
42
|
+
fail SortySorter::Errors::ColumnDoesNotExist.new(col.to_s) unless collection.column_names.include?(col.to_s)
|
43
|
+
col
|
44
|
+
rescue KeyError
|
45
|
+
fail SortySorter::Errors::InvalidExposedAttribute.new(column)
|
46
|
+
end
|
47
|
+
|
48
|
+
def sort_dir!
|
49
|
+
return direction if %i(desc asc).include?(direction)
|
50
|
+
defined_cols.fetch(:"#{column}").values.first
|
51
|
+
rescue KeyError
|
52
|
+
fail SortySorter::Errors::InvalidDirectionDefined
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Extensions
|
2
|
+
# ActiveRecord extensions
|
3
|
+
module ActiveRecord
|
4
|
+
# Extensions for ActiveRecord::Relation
|
5
|
+
module Relation
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
# Introduce a new AR Relation method :-)
|
9
|
+
def sorty_sort(column = nil, direction = nil)
|
10
|
+
::SortySorter::Sort.apply(self, column, direction)
|
11
|
+
end
|
12
|
+
|
13
|
+
def sorty_sort!(column = nil, direction = nil)
|
14
|
+
::SortySorter::Sort.apply!(self, column, direction)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveSupport.on_load(:active_record) do
|
21
|
+
ActiveRecord::Relation.send(:include, Extensions::ActiveRecord::Relation)
|
22
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module SortySorter
|
2
|
+
class Sort
|
3
|
+
def self.apply(collection, column = nil, direction = nil)
|
4
|
+
collection.order(Params.new(column, direction, collection).options)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.apply!(collection, column = nil, direction = nil)
|
8
|
+
collection.order(Params.new(column, direction, collection).options!)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/sorty_sorter.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sorty_sorter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sorty_sorter"
|
8
|
+
spec.version = SortySorter::VERSION
|
9
|
+
spec.authors = ["Kat Padilla"]
|
10
|
+
spec.email = ["hello@katpadi.ph"]
|
11
|
+
spec.summary = "This gem sorts ActiveRecord::Relation based on dynamic column (i.e. name, updated_at, etc.) and direction (asc, desc) parameters against columns defined in model."
|
12
|
+
spec.description = "This gem adds an ActiveRecord::Relation method that sorts based on dynamic column (i.e. name, updated_at, etc.) and direction (asc, desc) parameters and validated against definition in model."
|
13
|
+
spec.homepage = "https://github.com/katpadi/sorty-sorter"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency 'mysql2', '~> 0.3'
|
25
|
+
|
26
|
+
spec.add_dependency "activerecord", "~> 4.1"
|
27
|
+
end
|
data/spec/params_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe SortySorter::Params do
|
3
|
+
|
4
|
+
before(:each) do
|
5
|
+
clean_database
|
6
|
+
10.times do
|
7
|
+
Drone.create!(points: rand(1..100), name: (0...8).map { (65 + rand(26)).chr }.join)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "Bang method" do
|
12
|
+
it "raises error when exposed attribute is invalid" do
|
13
|
+
expect{Drone.all.sorty_sort!('amfufu', 'asc')}.to raise_error(SortySorter::Errors::InvalidExposedAttribute)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises error when no such column" do
|
17
|
+
expect{Drone.all.sorty_sort!('no_such_col', 'desc')}.to raise_error(SortySorter::Errors::ColumnDoesNotExist)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "raises error when invalid definition" do
|
21
|
+
expect{Drone.all.sorty_sort!('', 'desc')}.to raise_error(SortySorter::Errors::InvalidColumnDefined)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/sort_spec.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'support/active_record'
|
4
|
+
|
5
|
+
describe SortySorter::Sort do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
clean_database
|
9
|
+
end
|
10
|
+
|
11
|
+
context "Sorts integers correctly" do
|
12
|
+
it "DESC" do
|
13
|
+
pro = Drone.create!(points: 9999)
|
14
|
+
noob = Drone.create!(points: 1)
|
15
|
+
mediocre = Drone.create!(points: 500)
|
16
|
+
results = Drone.all.sorty_sort!('pointz', 'desc')
|
17
|
+
expect(results.index(noob)).to be > results.index(mediocre)
|
18
|
+
expect(results.index(mediocre)).to be > results.index(pro)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "ASC" do
|
22
|
+
noob = Drone.create!(points: 1)
|
23
|
+
mediocre = Drone.create!(points: 500)
|
24
|
+
pro = Drone.create!(points: 9999)
|
25
|
+
results = Drone.all.sorty_sort('points', 'asc')
|
26
|
+
expect(results.index(noob)).to be < results.index(mediocre)
|
27
|
+
expect(results.index(mediocre)).to be < results.index(pro)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "Sorts string correctly" do
|
32
|
+
it "DESC" do
|
33
|
+
kat = Drone.create!(points: 9999, name: 'Kat')
|
34
|
+
zayn = Drone.create!(points: 1, name: 'Zayn Malik')
|
35
|
+
andi = Drone.create!(points: 500, name: 'Andi')
|
36
|
+
results = Drone.all.sorty_sort!('name', 'desc')
|
37
|
+
expect(results.index(andi)).to be > results.index(kat)
|
38
|
+
expect(results.index(kat)).to be > results.index(zayn)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "ASC" do
|
42
|
+
kat = Drone.create!(points: 9999, name: 'Kat')
|
43
|
+
zayn = Drone.create!(points: 1, name: 'Zayn Malik')
|
44
|
+
andi = Drone.create!(points: 500, name: 'Andi')
|
45
|
+
results = Drone.all.sorty_sort!('name', 'asc')
|
46
|
+
expect(results.index(zayn)).to be > results.index(kat)
|
47
|
+
expect(results.index(kat)).to be > results.index(andi)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "Sorts dates correctly" do
|
52
|
+
it "DESC" do
|
53
|
+
kasalukuyan = Drone.create!(points: 1, name: 'Zayn Malik', updated_at: DateTime.now)
|
54
|
+
kahapon = Drone.create!(points: 9999, name: 'Kat', updated_at: 1.day.ago)
|
55
|
+
last_year = Drone.create!(points: 500, name: 'Andi', updated_at: 1.year.ago)
|
56
|
+
results = Drone.all.sorty_sort!('date', 'desc')
|
57
|
+
expect(results.index(last_year)).to be > results.index(kahapon)
|
58
|
+
expect(results.index(kahapon)).to be > results.index(kasalukuyan)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "ASC" do
|
62
|
+
kasalukuyan = Drone.create!(points: 1, name: 'Zayn Malik', updated_at: DateTime.now)
|
63
|
+
kahapon = Drone.create!(points: 9999, name: 'Kat', updated_at: 1.day.ago)
|
64
|
+
last_year = Drone.create!(points: 500, name: 'Andi', updated_at: 1.year.ago)
|
65
|
+
results = Drone.all.sorty_sort!('date', 'asc')
|
66
|
+
expect(results.index(kasalukuyan)).to be > results.index(kahapon)
|
67
|
+
expect(results.index(kahapon)).to be > results.index(last_year)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'support/active_record'
|
2
|
+
require 'sorty_sorter'
|
3
|
+
|
4
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
5
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
6
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
7
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
8
|
+
# files.
|
9
|
+
#
|
10
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
11
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
12
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
13
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
14
|
+
# a separate helper file that requires the additional dependencies and performs
|
15
|
+
# the additional setup, and require it from the spec files that actually need
|
16
|
+
# it.
|
17
|
+
#
|
18
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
19
|
+
# users commonly want.
|
20
|
+
#
|
21
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
22
|
+
RSpec.configure do |config|
|
23
|
+
|
24
|
+
config.before(:suite) do
|
25
|
+
ActiveRecord::Migration.create_table(:drones, force: true) do |t|
|
26
|
+
t.string :name
|
27
|
+
t.integer :points
|
28
|
+
t.string :title
|
29
|
+
t.timestamps
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
config.after(:suite) do
|
34
|
+
models = [Drone]
|
35
|
+
models.each do |model|
|
36
|
+
ActiveRecord::Base.connection.execute "DROP TABLE #{model.table_name}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# rspec-expectations config goes here. You can use an alternate
|
41
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
42
|
+
# assertions if you prefer.
|
43
|
+
config.expect_with :rspec do |expectations|
|
44
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
45
|
+
# and `failure_message` of custom matchers include text for helper methods
|
46
|
+
# defined using `chain`, e.g.:
|
47
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
48
|
+
# # => "be bigger than 2 and smaller than 4"
|
49
|
+
# ...rather than:
|
50
|
+
# # => "be bigger than 2"
|
51
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
52
|
+
end
|
53
|
+
|
54
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
55
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
56
|
+
config.mock_with :rspec do |mocks|
|
57
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
58
|
+
# a real object. This is generally recommended, and will default to
|
59
|
+
# `true` in RSpec 4.
|
60
|
+
mocks.verify_partial_doubles = true
|
61
|
+
end
|
62
|
+
|
63
|
+
# The settings below are suggested to provide a good initial experience
|
64
|
+
# with RSpec, but feel free to customize to your heart's content.
|
65
|
+
=begin
|
66
|
+
# These two settings work together to allow you to limit a spec run
|
67
|
+
# to individual examples or groups you care about by tagging them with
|
68
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
69
|
+
# get run.
|
70
|
+
config.filter_run :focus
|
71
|
+
config.run_all_when_everything_filtered = true
|
72
|
+
|
73
|
+
# Allows RSpec to persist some state between runs in order to support
|
74
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
75
|
+
# you configure your source control system to ignore this file.
|
76
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
77
|
+
|
78
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
79
|
+
# recommended. For more details, see:
|
80
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
81
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
82
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
83
|
+
config.disable_monkey_patching!
|
84
|
+
|
85
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
86
|
+
# be too noisy due to issues in dependencies.
|
87
|
+
config.warnings = true
|
88
|
+
|
89
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
90
|
+
# file, and it's useful to allow more verbose output when running an
|
91
|
+
# individual spec file.
|
92
|
+
if config.files_to_run.one?
|
93
|
+
# Use the documentation formatter for detailed output,
|
94
|
+
# unless a formatter has already been configured
|
95
|
+
# (e.g. via a command-line flag).
|
96
|
+
config.default_formatter = 'doc'
|
97
|
+
end
|
98
|
+
|
99
|
+
# Print the 10 slowest examples and example groups at the
|
100
|
+
# end of the spec run, to help surface which specs are running
|
101
|
+
# particularly slow.
|
102
|
+
config.profile_examples = 10
|
103
|
+
|
104
|
+
# Run specs in random order to surface order dependencies. If you find an
|
105
|
+
# order dependency and want to debug it, you can fix the order by providing
|
106
|
+
# the seed, which is printed after each run.
|
107
|
+
# --seed 1234
|
108
|
+
config.order = :random
|
109
|
+
|
110
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
111
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
112
|
+
# test failures related to randomization by passing the same `--seed` value
|
113
|
+
# as the one that triggered the failure.
|
114
|
+
Kernel.srand config.seed
|
115
|
+
=end
|
116
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'sorty_sorter'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection adapter: "mysql2", database: "test"
|
5
|
+
|
6
|
+
# module ActiveModel::Validations
|
7
|
+
# def errors_on(attribute)
|
8
|
+
# self.valid?
|
9
|
+
# [self.errors[attribute]].flatten.compact
|
10
|
+
# end
|
11
|
+
# alias :error_on :errors_on
|
12
|
+
# end
|
13
|
+
|
14
|
+
class Drone < ActiveRecord::Base
|
15
|
+
sort_with pointz: { points: :asc },
|
16
|
+
name: { name: :asc },
|
17
|
+
no_such_col: { waley: :asc },
|
18
|
+
date: { updated_at: :desc }
|
19
|
+
end
|
20
|
+
|
21
|
+
def clean_database
|
22
|
+
models = [Drone]
|
23
|
+
models.each do |model|
|
24
|
+
ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{model.table_name}"
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sorty_sorter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kat Padilla
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mysql2
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.1'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.1'
|
83
|
+
description: This gem adds an ActiveRecord::Relation method that sorts based on dynamic
|
84
|
+
column (i.e. name, updated_at, etc.) and direction (asc, desc) parameters and validated
|
85
|
+
against definition in model.
|
86
|
+
email:
|
87
|
+
- hello@katpadi.ph
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- lib/sorty_sorter.rb
|
99
|
+
- lib/sorty_sorter/declaration.rb
|
100
|
+
- lib/sorty_sorter/errors.rb
|
101
|
+
- lib/sorty_sorter/params.rb
|
102
|
+
- lib/sorty_sorter/relation.rb
|
103
|
+
- lib/sorty_sorter/sort.rb
|
104
|
+
- lib/sorty_sorter/version.rb
|
105
|
+
- sorty_sorter.gemspec
|
106
|
+
- spec/params_spec.rb
|
107
|
+
- spec/sort_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
- spec/support/active_record.rb
|
110
|
+
homepage: https://github.com/katpadi/sorty-sorter
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.2.2
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: This gem sorts ActiveRecord::Relation based on dynamic column (i.e. name,
|
134
|
+
updated_at, etc.) and direction (asc, desc) parameters against columns defined in
|
135
|
+
model.
|
136
|
+
test_files:
|
137
|
+
- spec/params_spec.rb
|
138
|
+
- spec/sort_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/support/active_record.rb
|