rails_flexible_sorting 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +71 -0
- data/app/helpers/flexible_sorting/sortable_helper.rb +24 -0
- data/app/views/flexible_sorting/_label.html.erb +13 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/flexible_sorting.rb +85 -0
- data/lib/rails_flexible_sorting.rb +8 -0
- data/rails_flexible_sorting-1.0.1.gem +0 -0
- data/rails_flexible_sorting.gemspec +29 -0
- metadata +13 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 244b204e49fa2de839259919c3f13e3bd51529982270c96ae01bb2fe4d027883
|
4
|
+
data.tar.gz: d74868461a9410dddd9a94db9cd9dde54b51692b082b3ed81adeef770929fe58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e6e5964437041df7b4bca58e39a4d78bb97ee3788af52397535d0e11df4db59b603af99a054c090fd51af54a9b527179a61dc4249d5784ae55ab0e7f9b290c9
|
7
|
+
data.tar.gz: 294bf3a90feed2df35af3b906c34556d1399a5424b5d96454cfd4951eca29bac0fe34e9e10ef12a10687ac5b86434d6b569f2f6cb0993a1c54eba5acbb1f6788
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# FlexibleSorting
|
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 "rails_flexible_sorting"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install rails_flexible_sorting
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```
|
25
|
+
class User < ApplicationRecord
|
26
|
+
include FlexibleSorting::Model
|
27
|
+
|
28
|
+
belongs_to :service
|
29
|
+
scope :sorted_by_service, -> { all.sort_by { |service| service.method } }
|
30
|
+
|
31
|
+
sortable :id, :updated_at, :email
|
32
|
+
sortable :profile_name, -> { joins(:profile) }, column: "profiles.name"
|
33
|
+
sortable :network, -> { sorted_by_service }, virtual: true
|
34
|
+
sortable :company, -> (obj, name) { obj.company_label_for(name) }, virtual: true, as_instance_method: true
|
35
|
+
sortable :state, -> (obj) { obj.state_label }, virtual: true, as_instance_method: true
|
36
|
+
|
37
|
+
def company_label_for(name)
|
38
|
+
# do something and return value
|
39
|
+
end
|
40
|
+
|
41
|
+
def state_label
|
42
|
+
# do something and return value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
In Views
|
48
|
+
|
49
|
+
```
|
50
|
+
<th><%= sortable :email, default: true %></th>
|
51
|
+
<th><%= sortable :updated_at, direction: :desc %></th>
|
52
|
+
<th>
|
53
|
+
<%= sortable :profile_name do %>
|
54
|
+
Custom label
|
55
|
+
<% end %>
|
56
|
+
</th>
|
57
|
+
```
|
58
|
+
If you want customize how arrows are generated copy `views/lexible_sorting/_label.html.erb`
|
59
|
+
and change how you want.
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
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.
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
68
|
+
|
69
|
+
## Code of Conduct
|
70
|
+
|
71
|
+
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).
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FlexibleSorting::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
|
+
url = url_for params.permit!.merge(sort: column, direction: new_direction)
|
14
|
+
|
15
|
+
link_to(url, options) do
|
16
|
+
label = block_given? ? capture(&block) : column
|
17
|
+
sortable_label_for(label, current, current_direction, scope)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def sortable_label_for(label, active, direction, scope)
|
22
|
+
render "flexible_sorting/label", label: label, active: active, direction: direction, scope: scope
|
23
|
+
end
|
24
|
+
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 "rails_flexible_sorting"
|
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
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
module FlexibleSorting
|
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, args = [])
|
16
|
+
FlexibleSorting::Sort.new(self, column, direction, args).all
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Column
|
22
|
+
attr_reader :name, :column, :scope, :virtual, :as_instance_method, :method_args
|
23
|
+
def initialize(name, scope = nil, column: name, virtual: false, as_instance_method: false)
|
24
|
+
@name = name
|
25
|
+
@column = column
|
26
|
+
@scope = scope
|
27
|
+
@virtual = virtual
|
28
|
+
@as_instance_method = as_instance_method
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Sort
|
33
|
+
attr_reader :scope
|
34
|
+
|
35
|
+
def initialize(scope, column, direction, args = [])
|
36
|
+
@scope = scope
|
37
|
+
@column = column
|
38
|
+
@direction = direction
|
39
|
+
@args = args
|
40
|
+
end
|
41
|
+
|
42
|
+
def columns
|
43
|
+
@columns = create_columns
|
44
|
+
end
|
45
|
+
|
46
|
+
def all
|
47
|
+
all = scope.all
|
48
|
+
|
49
|
+
if column
|
50
|
+
if column.scope.is_a?(Proc) && column.virtual && column.as_instance_method
|
51
|
+
all = all.sort_by { |e| column.scope.call(*[e, *args]) }
|
52
|
+
elsif column.scope.present?
|
53
|
+
all = all.merge(column.scope)
|
54
|
+
end
|
55
|
+
|
56
|
+
if column.virtual
|
57
|
+
all.reverse! if direction == "desc"
|
58
|
+
else
|
59
|
+
all = all.order("#{column.column} #{direction}")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
all
|
64
|
+
end
|
65
|
+
|
66
|
+
def direction
|
67
|
+
@direction.to_s == "desc" ? "desc" : "asc"
|
68
|
+
end
|
69
|
+
|
70
|
+
def column
|
71
|
+
columns[@column.to_s]&.first
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_columns
|
75
|
+
scope.sortable.flat_map do |columns|
|
76
|
+
columns = [columns] unless columns.all? { |column| column.is_a?(Symbol) }
|
77
|
+
columns.map { |column| Column.new(*column) }
|
78
|
+
end.group_by { |column| column.name.to_s }
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
attr_reader :args
|
84
|
+
end
|
85
|
+
end
|
Binary file
|
@@ -0,0 +1,29 @@
|
|
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 = "rails_flexible_sorting"
|
6
|
+
spec.version = "1.0.2"
|
7
|
+
spec.authors = ["Marcin Brysz"]
|
8
|
+
spec.email = ["bryszmarcin@gmail.com"]
|
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/Marcin81/rails_flexible_sorting"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "rails", ">= 5.2"
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
29
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_flexible_sorting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcin Brysz
|
@@ -72,12 +72,21 @@ email:
|
|
72
72
|
executables: []
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
|
-
files:
|
76
|
-
|
75
|
+
files:
|
76
|
+
- README.md
|
77
|
+
- app/helpers/flexible_sorting/sortable_helper.rb
|
78
|
+
- app/views/flexible_sorting/_label.html.erb
|
79
|
+
- bin/console
|
80
|
+
- bin/setup
|
81
|
+
- lib/flexible_sorting.rb
|
82
|
+
- lib/rails_flexible_sorting.rb
|
83
|
+
- rails_flexible_sorting-1.0.1.gem
|
84
|
+
- rails_flexible_sorting.gemspec
|
85
|
+
homepage: https://github.com/Marcin81/rails_flexible_sorting
|
77
86
|
licenses:
|
78
87
|
- MIT
|
79
88
|
metadata:
|
80
|
-
homepage_uri: https://
|
89
|
+
homepage_uri: https://github.com/Marcin81/rails_flexible_sorting
|
81
90
|
post_install_message:
|
82
91
|
rdoc_options: []
|
83
92
|
require_paths:
|