rails_admin_selectize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e1b9435aff8313e8ca0e535b052f4d7a24a7035dd88c47813840aa10635835c
4
+ data.tar.gz: 10ba76fdcb5f76c14dd94ce594298f7eec1ecd1b8952db21b8604e230cc35e4c
5
+ SHA512:
6
+ metadata.gz: 1aed00a5a2bd045b4237f6d2e77797358d64089fcf2fe2d458be5d9065d67318df12beb43a66bcc5bd178e65c869e1392a002299060a432fce3ac8a46a095c47
7
+ data.tar.gz: 76be6d08334f2f74f29281a2015116d6e3c1afce9b8feb90112d107ddd7ae761f166a9c3625e612b193e95cf461b360de7ac6ad876a3ddf860051a4dd1fccb24
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Gabriele Tassoni
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,35 @@
1
+ # RailsAdminSelectize
2
+ The default multiselect widget for create and edit forms in [Rails Admin](https://github.com/sferik/rails_admin) is useful, for sure, but some of my custerms find it a bit unpleasant to the eyes. Chatting around with some of them I found they expected, for multi select an approach similar to the normal select, a more streamlined and small element in the page.
3
+ Since I used, some time ago, the [selectize.js](https://selectize.github.io/selectize.js/) library, and found it to be full with options and beautiful, I started to look around and find a selectize custom field for Rails Admin, what I found is this [rails_admin_selectize](https://github.com/glyph-fr/rails_admin_selectize), but soon I noticed it was completely abandoned and targeted at very old versions of rails admin, not working in newer versions by just changing the dependency, so I decided to start from scratch and integrate the selectize into the Rails Admin environment one step at a time.
4
+
5
+ This Rails Admin Custom Field is not a fully custimizable selectize implementation, rathere a really straight forward one, aimed at using a selectize Field the KISSier way. I will maintain this and probably add some of the configurations from time to time, when needs arises in one of my future commissions, the code is kept KISS as more as I can, open to further implementations.
6
+
7
+ ## Usage
8
+ Just add :selectize to a field configuration or definition in a rails admin model.
9
+
10
+ ```ruby
11
+ configure :has_many_through_association_field_name, :selectize
12
+ ```
13
+
14
+ ## Installation
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'rails_admin_selectize'
19
+ ```
20
+
21
+ And then execute:
22
+ ```bash
23
+ $ bundle
24
+ ```
25
+
26
+ Or install it yourself as:
27
+ ```bash
28
+ $ gem install rails_admin_selectize
29
+ ```
30
+
31
+ ## Contributing
32
+ Contribution directions go here.
33
+
34
+ ## License
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RailsAdminSelectize'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ load 'rails/tasks/statistics.rake'
18
+
19
+ require 'bundler/gem_tasks'
@@ -0,0 +1,54 @@
1
+ :ruby
2
+ related_id = params[:associations] && params[:associations][field.name.to_s]
3
+ config = field.associated_model_config
4
+ source_abstract_model = RailsAdmin.config(form.object.class).abstract_model
5
+
6
+ if form.object.new_record? && related_id.present? && related_id != 'new'
7
+ selected = [config.abstract_model.get(related_id)]
8
+ else
9
+ selected = form.object.send(field.name)
10
+ end
11
+ selected_ids = selected.map{|s| s.send(field.associated_primary_key)}
12
+
13
+ current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update'
14
+
15
+ xhr = !field.associated_collection_cache_all
16
+
17
+ collection = if xhr
18
+ selected.map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] }
19
+ else
20
+ i = 0
21
+ controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] }.sort_by {|a| [selected_ids.index(a[1]) || selected_ids.size, i+=1] }
22
+ end
23
+
24
+ js_data = {
25
+ xhr: xhr,
26
+ :'edit-url' => (authorized?(:edit, config.abstract_model) ? edit_path(model_name: config.abstract_model.to_param, id: '__ID__') : ''),
27
+ remote_source: index_path(config.abstract_model, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, associated_collection: field.name, current_action: current_action, compact: true),
28
+ plugins: ['remove_button']
29
+ }
30
+
31
+ - selected_ids = (hdv = field.form_default_value).nil? ? selected_ids : hdv
32
+ = form.select field.method_name, collection, { selected: selected_ids, object: form.object }, field.html_attributes.reverse_merge({data: { selectize: true, options: js_data.to_json }, multiple: true, style: 'width:25.5em', class: "form-control pull-left"})
33
+
34
+ - if authorized?(:new, config.abstract_model) && field.inline_add
35
+ - path_hash = { model_name: config.abstract_model.to_param, modal: true }
36
+ - path_hash.merge!({ associations: { field.inverse_of => (form.object.persisted? ? form.object.id : 'new') } }) if field.inverse_of
37
+ = link_to "<i class=\"icon-plus icon-white\"></i> ".html_safe + wording_for(:link, :new, config.abstract_model), '#', data: { link: new_path(path_hash) }, class: "create btn btn-info", style: 'margin-left:10px'
38
+
39
+ :javascript
40
+ const selectizeField = $("##{field.abstract_model.to_s.parameterize.underscore}_#{field.method_name}")
41
+ selectizeField.selectize(selectizeField.data('options'))
42
+ if (selectizeField.parents("#modal").length) {
43
+ selectizeField.siblings('.btn').remove();
44
+ } else {
45
+ selectizeField.parents('.control-group').first().remoteForm({success: function(e) {
46
+ const lastOption = $(this).find("select option").last().first();
47
+ const value = lastOption.val();
48
+ const text = lastOption.text()
49
+ const selectized = selectizeField[0].selectize;
50
+ selectized.addOption({ text: text, value: value });
51
+ selectized.refreshOptions(false);
52
+ selectized.addItem(value);
53
+ }});
54
+ }
@@ -0,0 +1,36 @@
1
+ require "rails_admin_selectize/engine"
2
+
3
+ module RailsAdminSelectize
4
+ # Your code goes here...
5
+ end
6
+
7
+ require 'selectize-rails'
8
+ require 'rails_admin/config/fields/types/has_many_association'
9
+
10
+ module RailsAdmin
11
+ module Config
12
+ module Fields
13
+ module Types
14
+ class Selectize < RailsAdmin::Config::Fields::Types::HasManyAssociation
15
+ RailsAdmin::Config::Fields::Types.register(self)
16
+
17
+ register_instance_option :partial do
18
+ # nested_form ? :form_nested_many : :form_filtering_multiselect
19
+ :form_selectize
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ # RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
28
+ # if properties[:name] == :selectize
29
+ # fields << RailsAdmin::Config::Fields::Types::Selectize.new(parent, properties[:name], properties)
30
+ # true
31
+ # else
32
+ # false
33
+ # end
34
+ # end
35
+
36
+
@@ -0,0 +1,4 @@
1
+ module RailsAdminSelectize
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminSelectize
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_selectize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriele Tassoni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-22 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: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails_admin
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
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: selectize-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
55
+ description: Selectize is a fantastic way to select values from and association.
56
+ email:
57
+ - gabriele.tassoni@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/views/rails_admin/main/_form_selectize.html.haml
66
+ - lib/rails_admin_selectize.rb
67
+ - lib/rails_admin_selectize/engine.rb
68
+ - lib/rails_admin_selectize/version.rb
69
+ homepage: https://github.com/gabrieletassoni
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.6
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Add Selectize as a field.
92
+ test_files: []