rails_flexible_sorting 1.0.1 → 1.0.2

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
  SHA256:
3
- metadata.gz: 1af6b0108199dd25e47d94a8f0cbeb4b4f0c2d932c20f1b5c94fbb6ca2bb46fd
4
- data.tar.gz: db3f2a139a2934f1d5561709fa0d9e06f279a8b3b3cff6c528b0d9c0acb3a5d4
3
+ metadata.gz: 244b204e49fa2de839259919c3f13e3bd51529982270c96ae01bb2fe4d027883
4
+ data.tar.gz: d74868461a9410dddd9a94db9cd9dde54b51692b082b3ed81adeef770929fe58
5
5
  SHA512:
6
- metadata.gz: 16d966fcf846d2f5ce78e4b7a48bab9beea98d1b158c8d4d19de025d624194d6aa0d92519b97223be42e90c68d32f96ddfe34051b403e2f0dd38b25de41a7344
7
- data.tar.gz: 813d644febf6b448b7c28a8a8f750aaed368e3ee494f21b1e0570fe56bfb9fd90e29398faa9b97d4fe3bf178d10745cd98e1d9c817018750ad20b110f01e2cea
6
+ metadata.gz: 4e6e5964437041df7b4bca58e39a4d78bb97ee3788af52397535d0e11df4db59b603af99a054c090fd51af54a9b527179a61dc4249d5784ae55ab0e7f9b290c9
7
+ data.tar.gz: 294bf3a90feed2df35af3b906c34556d1399a5424b5d96454cfd4951eca29bac0fe34e9e10ef12a10687ac5b86434d6b569f2f6cb0993a1c54eba5acbb1f6788
@@ -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 %>
@@ -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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "flexible_sorting"
4
+
5
+ module RailsFlexibleSorting
6
+ class Engine < Rails::Engine
7
+ end
8
+ end
@@ -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.1
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
- homepage: https://rubygems.org/gems/rails_flexible_sorting
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://rubygems.org/gems/rails_flexible_sorting
89
+ homepage_uri: https://github.com/Marcin81/rails_flexible_sorting
81
90
  post_install_message:
82
91
  rdoc_options: []
83
92
  require_paths: