administrate_ransack 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: faad99daa51ceb91223e79770bc0df8aeb7adae21d76044b510cd7628a8fe323
4
+ data.tar.gz: 3b1e7a5f12bc54086a182a72715342c52c79e0bdeef77af03a79bf0f659aa590
5
+ SHA512:
6
+ metadata.gz: bb14807a4275bdc1d62467bca4d30bc3ec9010df4af46516351ad3ec63b9cd863fce18a87dfac75d20d35a785561fea58a254457eadb645d69d9d0163878dd2c
7
+ data.tar.gz: e4f7f6c47e66712a9c574ff0394739189a402f33281612113129ec10af6166d85ef5a8d73b0922a5d47e99f245e18ddd961abdec48cb491e525fd4b44110b9d2
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Mattia Roccoberton
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Administrate Ransack [![Gem Version](https://badge.fury.io/rb/administrate_ransack.svg)](https://badge.fury.io/rb/administrate_ransack)
2
+ A plugin for [Administrate](https://github.com/thoughtbot/administrate) to use [Ransack](https://github.com/activerecord-hackery/ransack) for filtering resources.
3
+
4
+ Features:
5
+ - add Ransack method via module prepend in controller;
6
+ - offer a filters bar based on the resource's attributes;
7
+ - customize searchable attributes.
8
+
9
+ ## Installation
10
+ - After installing Administrate, add to *Gemfile*: `gem 'administrate_ransack'` (and execute `bundle`)
11
+ - Edit your admin resource controller adding inside the class body:
12
+ ```rb
13
+ prepend AdministrateRansack::Searchable
14
+ ```
15
+ - Add to your resource index view:
16
+ ```erb
17
+ <%= render('administrate_ransack/filters', attribute_types: page.attribute_types) %>
18
+ ```
19
+
20
+ ## Customizations
21
+ - Setup the fields for the filters in the index view:
22
+ ```erb
23
+ <% attribute_types = {
24
+ author: Administrate::Field::BelongsTo,
25
+ title: Administrate::Field::String,
26
+ published: Administrate::Field::Boolean
27
+ } %>
28
+ <%= render(
29
+ 'administrate_ransack/filters',
30
+ attribute_types: attribute_types
31
+ ) %>
32
+ ```
33
+ - Optionally setup the layout for filters as a sidebar:
34
+ ```css
35
+ .main-content__body {
36
+ display: inline-block;
37
+ width: calc(100% - 320px);
38
+ vertical-align: top;
39
+ }
40
+
41
+ [data-administrate-ransack-filters] {
42
+ display: inline-block;
43
+ padding-left: 10px;
44
+ padding-top: 10px;
45
+ width: 300px;
46
+ }
47
+
48
+ [data-administrate-ransack-filters] .filter {
49
+ margin-bottom: 10px;
50
+ }
51
+
52
+ [data-administrate-ransack-filters] .filters-buttons {
53
+ margin-top: 30px;
54
+ }
55
+ ```
56
+
57
+ ## Notes
58
+ - For associations (has many/belongs to) the label used can be customized adding an `admin_label` method to the target model which returns a string
59
+
60
+ ## Do you like it? Star it!
61
+ If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
62
+
63
+ ## Contributors
64
+ - [Mattia Roccoberton](https://blocknot.es/): author
65
+
66
+ ## License
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
@@ -0,0 +1,60 @@
1
+ <%= search_form_for [:admin, @ransack_results], html: { 'data-administrate-ransack-filters': '1' } do |f| %>
2
+ <div class="filters">
3
+ <% attribute_types.each do |field, type| %>
4
+ <% next if field == :id %>
5
+
6
+ <div class="filter">
7
+ <% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
8
+ <% case input_type %>
9
+ <% when 'Administrate::Field::BelongsTo' %>
10
+ <% association = @ransack_results.klass.reflections[field.to_s] %>
11
+ <% if association %>
12
+ <% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
13
+ <%= f.label field %>
14
+ <%= f.collection_select "#{field}_id_eq", association.klass.all, :id, label, include_blank: true %>
15
+ <% end %>
16
+ <% when 'Administrate::Field::Boolean' %>
17
+ <%= f.label field %>
18
+ <%= f.select "#{field}_eq", [['no', false], ['yes', true]], include_blank: true %>
19
+ <% when 'Administrate::Field::Date' %>
20
+ <%= f.label field %>
21
+ <%= f.date_field "#{field}_gteq" %>
22
+ <%= f.date_field "#{field}_lteq" %>
23
+ <% when 'Administrate::Field::DateTime' %>
24
+ <%= f.label field %>
25
+ <%= f.datetime_field "#{field}_gteq" %>
26
+ <%= f.datetime_field "#{field}_lteq" %>
27
+ <% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
28
+ <%= f.label field %>
29
+ <%= f.search_field "#{field}_cont" %>
30
+ <% when 'Administrate::Field::Number' %>
31
+ <%= f.label field %>
32
+ <%= f.number_field "#{field}_eq" %>
33
+ <% when 'Administrate::Field::HasMany' %>
34
+ <% association = @ransack_results.klass.reflections[field.to_s] %>
35
+ <% if association %>
36
+ <% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
37
+ <%= f.label field %>
38
+ <%= f.collection_check_boxes "#{field}_id_in", association.klass.all, :id, label do |b| %>
39
+ <%= b.label do %>
40
+ <%= b.check_box %>
41
+ <span><%= b.object.send(label) %></span>
42
+ <% end %>
43
+ <% end %>
44
+ <% end %>
45
+ <% when 'Administrate::Field::Select' %>
46
+ <%= f.label field %>
47
+ <%= f.select "#{field}_eq", type.options[:collection], include_blank: true %>
48
+ <% end %>
49
+ <%# unsupported Field::HasOne %>
50
+ <%# unsupported Field::Polymorphic %>
51
+ <%# unsupported Field::Password %>
52
+ </div>
53
+ <% end %>
54
+ </div>
55
+
56
+ <div class="filters-buttons">
57
+ <%= f.submit %>
58
+ <%= link_to t('administrate_ransack.filters.clear_filters'), url_for([:admin, @ransack_results.klass]), class: 'btn-clear-filters' %>
59
+ </div>
60
+ <% end %>
@@ -0,0 +1,4 @@
1
+ en:
2
+ administrate_ransack:
3
+ filters:
4
+ clear_filters: Clear filters
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'administrate_ransack/engine'
4
+ require 'administrate_ransack/searchable'
5
+
6
+ module AdministrateRansack
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdministrateRansack
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace AdministrateRansack
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ransack'
4
+
5
+ module AdministrateRansack
6
+ module Searchable
7
+ def scoped_resource
8
+ @ransack_results = super.ransack(params[:q])
9
+ @ransack_results.result(distinct: true)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdministrateRansack
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: administrate_ransack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: administrate
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.14.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.14.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: ransack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.3.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.90'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.90'
55
+ description: A plugin for Administrate to use Ransack for filtering resources
56
+ email:
57
+ - mat@blocknot.es
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/views/administrate_ransack/_filters.html.erb
66
+ - config/locales/en.yml
67
+ - lib/administrate_ransack.rb
68
+ - lib/administrate_ransack/engine.rb
69
+ - lib/administrate_ransack/searchable.rb
70
+ - lib/administrate_ransack/version.rb
71
+ homepage: https://github.com/blocknotes/administrate_ransack
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Administrate Ransack plugin
94
+ test_files: []