rails_admin_selectize 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: c8ac8cbadaa82b7ff3ccd3eccdc5baa06fea29de55947fb5890baacc4553d621
4
+ data.tar.gz: 53c8dbc7fe5c7393e5951305be6a251d178cc29e517729355867e3b33898732d
5
+ SHA512:
6
+ metadata.gz: 6a15d1092f632591f5ba629607824ea3aefbd467a9ff0e88597b612a6ca0cc5a9265cfe1b3bb3206be053bd4cc03b216ade647eb24624761b2e1995aba04db3a
7
+ data.tar.gz: a50745c656e500ac4faabc975235730100936bb8c8a75612758992224031177ca51b8d4c8ed7f903e3042d6161bf97abefbf79b38bf9048405fc37d7edd41c83
@@ -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,2 @@
1
+ //= require thecore_ui_commons/thecore
2
+ //= require selectize
@@ -0,0 +1,2 @@
1
+ @import 'selectize';
2
+ @import 'selectize.bootstrap3';
@@ -0,0 +1,56 @@
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: "max-width: max-content", 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
+ selectizeField.selectize()
43
+ if (selectizeField.parents("#modal").length) {
44
+ selectizeField.siblings('.btn').remove();
45
+ } else {
46
+ selectizeField.parents('.control-group').first().remoteForm({success: function(e) {
47
+ const lastOption = $(this).find("select option").last().first();
48
+ const value = lastOption.val();
49
+ const text = lastOption.text()
50
+ const selectized = selectizeField[0].selectize;
51
+ selectized.addOption({ text: text, value: value });
52
+ selectized.refreshOptions(false);
53
+ selectized.addItem(value);
54
+ }});
55
+
56
+ }
@@ -0,0 +1,9 @@
1
+ Rails.application.configure do
2
+ config.assets.paths << root.join("app", "assets", "stylesheets", "rails_admin_selectize")
3
+ config.assets.paths << root.join("app", "assets", "javascripts", "rails_admin_selectize")
4
+ # Pages under Rails Admin
5
+ config.assets.precompile += %w( rails_admin_selectize/thecore_rails_admin.css rails_admin_selectize/thecore_rails_admin.js )
6
+
7
+ config.after_initialize do
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ require "rails_admin_selectize/engine"
2
+ require 'selectize-rails'
3
+ require 'rails_admin/config/fields/types/has_many_association'
4
+
5
+ module RailsAdminSelectize
6
+ end
7
+
8
+ module RailsAdmin
9
+ module Config
10
+ module Fields
11
+ module Types
12
+ class Selectize < RailsAdmin::Config::Fields::Types::HasManyAssociation
13
+ RailsAdmin::Config::Fields::Types.register(self)
14
+
15
+ register_instance_option :partial do
16
+ # nested_form ? :form_nested_many : :form_filtering_multiselect
17
+ :form_selectize
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
26
+ if properties.name == :selectize
27
+ fields << RailsAdmin::Config::Fields::Types::Selectize.new(parent, properties[:name], properties)
28
+ true
29
+ else
30
+ false
31
+ end
32
+ end
33
+
34
+
@@ -0,0 +1,13 @@
1
+ module RailsAdminSelectize
2
+ class Engine < ::Rails::Engine
3
+ # appending migrations to the main app's ones
4
+ initializer 'rails_admin_selectize.append_migrations' do |app|
5
+ unless app.root.to_s.match root.to_s
6
+ config.paths["db/migrate"].expanded.each do |expanded_path|
7
+ app.config.paths["db/migrate"] << expanded_path
8
+ end
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminSelectize
2
+ VERSION = "#{`git describe --tags $(git rev-list --tags --max-count=1)`}"
3
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_selectize
3
+ version: !ruby/object:Gem::Version
4
+ version: '0'
5
+ platform: ruby
6
+ authors:
7
+ - Gabriele Tassoni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-20 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/assets/javascripts/rails_admin_selectize/index.js
66
+ - app/assets/stylesheets/rails_admin_selectize/index.scss
67
+ - app/views/rails_admin/main/_form_selectize.html.haml
68
+ - config/initializers/rails_admin_selectize_app_configs.rb
69
+ - lib/rails_admin_selectize.rb
70
+ - lib/rails_admin_selectize/engine.rb
71
+ - lib/rails_admin_selectize/version.rb
72
+ homepage: https://github.com/gabrieletassoni
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Add Selectize as a field.
95
+ test_files: []