sortify 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +76 -0
- data/Rakefile +23 -0
- data/lib/sortify.rb +36 -0
- data/lib/sortify/version.rb +3 -0
- data/lib/tasks/sortify_tasks.rake +4 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3bb9b394d937118fbf5e0f2222d11b5bcaa3d7a8
|
4
|
+
data.tar.gz: b26dde80e4cd1a48a7abcd2e72a01d3a510c08ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0634c7b7de9c65a0a806e47ca830d8d22351e6b8c571b9091637ebc92e90552039be11394df802fe7a9bca6a24bf270a239937367edf61b70d3e317a65a68497
|
7
|
+
data.tar.gz: 7ddab951613d58e7c903852e230ac49b728957545dec41bd27ed8b46576ebea1fb8ab705e70e0a15411740e07265b7f3794db1c8e36c058074a7a5782644c3a6
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Melody
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
#Sortify
|
2
|
+
[![Build Status](https://travis-ci.org/meltheadorable/sortify.svg)](https://travis-ci.org/meltheadorable/sortify)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/meltheadorable/sortify/badges/gpa.svg)](https://codeclimate.com/github/meltheadorable/sortify)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/meltheadorable/sortify/badge.svg?branch=stable)](https://coveralls.io/r/meltheadorable/sortify?branch=stable)
|
5
|
+
[![Dependency Status](https://gemnasium.com/meltheadorable/sortify.svg)](https://gemnasium.com/meltheadorable/sortify)
|
6
|
+
[![Documentation Status](http://inch-ci.org/github/meltheadorable/sortify.svg?branch=develop)](http://inch-ci.org/github/meltheadorable/sortify)
|
7
|
+
|
8
|
+
Sortify helps you handle user-provided sort options in Rails apps.
|
9
|
+
|
10
|
+
> #### :warning: **Warning**:
|
11
|
+
> Sortify is in the very early stages of development right now, has no tests and could introduce breaking changes. Use at your own risk.
|
12
|
+
|
13
|
+
## Getting Started
|
14
|
+
|
15
|
+
To get started, add sortify to your gemfile:
|
16
|
+
|
17
|
+
`gem 'sortify', github: 'meltheadorable/github', branch: 'develop'`
|
18
|
+
|
19
|
+
You can then run `bundle install` to fetch the current development version.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Using Sortify is very simple. All you need to do is specify sorting options in your models, then call `sortify` in your controllers naming a sorting option.
|
24
|
+
|
25
|
+
### Models
|
26
|
+
|
27
|
+
Sortify acts as a wrapper around ActiveRecord scopes, providing a tiny bit of extra functionality to keep track of valid sorting options.
|
28
|
+
|
29
|
+
If you don't know what scopes are, [you can read about them here](http://guides.rubyonrails.org/active_record_querying.html#scopes):
|
30
|
+
|
31
|
+
First you'll need to extend Sortify, and then you can specify your sort options.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Item < ActiveRecord::Base
|
35
|
+
extend Sortify # includes the sortify methods in your model
|
36
|
+
|
37
|
+
default_sort_option :most_recent
|
38
|
+
sort_option :most_recent, -> { order(updated_at: :desc) }
|
39
|
+
sort_option :created_first, -> { order(created_at: :asc) }
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
`sort_option` takes a symbol as a name, and a lambda, exactly like an ActiveRecord scope.
|
44
|
+
|
45
|
+
You can optionally provide a `default_sort_option` which specifies which sort to use in the event that the user-specified sort is invalid or absent.
|
46
|
+
|
47
|
+
|
48
|
+
### Controllers
|
49
|
+
|
50
|
+
For your controllers, Sortify provides the `sortify` method, which takes a string naming one of your sort options as an argument. Because Sortify uses scopes under the hood, it can be chained with other scopes.
|
51
|
+
|
52
|
+
> #### :warning: **Warning**:
|
53
|
+
> The `sortify` method will raise a `NoMethodError` if it cannot find a sorting option with the name you passed in unless a valid default is specified.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class ItemController < ApplicationController
|
57
|
+
def index
|
58
|
+
@items = Item.sortify("most_recent") # sorts your items by most recent
|
59
|
+
@others = Item.sortify(params[:sort]) # sorts by the method specified in the params
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
Because it uses scopes under the hood, Sortify also provides separate methods for your sorting options:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class ItemController < ApplicationController
|
68
|
+
def index
|
69
|
+
@items = Item.created_first # all of your Items, sorted by creation date
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
Sortify is released under the [MIT License](LICENSE)
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Sortify'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
data/lib/sortify.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Sortify
|
2
|
+
def sort_option(name, body)
|
3
|
+
@sort_options ||= []
|
4
|
+
name = name.to_sym
|
5
|
+
|
6
|
+
begin
|
7
|
+
scope name, body
|
8
|
+
rescue ArgumentError => e
|
9
|
+
raise e
|
10
|
+
else
|
11
|
+
@sort_options << name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def default_sort_option(name)
|
16
|
+
@default_sort_option = name.to_sym
|
17
|
+
end
|
18
|
+
|
19
|
+
def sort_options
|
20
|
+
return @sort_options
|
21
|
+
end
|
22
|
+
|
23
|
+
def sortify(sort_option)
|
24
|
+
sort_option = sort_option.to_sym
|
25
|
+
|
26
|
+
if @sort_options.include? sort_option
|
27
|
+
self.send(sort_option)
|
28
|
+
else
|
29
|
+
begin
|
30
|
+
self.send(@default_sort_option)
|
31
|
+
rescue
|
32
|
+
raise NoMethodError, "The default sort option you provided, '#{@default_sort_option.to_s}' does not exist."
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sortify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Melody
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rspec
|
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: fuubar
|
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: coveralls
|
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: appraisal
|
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: Sortify acts as a wrapper around ActiveRecord scopes, providing a tiny
|
98
|
+
bit of extra functionality to keep track of valid sorting options and call them
|
99
|
+
from user input.
|
100
|
+
email:
|
101
|
+
- meltheadorable@gmail.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- LICENSE
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/sortify.rb
|
110
|
+
- lib/sortify/version.rb
|
111
|
+
- lib/tasks/sortify_tasks.rake
|
112
|
+
homepage: https://github.com/meltheadorable/sortify
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.4.5
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Sortify helps you handle user-provided sort options in Rails apps.
|
136
|
+
test_files: []
|