sortable-for-rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +10 -0
- data/app/helpers/sortable/sortable_helper.rb +22 -0
- data/app/views/sortable/_label.html.erb +13 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sortable-for-rails.rb +8 -0
- data/lib/sortable.rb +69 -0
- data/sortable-for-rails.gemspec +33 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a2d4bbc350e99a47d9970db6c37b5d89e47bf6a0f26fd4fccd1e8dec71757d84
|
4
|
+
data.tar.gz: 966cc6ebac6496a4632c886830068c7e9e30a3bead23344f94ead732e75369a1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d07d2354242f3cea174566598705962d955189f86facb9a6c0691e4b49ed3ebc65b1ca73df48ee77d6fb894cc78a9cb8e588c4dc3cb2545ddb537e7ddca44324
|
7
|
+
data.tar.gz: 4d9c3a6b109ba6d9b180843e73f2c24d90cb5d381ecb81ccb60cc1a0f8e3b0b11a9ca5e46742efa9e2f1b388ca8d0dd3fe1aa76c20ad02cfe1d6cb2fbbd78ba3
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at grzegorz.derebecki@fdb.pl. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Grzegorz Derebecki
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# SortableForRails
|
2
|
+
|
3
|
+
## Description
|
4
|
+
Simple gem for sorting data in HTML tables
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'sortable-for-rails'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install sortable-for-rails
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```
|
25
|
+
class User < ApplicationRecord
|
26
|
+
include Sortable::Model
|
27
|
+
|
28
|
+
sortable :id, :updated_at, :email
|
29
|
+
sortable :profile_name, -> { joins(:profile) }, column: "profiles.name"
|
30
|
+
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
In Views
|
35
|
+
|
36
|
+
```
|
37
|
+
<th><%= sortable :email, default: true %></th>
|
38
|
+
<th><%= sortable :updated_at, direction: :desc %></th>
|
39
|
+
<th>
|
40
|
+
<%= sortable :profile_name do %>
|
41
|
+
Custom label
|
42
|
+
<% end %>
|
43
|
+
</th>
|
44
|
+
```
|
45
|
+
If you want customize how arrows are generated copy `views/sortable/_label.html.erb`
|
46
|
+
and change how you want.
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
51
|
+
|
52
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/madmax/sortable-for-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
61
|
+
|
62
|
+
## Code of Conduct
|
63
|
+
|
64
|
+
Everyone interacting in the SortableForRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/madmax/sortable-for-rails/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sortable::SortableHelper
|
4
|
+
def sortable(column, default: false, direction: :asc, scope: nil, **options, &block)
|
5
|
+
current = column.to_s == params[:sort] || params[:sort].blank? && default
|
6
|
+
current_direction = (params[:direction] || direction).to_s
|
7
|
+
|
8
|
+
new_direction = direction.to_s
|
9
|
+
if current
|
10
|
+
new_direction = current_direction.to_s == "asc" ? "desc" : "asc"
|
11
|
+
end
|
12
|
+
|
13
|
+
link_to(url_for(sort: column, direction: new_direction), options) do
|
14
|
+
label = block_given? ? capture(&block) : column
|
15
|
+
sortable_label_for(label, current, current_direction, scope)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def sortable_label_for(label, active, direction, scope)
|
20
|
+
render "sortable/label", label: label, active: active, direction: direction, scope: scope
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<% if label.is_a?(Symbol) %>
|
2
|
+
<%= t label, scope: scope %>
|
3
|
+
<% else %>
|
4
|
+
<%= label %>
|
5
|
+
<% end %>
|
6
|
+
|
7
|
+
<% if active %>
|
8
|
+
<% if direction == "asc" %>
|
9
|
+
<i class="fas fa-chevron-up"></i>
|
10
|
+
<% else %>
|
11
|
+
<i class="fas fa-chevron-down"></i>
|
12
|
+
<% end %>
|
13
|
+
<% end %>
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sortable/for/rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/sortable.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
module Sortable
|
5
|
+
module Model
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
def sortable(*args)
|
10
|
+
@sortable ||= []
|
11
|
+
@sortable << args unless args.empty?
|
12
|
+
@sortable
|
13
|
+
end
|
14
|
+
|
15
|
+
def sorting(column, direction = :asc)
|
16
|
+
Sortable::Sort.new(self, column, direction).all
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Column
|
22
|
+
attr_reader :name, :column, :scope
|
23
|
+
def initialize(name, scope = nil, column: name)
|
24
|
+
@name = name
|
25
|
+
@column = column
|
26
|
+
@scope = scope
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Sort
|
31
|
+
attr_reader :scope
|
32
|
+
|
33
|
+
def initialize(scope, column, direction)
|
34
|
+
@scope = scope
|
35
|
+
@column = column
|
36
|
+
@direction = direction
|
37
|
+
end
|
38
|
+
|
39
|
+
def columns
|
40
|
+
@columns = create_columns
|
41
|
+
end
|
42
|
+
|
43
|
+
def all
|
44
|
+
all = scope.all
|
45
|
+
|
46
|
+
if column
|
47
|
+
all = all.merge(column.scope) if column.scope.present?
|
48
|
+
all = all.order("#{column.column} #{direction}")
|
49
|
+
end
|
50
|
+
|
51
|
+
all
|
52
|
+
end
|
53
|
+
|
54
|
+
def direction
|
55
|
+
@direction.to_s == "desc" ? "desc" : "asc"
|
56
|
+
end
|
57
|
+
|
58
|
+
def column
|
59
|
+
columns[@column.to_s]&.first
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_columns
|
63
|
+
scope.sortable.flat_map do |columns|
|
64
|
+
columns = [columns] unless columns.all? { |column| column.is_a?(Symbol) }
|
65
|
+
columns.map { |column| Column.new(*column) }
|
66
|
+
end.group_by { |column| column.name.to_s }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "sortable-for-rails"
|
6
|
+
spec.version = "1.0.0"
|
7
|
+
spec.authors = ["Grzegorz Derebecki"]
|
8
|
+
spec.email = ["grzegorz.derebecki@fdb.pl"]
|
9
|
+
|
10
|
+
spec.summary = %q{Allow easy sorting tables tables in rails}
|
11
|
+
spec.description = %q{Allow easy sorting tables tables in rails}
|
12
|
+
spec.homepage = "https://github.com/madmax/sortable-for-rails"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/madmax/sortable-for-rails"
|
17
|
+
spec.metadata["changelog_uri"] = "https://github.com/madmax/sortable-for-rails"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "rails", ">= 5.2"
|
29
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
32
|
+
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sortable-for-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Grzegorz Derebecki
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-16 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: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
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: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: Allow easy sorting tables tables in rails
|
70
|
+
email:
|
71
|
+
- grzegorz.derebecki@fdb.pl
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- app/helpers/sortable/sortable_helper.rb
|
84
|
+
- app/views/sortable/_label.html.erb
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/sortable-for-rails.rb
|
88
|
+
- lib/sortable.rb
|
89
|
+
- sortable-for-rails.gemspec
|
90
|
+
homepage: https://github.com/madmax/sortable-for-rails
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata:
|
94
|
+
homepage_uri: https://github.com/madmax/sortable-for-rails
|
95
|
+
source_code_uri: https://github.com/madmax/sortable-for-rails
|
96
|
+
changelog_uri: https://github.com/madmax/sortable-for-rails
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubygems_version: 3.0.3
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Allow easy sorting tables tables in rails
|
116
|
+
test_files: []
|