sorty_sorter 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b1b42459784bcc7a3294f7341904f513cada412
4
- data.tar.gz: cc92de465496b817bed266aa236fd1b825b9283f
3
+ metadata.gz: 61c6078ac8fca37df8b1d767178f82fc8d19fd6d
4
+ data.tar.gz: 936f24fd55994a7a50e646b042197c6976fe03ae
5
5
  SHA512:
6
- metadata.gz: 7439a3e1c9239b0627d898eeea02e241694abd8d11060655fec8d7ab02d013b6a6f8524ead741fbfc3aa3795601a1b040c274d2babd9a345a55515d5ca8daf26
7
- data.tar.gz: 65ab4c73d5e445a26d200e06aa365409a0e6462322f1e68eac8258c7db0c13edf957b677834fc676825ae4509e6cff00b324581ba6de3443131e3a58293c947b
6
+ metadata.gz: 8c4deaa064946f5b7fa6894f0c820b62ec4c0cd8537173bb223963ab041ab8fd9b59d38a59ea42967f24b25557ce48e22156f46549aa92070db0813c8a0339b6
7
+ data.tar.gz: 237ad7f56cb9e317cb093338e2e4ee0bd6079875efd0b2a80b4c5655ad7bdd37272e891bd0a06d67ad7ae38f7b5e882ec78042e04c10bd3336d5efcfb2beb577
data/README.md CHANGED
@@ -4,10 +4,7 @@
4
4
  [![Code Climate](https://codeclimate.com/github/katpadi/sorty_sorter/badges/gpa.svg)](https://codeclimate.com/github/katpadi/sorty_sorter)
5
5
  [![Test Coverage](https://codeclimate.com/github/katpadi/sorty_sorter/badges/coverage.svg)](https://codeclimate.com/github/katpadi/sorty_sorter/coverage)
6
6
 
7
- 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.
8
-
9
- 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.
10
-
7
+ This gem sorts AR collection when given a set of parameters that will be validated against the whitelisted attributes in the model.
11
8
 
12
9
  ## Installation
13
10
 
@@ -25,7 +22,7 @@ Or install it yourself as:
25
22
 
26
23
  ## Usage
27
24
 
28
- 1. Add declaration of sorting columns to model:
25
+ 1. Define whitelist attributes to model:
29
26
 
30
27
  ```ruby
31
28
  sort_with update_date: { updated_at: :desc },
@@ -33,9 +30,9 @@ Or install it yourself as:
33
30
  ```
34
31
 
35
32
  In the example above, `update_date` is the exposed attribute and it represents the `updated_at` column in DB.
36
- The declaration will serve as the "valid" attributes that may be used for sorting.
33
+ The whitelist hash represents the "valid" attributes that may be sorted.
37
34
 
38
- 2. Call sort method:
35
+ 2. Call `sorty_sort` method:
39
36
 
40
37
  ```ruby
41
38
  @collection.sorty_sort('name', 'asc')
@@ -45,16 +42,27 @@ There is also a bang method `sorty_sort!` that will raise an exception if you're
45
42
 
46
43
  ## Example
47
44
 
48
- An example when used in API:
45
+ As mentioned, a definition of the whitelist hash should be done in the model.
49
46
 
50
- ```sh
51
- curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://katpadi/drones?sort=update_date&dir=desc
47
+ ```ruby
48
+ # app/models/drone.rb
49
+
50
+ class Drone < ActiveRecord::Base
51
+ # Define!
52
+ sort_with update_date: { updated_at: :desc },
53
+ points: { points: :asc },
54
+ title: { name: :asc }
55
+ end
52
56
  ```
53
57
 
54
- 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:
58
+ The outer Hash keys (i.e. `update_date`) represent the "exposed" attributes whereas their corresponding values (`updated_at: :desc`) represent the DB column name as the key, and its default sort direction in case there is no argument passed as the value.
59
+
60
+ Say you want to sort the collection based on `name`, the parameter `title` should be passed because that is the "exposed" attribute.
61
+
62
+ Since the gem already mixed in a method `sorty_sort` to the ActiveRecord::Relation, you can do the following conveniently:
55
63
 
56
64
  ```ruby
57
- Drone.available.sorty_sort(params[:sort], params[:dir])
65
+ Drone.all.sorty_sort('title', 'asc')
58
66
  ```
59
67
 
60
68
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module SortySorter
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = SortySorter::VERSION
9
9
  spec.authors = ["Kat Padilla"]
10
10
  spec.email = ["hello@katpadi.ph"]
11
- spec.summary = %q{ Sorts ActiveRecord::Relation based on dynamic parameters against the attributes defined in model. }
12
- spec.description = %q{ 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. }
11
+ spec.summary = %q{ This gem sorts AR collection when given a set of parameters that will be validated against the whitelisted attributes in the model. }
12
+ spec.description = %q{ This gem sorts AR collection when given a set of parameters that will be validated against the whitelisted attributes in the model. The gem mixed in a `sorty_sort` method in the ActiveRecord::Relation. }
13
13
  spec.homepage = "https://github.com/katpadi/sorty_sorter"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = '>= 1.9.3'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorty_sorter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kat Padilla
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-10 00:00:00.000000000 Z
11
+ date: 2016-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,9 +80,9 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '4.1'
83
- description: " This gem adds an ActiveRecord::Relation method that sorts based on
84
- dynamic column (i.e. name, updated_at, etc.) and direction (asc, desc) parameters
85
- and validated against definition in model. "
83
+ description: " This gem sorts AR collection when given a set of parameters that will
84
+ be validated against the whitelisted attributes in the model. The gem mixed in a
85
+ `sorty_sort` method in the ActiveRecord::Relation. "
86
86
  email:
87
87
  - hello@katpadi.ph
88
88
  executables: []
@@ -130,8 +130,8 @@ rubyforge_project:
130
130
  rubygems_version: 2.2.2
131
131
  signing_key:
132
132
  specification_version: 4
133
- summary: Sorts ActiveRecord::Relation based on dynamic parameters against the attributes
134
- defined in model.
133
+ summary: This gem sorts AR collection when given a set of parameters that will be
134
+ validated against the whitelisted attributes in the model.
135
135
  test_files:
136
136
  - spec/params_spec.rb
137
137
  - spec/sort_spec.rb