sortabl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7baca944a6f0e4b3e4c0e29515c4815b2ec536b5
4
+ data.tar.gz: 31383da19f0286990e1221152403a7de85d78c02
5
+ SHA512:
6
+ metadata.gz: 7933a2a368fd102bed12e92897e3ccc3c695843e6f298a4af341915a1172d3fbf8509057824fb06e937b671727630a1876e6100720b46087d717e8f7500ee56c
7
+ data.tar.gz: 7e59e61941464ab223a82df9ffa7efa128bb0d4347f74f890a7701349a952444390b2111e6b5c5fdc27255501118436f43bb2833eb18fb489a2b0a7a743558b0
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Sortabl
2
+
3
+ A Rails Plugin to simplify sorting tables.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sortabl'
11
+ ```
12
+
13
+ ## Basic Usage
14
+
15
+ ### Controller
16
+
17
+ ```ruby
18
+ @posts = Post.sortabl(sort_by: params[:sort])
19
+ ```
20
+
21
+ And that's it! Records will be sorted by permitted parameter `:sort`. If parameter `:sort` isn't permitted, records will be sorted by primary key. The `default` key can be also set as argument. Like:
22
+
23
+ ```ruby
24
+ @posts = Post.sortable(sort_by: params[:sort], default: :author)
25
+ ```
26
+
27
+ Permitted values can be every attribute of model class followed by `_asc` or `_desc`. For example: `sort: :author_asc`
28
+ If there's an attribute permitted which doesn't exist in model, it falls back to sort by `default` key. Attributes can be limited with `only` and `except`. For example:
29
+
30
+ ```ruby
31
+ @posts = Post.sortabl(sort_by: params[:sort], only: [:title, :author])
32
+ # or
33
+ @posts = Post.sortabl(sort_by: params[:sort], except: [:text])
34
+ ```
35
+
36
+
37
+ ### View
38
+
39
+ There's also a view helper for rendering table heads:
40
+
41
+ ```erb
42
+ <table>
43
+ <thead>
44
+ <%= render_th 'Author', :author, id: 'author-column', class: 'author-column' %>
45
+ </thead>
46
+ <tbody>
47
+ ...
48
+ </tbody>
49
+ </table>
50
+
51
+ # Will be rendered to:
52
+ <table>
53
+ <thead>
54
+ <th id="author-column" class="author-column"><a href="/posts?sort=author_asc">Author<i class="fa fa-sort"></i></a></th>
55
+ # or
56
+ <th id="author-column" class="author-column"><a href="/posts?sort=author_desc">Author<i class="fa fa-sort-asc"></i></a></th>
57
+ # or
58
+ <th id="author-column" class="author-column"><a href="/posts">Author<i class="fa fa-sort-desc"></i></a></th>
59
+ </thead>
60
+ <tbody>
61
+ ...
62
+ </tbody>
63
+ </table>
64
+ ```
65
+
66
+ Which hyperlink will be rendered, depends on permitted values.
67
+ For now, this gem uses [font-awesome-rails](https://github.com/bokmann/font-awesome-rails) for placing icons. So you may want to install font-awesome gem to your project as well. More icon libaries will be supported in future.
68
+ Gem works also fine with pagination like [will_paginate](https://github.com/mislav/will_paginate).
69
+
70
+ Happy sorting ;)
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
@@ -0,0 +1,60 @@
1
+ module Sortabl
2
+ module ActiveRecordExtensions
3
+ module Sortabl
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ # class Base
8
+ module ClassMethods
9
+
10
+ def sortabl *args
11
+ # Init
12
+ default = args[0][:default].to_s if args
13
+ only = args[0][:only]
14
+ except = args[0][:except]
15
+ sort_params = {}
16
+
17
+ raise "Invalid Parameters: Do not use 'only' and 'except' together!" if only.present? and except.present?
18
+
19
+ # Set default order attribute
20
+ # default:
21
+ #
22
+ order_by_default = default.present? ? default : self.primary_key
23
+
24
+ case true
25
+ # List only attributes
26
+ # only:
27
+ #
28
+ when only.present?
29
+ (only.map(&:to_s)).each do |attribute|
30
+ sort_params[attribute + '_asc'] = "#{attribute} asc"
31
+ sort_params[attribute + '_desc'] = "#{attribute} desc"
32
+ end
33
+
34
+ # List class attributes without excepts
35
+ # except:
36
+ #
37
+ when except.present?
38
+ (self.column_names - except.map(&:to_s)).each do |attribute|
39
+ sort_params[attribute + '_asc'] = "#{attribute} asc"
40
+ sort_params[attribute + '_desc'] = "#{attribute} desc"
41
+ end
42
+
43
+ # List class attribtues
44
+ #
45
+ else
46
+ self.column_names.each do |attribute|
47
+ sort_params[attribute + '_asc'] = "#{attribute} asc"
48
+ sort_params[attribute + '_desc'] = "#{attribute} desc"
49
+ end
50
+ end
51
+
52
+ # Order class object
53
+ order((sort_params[args[0][:sort_by]] or "#{order_by_default} asc"))
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,44 @@
1
+ module Sortabl
2
+ module ActionViewExtensions
3
+ module SortablHelper
4
+
5
+ # Renders table head
6
+ # Uses fontawesome as default icons
7
+ # <th 'data-sortable': 'attribute_direction'>Name <i class='fa fa-...'></th>
8
+ #
9
+ def render_th name, attribute, *args
10
+
11
+ # To provide full path with all given params
12
+ # make a copy of incoming params and override sort_param only
13
+ sort_params = params.except(:sort)
14
+
15
+ # Set default sort path
16
+ sort_params[:sort] = "#{attribute}_asc"
17
+
18
+ # if there is already a sort param set, invert or remove sort direction
19
+ if params[:sort].present?
20
+
21
+ # if sort param match current attribute, change direction
22
+ if attribute.to_s == params[:sort].gsub(/(_asc$|_desc$)/, '')
23
+
24
+ sort_direction = params[:sort].gsub(/^((?!desc$|asc$).)*/, '')
25
+
26
+ sort_params[:sort] = "#{attribute}_desc" if sort_direction == 'asc'
27
+ sort_params[:sort] = nil if sort_direction == 'desc'
28
+
29
+ end
30
+ end
31
+
32
+ # Generate HTML Code
33
+ html = <<-HTML
34
+ <th id="#{args[0][:id] if args.present?}" class="#{args[0][:class] if args.present?}" #{args[0].map{ |k,v| "#{k}='#{v}'" unless (k.to_s =~ /^data-(.*)$/).nil? }.join(' ') if args.present?}>
35
+ <a href='#{url_for(sort_params)}'>#{name}<i class='fa fa-sort#{sort_direction.present? ? "-#{sort_direction}" : ""}'></i></a>
36
+ </td>
37
+ HTML
38
+
39
+ html.html_safe
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Sortabl
2
+ VERSION = "0.1.0"
3
+ end
data/lib/sortabl.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "sortabl/version"
2
+ require "sortabl/sortabl_core"
3
+ require "sortabl/sortabl_helper"
4
+
5
+ # Extend ActiveRecord's functionality
6
+ ActiveRecord::Base.send :include, Sortabl::ActiveRecordExtensions::Sortabl
7
+
8
+ # You can do this here or in a Railtie
9
+ ActionView::Base.send :include, Sortabl::ActionViewExtensions::SortablHelper
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sortabl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frederic Walch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Check github repository for more in-depth information.
84
+ email:
85
+ - frederic.walch@informatik.hu-berlin.de
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - README.md
91
+ - lib/sortabl.rb
92
+ - lib/sortabl/sortabl_core.rb
93
+ - lib/sortabl/sortabl_helper.rb
94
+ - lib/sortabl/version.rb
95
+ homepage: https://github.com/freder1c/sortabl
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Active Record plugin which helps and simplify sorting tables.
119
+ test_files: []