sorted-actionview 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rubocop.yml +36 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +12 -0
- data/lib/sorted/actionview/builder.rb +25 -0
- data/lib/sorted/actionview/helper.rb +67 -0
- data/lib/sorted/actionview/railtie.rb +15 -0
- data/lib/sorted/actionview/version.rb +5 -0
- data/lib/sorted/actionview.rb +7 -0
- data/sorted-actionview.gemspec +28 -0
- data/spec/sorted/actionview_spec.rb +64 -0
- data/spec/spec_helper.rb +4 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b03e155258096d07a8880ade6143619849de9d1b
|
4
|
+
data.tar.gz: 7475fdd0ae7e39fa8cf5fa302531e0f749aa8a9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee99cff98ab4b095184562d95cc53673fc86e1a4e18f2e379789a613396f77079a5350ceac4bca9b8dc1d028c63ed01c652becac37b296555cd248048a92016d
|
7
|
+
data.tar.gz: 12e254d703f967ea67cd4630ef39c97075f7b0147eb96b7bc8d28bfb4c69e9717327187e239ac22a03709057030abdd2e2791ec3361ea492fb316a5163a92e8d
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-01-31 16:31:38 +1100 using RuboCop version 0.28.0.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 1
|
9
|
+
Metrics/AbcSize:
|
10
|
+
Max: 29
|
11
|
+
|
12
|
+
# Offense count: 1
|
13
|
+
Metrics/CyclomaticComplexity:
|
14
|
+
Max: 8
|
15
|
+
|
16
|
+
# Offense count: 6
|
17
|
+
# Configuration parameters: AllowURI, URISchemes.
|
18
|
+
Metrics/LineLength:
|
19
|
+
Max: 118
|
20
|
+
|
21
|
+
# Offense count: 1
|
22
|
+
# Configuration parameters: CountComments.
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Max: 13
|
25
|
+
|
26
|
+
# Offense count: 1
|
27
|
+
Metrics/PerceivedComplexity:
|
28
|
+
Max: 9
|
29
|
+
|
30
|
+
# Offense count: 5
|
31
|
+
Style/Documentation:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
# Offense count: 2
|
35
|
+
Style/RegexpLiteral:
|
36
|
+
MaxSlashes: 0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Rufus Post
|
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,38 @@
|
|
1
|
+
# Sorted::Actionview
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/mynameisrufus/sorted-actionview.svg?branch=master)](https://travis-ci.org/mynameisrufus/sorted-actionview)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sorted-actionview'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sorted-actionview
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Generate a sorted link with the email attribute:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
link_to_sorted "Email", :email
|
27
|
+
```
|
28
|
+
|
29
|
+
Works the same as the `link_to` method except a second argument for the
|
30
|
+
sort attribute is needed.
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it ( https://github.com/[my-github-username]/sorted-actionview/fork )
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rubocop/rake_task'
|
3
|
+
require 'rspec/core'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
7
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
task default: %w(rubocop spec)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'sorted'
|
2
|
+
|
3
|
+
module Sorted
|
4
|
+
module ActionView
|
5
|
+
class Builder
|
6
|
+
attr_reader :params
|
7
|
+
|
8
|
+
def initialize(order, params = {})
|
9
|
+
@sort = ::Sorted::URIQuery.parse(params.delete(:sort))
|
10
|
+
@order = ::Sorted::SQLQuery.parse(order)
|
11
|
+
@params = params
|
12
|
+
set = @order.direction_intersect(@sort)
|
13
|
+
@params[:sort] = ::Sorted::URIQuery.encode(set)
|
14
|
+
end
|
15
|
+
|
16
|
+
def css
|
17
|
+
if @sort.keys.include? @order.keys.first
|
18
|
+
"sorted #{@sort.assoc(@order.keys.first).last}"
|
19
|
+
else
|
20
|
+
'sorted'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'sorted/actionview/builder'
|
3
|
+
|
4
|
+
module Sorted
|
5
|
+
module ActionView
|
6
|
+
module Helper
|
7
|
+
# Creates a link tag of the given +name+ and +attribute+ creating
|
8
|
+
# a url using a set of +options+.
|
9
|
+
#
|
10
|
+
# ==== Examples
|
11
|
+
#
|
12
|
+
# Basic usage
|
13
|
+
#
|
14
|
+
# link_to_sorted "Email", :email
|
15
|
+
# # => <a href="/profiles?sort=email_asc" class="desc">Email</a>
|
16
|
+
#
|
17
|
+
# Or use a block
|
18
|
+
#
|
19
|
+
# link_to_sorted :email do
|
20
|
+
# <strong>Sort by email</strong> -- <span></span>
|
21
|
+
# end
|
22
|
+
# # => <a href="/profiles?sort=email_asc" class="desc"><strong>Sort by email</strong> -- <span></span></a>
|
23
|
+
#
|
24
|
+
def link_to_sorted(*args, &block)
|
25
|
+
if block_given?
|
26
|
+
order = args[0]
|
27
|
+
options = args[1] || {}
|
28
|
+
html_options = args[2] || {}
|
29
|
+
else
|
30
|
+
block = proc { args[0].to_s }
|
31
|
+
order = args[1]
|
32
|
+
options = args[2] || {}
|
33
|
+
html_options = args[3] || {}
|
34
|
+
end
|
35
|
+
|
36
|
+
sorter = ::Sorted::ActionView::Builder.new(order, ((request.get? && !params.nil?) ? params.dup : {}))
|
37
|
+
options[:class] = [options[:class], sorter.css].join(' ').strip
|
38
|
+
link_to(url_for(sorter.params), options, html_options, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Convenience method for quickly spitting out a sorting menu.
|
42
|
+
#
|
43
|
+
# ==== Examples
|
44
|
+
#
|
45
|
+
# Basic usage
|
46
|
+
#
|
47
|
+
# sort_by :first_name, :last_name
|
48
|
+
#
|
49
|
+
# To provide a string to use instead of a column name, pass an array composed
|
50
|
+
# of your label string and the column name (symbol):
|
51
|
+
#
|
52
|
+
# sortable_by :author_name, :title, ["Date of Publication", :published_at]
|
53
|
+
#
|
54
|
+
def sortable_by(*columns)
|
55
|
+
links = content_tag :span, 'Sort by: '
|
56
|
+
columns.each do |c|
|
57
|
+
if c.is_a? Array
|
58
|
+
links += link_to_sorted(c[0], c[1].to_sym)
|
59
|
+
else
|
60
|
+
links += link_to_sorted(c.to_s.titleize, c.to_sym)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
content_tag :div, links, class: 'sortable'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'rails/railtie'
|
3
|
+
require 'sorted/actionview/helper'
|
4
|
+
|
5
|
+
module Sorted
|
6
|
+
module ActiveRecord
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
|
+
config.after_initialize do |_app|
|
9
|
+
ActiveSupport.on_load(:action_view) do
|
10
|
+
include Sorted::ActionView::Helper
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sorted/actionview/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sorted-actionview'
|
8
|
+
spec.version = Sorted::Actionview::VERSION
|
9
|
+
spec.authors = ['Rufus Post']
|
10
|
+
spec.email = ['Rufus.Post@team.telstra.com']
|
11
|
+
spec.summary = 'Actionview helpers for sorted.'
|
12
|
+
spec.description = 'Allows the use of the sorted gem with Actionview.'
|
13
|
+
spec.homepage = ''
|
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_dependency 'sorted', '~> 2.0.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'railties', '>= 4.0.0'
|
26
|
+
spec.add_development_dependency 'rubocop'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'action_view'
|
3
|
+
require 'sorted/actionview/helper'
|
4
|
+
|
5
|
+
ActionView::Base.send(:include, Sorted::ActionView::Helper)
|
6
|
+
|
7
|
+
describe Sorted::ActionView do
|
8
|
+
it 'should integrate with ActiveRecord::Base' do
|
9
|
+
expect(ActionView::Base.new.respond_to?(:link_to_sorted)).to eq(true)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should return the default sort order and preserve the existing params' do
|
13
|
+
order = :email
|
14
|
+
params = { page: 10 }
|
15
|
+
result = { page: 10, sort: 'email_asc' }
|
16
|
+
|
17
|
+
sorter = Sorted::ActionView::Builder.new order, params
|
18
|
+
expect(sorter.params).to eq(result)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should only return the sorted css class if email has not yet been sorted' do
|
22
|
+
order = :email
|
23
|
+
params = {}
|
24
|
+
result = 'sorted'
|
25
|
+
|
26
|
+
sorter = Sorted::ActionView::Builder.new order, params
|
27
|
+
expect(sorter.css).to eq(result)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should only return the sorted css class if email has not yet been sorted' do
|
31
|
+
order = :email
|
32
|
+
params = { sort: 'email_asc' }
|
33
|
+
result = 'sorted asc'
|
34
|
+
|
35
|
+
sorter = Sorted::ActionView::Builder.new order, params
|
36
|
+
expect(sorter.css).to eq(result)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should return the default order when params are empty' do
|
40
|
+
order = :email
|
41
|
+
result = { sort: 'email_asc' }
|
42
|
+
|
43
|
+
sorter = Sorted::ActionView::Builder.new order, {}
|
44
|
+
expect(sorter.params).to eq(result)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should correctly toggle multiple params' do
|
48
|
+
order = 'email DESC, name DESC'
|
49
|
+
params = { sort: 'email_asc!name_asc' }
|
50
|
+
result = { sort: 'email_desc!name_desc' }
|
51
|
+
|
52
|
+
sorter = Sorted::ActionView::Builder.new order, params
|
53
|
+
expect(sorter.params).to eq(result)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should have sort order over existing params' do
|
57
|
+
order = :email
|
58
|
+
params = { sort: 'name_asc' }
|
59
|
+
result = { sort: 'email_asc!name_asc' }
|
60
|
+
|
61
|
+
sorter = Sorted::ActionView::Builder.new order, params
|
62
|
+
expect(sorter.params).to eq(result)
|
63
|
+
end
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sorted-actionview
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rufus Post
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sorted
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: railties
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
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: rspec
|
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: Allows the use of the sorted gem with Actionview.
|
98
|
+
email:
|
99
|
+
- Rufus.Post@team.telstra.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rubocop.yml"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/sorted/actionview.rb
|
111
|
+
- lib/sorted/actionview/builder.rb
|
112
|
+
- lib/sorted/actionview/helper.rb
|
113
|
+
- lib/sorted/actionview/railtie.rb
|
114
|
+
- lib/sorted/actionview/version.rb
|
115
|
+
- sorted-actionview.gemspec
|
116
|
+
- spec/sorted/actionview_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
homepage: ''
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.4.5
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Actionview helpers for sorted.
|
142
|
+
test_files:
|
143
|
+
- spec/sorted/actionview_spec.rb
|
144
|
+
- spec/spec_helper.rb
|