rails_admin_simple_has_many 0.0.1

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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/rails_admin/ra.simple-has-many.js +132 -0
  6. data/app/assets/javascripts/rails_admin/routes.js.erb +1 -0
  7. data/app/views/rails_admin/main/_form_simple_has_many.html.haml +50 -0
  8. data/config/initializers/jsroutes.rb +4 -0
  9. data/config/initializers/rails_admin_simple_has_many.rb +10 -0
  10. data/lib/rails_admin/config/fields/types/simple_has_many.rb +13 -0
  11. data/lib/rails_admin_simple_has_many.rb +10 -0
  12. data/lib/rails_admin_simple_has_many/version.rb +3 -0
  13. data/lib/tasks/rails_admin_simple_multiselect_widget_tasks.rake +4 -0
  14. data/test/dummy/README.rdoc +28 -0
  15. data/test/dummy/Rakefile +6 -0
  16. data/test/dummy/app/assets/javascripts/application.js +13 -0
  17. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  18. data/test/dummy/app/controllers/application_controller.rb +5 -0
  19. data/test/dummy/app/helpers/application_helper.rb +2 -0
  20. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  21. data/test/dummy/bin/bundle +3 -0
  22. data/test/dummy/bin/rails +4 -0
  23. data/test/dummy/bin/rake +4 -0
  24. data/test/dummy/bin/setup +29 -0
  25. data/test/dummy/config.ru +4 -0
  26. data/test/dummy/config/application.rb +26 -0
  27. data/test/dummy/config/boot.rb +5 -0
  28. data/test/dummy/config/database.yml +25 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +41 -0
  31. data/test/dummy/config/environments/production.rb +79 -0
  32. data/test/dummy/config/environments/test.rb +42 -0
  33. data/test/dummy/config/initializers/assets.rb +11 -0
  34. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  36. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  37. data/test/dummy/config/initializers/inflections.rb +16 -0
  38. data/test/dummy/config/initializers/mime_types.rb +4 -0
  39. data/test/dummy/config/initializers/session_store.rb +3 -0
  40. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/test/dummy/config/locales/en.yml +23 -0
  42. data/test/dummy/config/routes.rb +56 -0
  43. data/test/dummy/config/secrets.yml +22 -0
  44. data/test/dummy/public/404.html +67 -0
  45. data/test/dummy/public/422.html +67 -0
  46. data/test/dummy/public/500.html +66 -0
  47. data/test/dummy/public/favicon.ico +0 -0
  48. data/test/rails_admin_simple_has_many_test.rb +7 -0
  49. data/test/test_helper.rb +20 -0
  50. metadata +164 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 66702214aec8ad68a4e7f8f1221e1c21f7ff73b0
4
+ data.tar.gz: 7a69aa0d956bdc88c45f69c1a45b73eeb704ab09
5
+ SHA512:
6
+ metadata.gz: 3bc00aaa57f7b5610a4ea3a783e7928923c2cf54d20a43730c9aa1e128cee4cec70a309e8bd4e0ee786ba1d369d590e7b489226c98405cd1d2ec7d3dddc549a9
7
+ data.tar.gz: b52cde44f87ce1f98b07ace3c9a4385dd61280b570c3eb083f89fe7d6eb3612b9f155cddc146155c4437c20d7d84a09a7221bb99ed7b559330f18e7a3a537393
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 aimannajjar
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.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = RailsAdminSimpleHasMany
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
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 = 'RailsAdminSimpleHasMany'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,132 @@
1
+ /*
2
+ * RailsAdmin simple has many
3
+ *
4
+ * Based on original filtering-simplehasmany widget but modified substantially
5
+ * to simplify it (e.g. removing the first select menu and add a destroy button)
6
+ *
7
+ */
8
+
9
+ (function($) {
10
+ $.widget("ra.simpleHasMany", {
11
+ _cache: {},
12
+
13
+ _create: function() {
14
+ this._build();
15
+ this._buildCache();
16
+ this._bindEvents();
17
+ },
18
+
19
+ _build: function() {
20
+ var i;
21
+
22
+ this.wrapper = $('<div class="ra-multiselect">');
23
+ this.wrapper.insertAfter(this.element);
24
+ this.header = $('<div class="ra-multiselect-header ui-helper-clearfix">');
25
+ this.wrapper.append(this.header);
26
+
27
+ this.columns = {
28
+ right: $('<div class="ra-multiselect-column ra-multiselect-right">')
29
+ };
30
+
31
+ for (i in this.columns) {
32
+ if (this.columns.hasOwnProperty(i)) {
33
+ this.wrapper.append(this.columns[i]);
34
+ }
35
+ }
36
+
37
+ this.collection = $('<select></select>');
38
+ this.collection.addClass("form-control ra-multiselect-collection");
39
+ this.collection.wrap('<div class="wrapper"/>');
40
+ this.selection = $('<select class="form-control ra-multiselect-selection" multiple="multiple"></select>');
41
+ this.columns.right.append(this.selection);
42
+ this.selection.wrap('<div class="wrapper"/>');
43
+ this.remove = $('<br/><a style="margin-left:10px; margin-top:5px;" href="#" class="ra-multiselect-item-remove btn btn-danger"><i class=\"icon-plus icon-minus\"></i></a>');
44
+ help_block = this.wrapper.parent().find('.help-block')[0];
45
+ this.remove.insertBefore(help_block)
46
+ this.element.css({display: "none"});
47
+ this.tooManyObjectsPlaceholder = $('<option disabled="disabled" />').text(RailsAdmin.I18n.t("too_many_objects"));
48
+ this.noObjectsPlaceholder = $('<option disabled="disabled" />').text(RailsAdmin.I18n.t("no_objects"))
49
+
50
+ if(this.options.xhr){
51
+ this.collection.append(this.tooManyObjectsPlaceholder);
52
+ }
53
+ },
54
+
55
+ _buildCache: function(options) {
56
+ var widget = this;
57
+
58
+ this.element.find("option").each(function(i, option) {
59
+ if (option.selected) {
60
+ widget._cache['o_' + option.value] = {id: option.value, value: option.innerHTML};
61
+ $(option).clone().appendTo(widget.selection).attr("selected", false).attr("title", $(option).text());
62
+ } else {
63
+ widget._cache['o_' + option.value] = {id: option.value, value: option.innerHTML};
64
+ $(option).clone().appendTo(widget.collection).attr("selected", false).attr("title", $(option).text());
65
+ }
66
+ });
67
+ },
68
+
69
+ _bindEvents: function() {
70
+ var widget = this;
71
+
72
+ this.remove.click(function(e){
73
+ selected_id = $(':selected', widget.selection)[0].value
74
+ title = $(':selected', widget.selection)[0].title
75
+ model_name = this.options.model_name
76
+ delete_path = Routes.rails_admin_delete_path(model_name, selected_id)
77
+
78
+ var answer = confirm ("Are you sure you want to delete '" + title + "' ? " +
79
+ "\nThis will take effect immediately andCANNOT be undone");
80
+ if (answer)
81
+ {
82
+ $.ajax({
83
+ type: "POST",
84
+ url: delete_path,
85
+ dataType: "json",
86
+ data: {"_method":"delete"},
87
+ complete: function(){
88
+ widget._deSelect($(':selected', widget.selection));
89
+ },
90
+ error: function() {
91
+ alert("Error: Could not delete dimension, please try again")
92
+ }
93
+ });
94
+
95
+ }
96
+ e.preventDefault();
97
+ }.bind(this));
98
+ },
99
+
100
+ _deSelect: function(options) {
101
+ var widget = this;
102
+ options.each(function(i, option) {
103
+ widget.element.find('option[value="' + option.value + '"]').removeAttr("selected");
104
+ });
105
+ $(options).appendTo(this.collection).attr('selected', false);
106
+ },
107
+
108
+
109
+ selected: function(value) {
110
+ return this.element.find('option[value="' + value + '"]').attr("selected");
111
+ },
112
+
113
+ destroy: function() {
114
+ this.wrapper.remove();
115
+ this.element.css({display: "inline"});
116
+ $.Widget.prototype.destroy.apply(this, arguments);
117
+ }
118
+ });
119
+ })(jQuery);
120
+
121
+ $(document).on('rails_admin.dom_ready', function(e, content) {
122
+ content = content ? content : $('form');
123
+ content.find('[data-simplehasmany]').each(function() {
124
+ $(this).simpleHasMany($(this).data('options'));
125
+ if ($(this).parents("#modal").length) {
126
+ return $(this).siblings('.btn').remove();
127
+ } else {
128
+ return $(this).parents('.control-group').first().remoteForm();
129
+ }
130
+ });
131
+ });
132
+
@@ -0,0 +1 @@
1
+ <%= JsRoutes.generate() %>
@@ -0,0 +1,50 @@
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
+ 'model_name': config.abstract_model.to_param,
29
+ sortable: !!field.orderable,
30
+ removable: !!field.removable,
31
+ cacheAll: !!field.associated_collection_cache_all,
32
+ regional: {
33
+ chooseAll: t("admin.misc.chose_all"),
34
+ chosen: t("admin.misc.chosen", name: config.label_plural),
35
+ clearAll: t("admin.misc.clear_all"),
36
+ search: t("admin.misc.search"),
37
+ up: t("admin.misc.up"),
38
+ down: t("admin.misc.down")
39
+ }
40
+ }
41
+
42
+ %input{name: form.dom_name(field), type: "hidden", value: ""}
43
+ - selected_ids = (hdv = field.form_default_value).nil? ? selected_ids : hdv
44
+ = form.select field.method_name, collection, { selected: selected_ids, object: form.object }, field.html_attributes.reverse_merge({data: { simplehasmany: true, options: js_data.to_json }, multiple: true})
45
+ - if authorized?(:new, config.abstract_model) && field.inline_add
46
+ - path_hash = { model_name: config.abstract_model.to_param, modal: true }
47
+ - path_hash.merge!({ associations: { field.inverse_of => (form.object.persisted? ? form.object.id : 'new') } }) if field.inverse_of
48
+ = link_to "<i class=\"icon-plus icon-white\"></i> ".html_safe, '#', data: { link: new_path(path_hash) }, class: "create btn btn-info", style: 'margin-left:10px'
49
+ = javascript_include_tag "rails_admin/routes"
50
+ = javascript_include_tag "rails_admin/ra.simple-has-many"
@@ -0,0 +1,4 @@
1
+ require 'js_routes'
2
+ ::JsRoutes.setup do |config|
3
+
4
+ end
@@ -0,0 +1,10 @@
1
+ module RailsAdminSimpleHasMany
2
+ class SimpleHasManyType < RailsAdmin::Config::Fields::Types::HasManyAssociation
3
+ # # Register field type for the type loader
4
+ RailsAdmin::Config::Fields::Types.register(:simple_has_many, self)
5
+
6
+ register_instance_option :partial do
7
+ :form_simple_has_many
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails_admin/config/fields/association'
2
+
3
+ module RailsAdminSimpleHasMany
4
+ module Config
5
+ module Fields
6
+ module Types
7
+ class RailsAdminSimpleMultiselect < RailsAdmin::Config::Fields::Types::HasManyAssociation
8
+ RailsAdmin::Config::Fields::Types.register(:simple_multiselect, self)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module RailsAdminSimpleHasMany
2
+
3
+ class Engine < Rails::Engine
4
+ initializer 'RailsAdmin Simple Has Many precompile hook', group: :all do |app|
5
+ app.config.assets.precompile += %w( rails_admin/routes.js rails_admin/ra.simple-has-many.js )
6
+ end
7
+ end
8
+
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminSimpleHasMany
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_admin_simple_has_many do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts "== Installing dependencies =="
12
+ system "gem install bundler --conservative"
13
+ system "bundle check || bundle install"
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system "bin/rake db:setup"
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system "rm -f log/*"
25
+ system "rm -rf tmp/cache"
26
+
27
+ puts "\n== Restarting application server =="
28
+ system "touch tmp/restart.txt"
29
+ end