activeadmin_addons 0.10.1 → 0.11.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 262c6935a014a6a9cf1d0ffc946fcfdecc15d875
4
- data.tar.gz: 26cf0c452aac19ecf87b9519dec88e17097476f0
3
+ metadata.gz: 91d4876ea2cb7334f8c2dbc40fb17e5823478982
4
+ data.tar.gz: cde3168fdf880d9a82405bbd390387bb51bbfff4
5
5
  SHA512:
6
- metadata.gz: cd2f61ef49875b490e0350558a51dfc237dd1d733c631e5b654282f546ecb9a5d91a7a8dee37588f9150810552a5a617c9b7cae0eb0ac48bd850cb5c4dbc2f7a
7
- data.tar.gz: 9eeca7a307ab62d44f32834937473cbb3b4068ef53bc8cce7bcc04d2df4cbc4d0854910dc650b84c14fbe655437514226e8437bb3d8f2ef1ebe74e60942de51b
6
+ metadata.gz: bbedf34cd625f6c3379730abd2b1b9055b044080fc4c5d6b77499a02f1c61a49441c1d82c834de2971caa554df7908fcf0c55bca452ac28aa43583500131af60
7
+ data.tar.gz: e27cbfb2b0272abe956a1f4fed8520a5e8ee898ec838d7837ad98af96d75f35bd76789377e484a4a700984114fa47ed52a829a3f7cac8526d589c1ab4ebc5ace
@@ -1,3 +1,4 @@
1
+ #= require ./config
1
2
  #= require ./select2
2
3
  #= require ./date-time-picker
3
4
  #= require ./color-picker
@@ -0,0 +1,7 @@
1
+ $(function() {
2
+ ActiveadminAddons = {
3
+ config: {
4
+ defaultSelect: $('body').data('default-select')
5
+ }
6
+ }
7
+ });
@@ -2,182 +2,208 @@
2
2
  //= require_self
3
3
 
4
4
  $(function() {
5
- setupSelect2(document);
5
+ configureSelect2(document);
6
6
 
7
7
  $(document).on('has_many_add:after', function(event, container){
8
- setupSelect2(container);
8
+ configureSelect2(container);
9
9
  });
10
10
 
11
- function setupSelect2(container) {
11
+ function configureSelect2(container) {
12
12
  var INVALID_PARENT_ID = -1;
13
13
  var DEFAULT_SELECT_WIDTH = '80%';
14
14
 
15
- $('.select2-tags', container).each(function(i, el) {
16
- var model = $(el).data('model'),
17
- method = $(el).data('method'),
18
- width = $(el).data('width'),
19
- selectOptions = {
20
- width: width || DEFAULT_SELECT_WIDTH,
21
- tags: $(el).data('collection')
22
- };
23
-
24
- if(!!model) {
25
- selectOptions.createSearchChoice = function() { return null; };
26
- var prefix = model + '_' + method;
27
- $(el).on('select2-selecting', onItemAdded);
28
- $(el).on('select2-removed', onItemRemoved);
29
- }
15
+ setupDefaultBehaviour();
16
+ setupAjaxSearch();
17
+ setupTags();
18
+
19
+ function setupTags() {
20
+ $('.select2-tags', container).each(function(i, el) {
21
+ var model = $(el).data('model'),
22
+ method = $(el).data('method'),
23
+ width = $(el).data('width'),
24
+ selectOptions = {
25
+ width: width || DEFAULT_SELECT_WIDTH,
26
+ tags: $(el).data('collection')
27
+ };
28
+
29
+ if(!!model) {
30
+ selectOptions.createSearchChoice = function() { return null; };
31
+ var prefix = model + '_' + method;
32
+ $(el).on('select2-selecting', onItemAdded);
33
+ $(el).on('select2-removed', onItemRemoved);
34
+ }
30
35
 
31
- $(el).select2(selectOptions);
36
+ $(el).select2(selectOptions);
32
37
 
33
- function onItemRemoved(event) {
34
- var itemId = '[id=\'' + prefix + '_' + event.val + '\']';
35
- $(itemId).remove();
36
- }
38
+ function onItemRemoved(event) {
39
+ var itemId = '[id=\'' + prefix + '_' + event.val + '\']';
40
+ $(itemId).remove();
41
+ }
37
42
 
38
- function onItemAdded(event) {
39
- var selectedItemsContainer = $("[id='" + prefix + "_selected_values']"),
40
- itemName = model + '[' + method + '][]',
41
- itemId = prefix + '_' + event.val;
42
-
43
- $('<input>').attr({
44
- id: itemId,
45
- name: itemName,
46
- type: 'hidden',
47
- value: event.val
48
- }).appendTo(selectedItemsContainer);
49
- }
50
- });
43
+ function onItemAdded(event) {
44
+ var selectedItemsContainer = $("[id='" + prefix + "_selected_values']"),
45
+ itemName = model + '[' + method + '][]',
46
+ itemId = prefix + '_' + event.val;
47
+
48
+ $('<input>').attr({
49
+ id: itemId,
50
+ name: itemName,
51
+ type: 'hidden',
52
+ value: event.val
53
+ }).appendTo(selectedItemsContainer);
54
+ }
55
+ });
56
+ }
57
+
58
+ function setupAjaxSearch() {
59
+ $('.select2-ajax', container).each(function(i, el) {
60
+ var url = $(el).data('url');
61
+ var fields = $(el).data('fields');
62
+ var displayName = $(el).data('display_name');
63
+ var parent = $(el).data('parent');
64
+ var width = $(el).data('width') || DEFAULT_SELECT_WIDTH;
65
+ var model = $(el).data('model');
66
+ var responseRoot = $(el).data('response_root');
67
+ var collection = $(el).data('collection');
68
+ var minimumInputLength = $(el).data('minimum_input_length');
69
+ var order = $(el).data('order') || (fields[0] + '_desc');
70
+ var parentId = $(el).data('parent_id') || INVALID_PARENT_ID;
71
+ var selectInstance;
72
+
73
+ var ajaxOptions = {
74
+ url: url,
75
+ dataType: 'json',
76
+ delay: 250,
77
+ data: function(term) {
78
+ var textQuery = { m: 'or' };
79
+ fields.forEach(function(field) {
80
+ if (field == "id") {
81
+ textQuery[field + '_eq'] = term;
82
+ } else {
83
+ textQuery[field + '_contains'] = term;
84
+ }
85
+ });
86
+
87
+ var query = {
88
+ order: order,
89
+ q: {
90
+ groupings: [textQuery],
91
+ combinator: 'and'
92
+ }
93
+ };
94
+
95
+ if (!!parent) {
96
+ query.q[parent + '_eq'] = parentId;
97
+ }
98
+
99
+ return query;
100
+ },
101
+ results: function(data, page) {
102
+ if(data.constructor == Object) {
103
+ data = data[responseRoot];
104
+ }
51
105
 
52
- $('select:not(.default-select)', container).each(function(i, el) {
53
- var firstOption = $('option', el).first(),
106
+ return {
107
+ results: jQuery.map(data, function(resource) {
108
+ return {
109
+ id: resource.id,
110
+ text: resource[displayName].toString()
111
+ };
112
+ })
113
+ };
114
+ },
115
+ cache: true
116
+ };
117
+
118
+ var collectionOptions = function(query) {
119
+ var data = { results: [] };
120
+
121
+ collection.forEach(function(record) {
122
+ var matched = fields.some(function(field) {
123
+ var regex = new RegExp(query.term, 'i');
124
+ return !!record[field] && !!record[field].match(regex);
125
+ });
126
+
127
+ if((!parent || record[parent] == parentId) && matched) {
128
+ data.results.push({ id: record.id, text: record[displayName].toString() });
129
+ }
130
+ });
131
+
132
+ query.callback(data);
133
+ };
134
+
135
+ var select2Config = {
136
+ width: width,
137
+ containerCssClass: 'nested-select-container',
138
+ minimumInputLength: minimumInputLength,
139
+ initSelection: function(element, callback) {
140
+ var id = $(element).val();
141
+ var text = $(element).data('selected') || '';
142
+ $(element).data('selected', '');
143
+
144
+ callback({
145
+ id: id,
146
+ text: text
147
+ });
148
+ },
149
+ placeholder: ' ',
150
+ allowClear: true
151
+ };
152
+
153
+ if (!!parent) {
154
+ var parentSelector = '#' + model + '_' + parent;
155
+
156
+ $(el).parent().find(parentSelector).on('change', function(e) {
157
+ selectInstance.val(null).trigger('change');
158
+ parentId = e.val;
159
+
160
+ if(!parentId) {
161
+ parentId = INVALID_PARENT_ID;
162
+ }
163
+ });
164
+ }
165
+
166
+ if (collection) {
167
+ select2Config.query = collectionOptions;
168
+ } else {
169
+ select2Config.ajax = ajaxOptions;
170
+ }
171
+
172
+ selectInstance = $(el).select2(select2Config);
173
+ });
174
+ }
175
+
176
+ function setupSelect2(select) {
177
+ var firstOption = $('option', select).first(),
54
178
  allowClear = false;
55
179
 
56
180
  if (firstOption.val() === "" && firstOption.text() === "") {
57
181
  allowClear = true;
58
182
  }
59
183
 
60
- if ($(el).closest('.filter_form').length > 0) {
61
- $(el).select2({
184
+ if ($(select).closest('.filter_form').length > 0) {
185
+ $(select).select2({
62
186
  width: 'resolve',
63
187
  allowClear: allowClear
64
188
  });
65
189
  } else {
66
- $(el).select2({
190
+ $(select).select2({
67
191
  width: DEFAULT_SELECT_WIDTH,
68
192
  allowClear: allowClear
69
193
  });
70
194
  }
71
- });
72
-
73
- $('.select2-ajax', container).each(function(i, el) {
74
- var url = $(el).data('url');
75
- var fields = $(el).data('fields');
76
- var displayName = $(el).data('display_name');
77
- var parent = $(el).data('parent');
78
- var width = $(el).data('width') || DEFAULT_SELECT_WIDTH;
79
- var model = $(el).data('model');
80
- var responseRoot = $(el).data('response_root');
81
- var collection = $(el).data('collection');
82
- var minimumInputLength = $(el).data('minimum_input_length');
83
- var order = fields[0] + '_desc';
84
- var parentId = $(el).data('parent_id') || INVALID_PARENT_ID;
85
- var selectInstance;
86
-
87
- var ajaxOptions = {
88
- url: url,
89
- dataType: 'json',
90
- delay: 250,
91
- data: function(term) {
92
- var textQuery = { m: 'or' };
93
- fields.forEach(function(field) {
94
- textQuery[field + '_contains'] = term;
95
- });
96
-
97
- var query = {
98
- order: order,
99
- q: {
100
- groupings: [textQuery],
101
- combinator: 'and'
102
- }
103
- };
104
-
105
- if (!!parent) {
106
- query.q[parent + '_eq'] = parentId;
107
- }
108
-
109
- return query;
110
- },
111
- results: function(data, page) {
112
- if(data.constructor == Object) {
113
- data = data[responseRoot];
114
- }
115
-
116
- return {
117
- results: jQuery.map(data, function(resource) {
118
- return {
119
- id: resource.id,
120
- text: resource[displayName].toString()
121
- };
122
- })
123
- };
124
- },
125
- cache: true
126
- };
127
-
128
- var collectionOptions = function(query) {
129
- var data = { results: [] };
130
-
131
- collection.forEach(function(record) {
132
- var matched = fields.some(function(field) {
133
- var regex = new RegExp(query.term, 'i');
134
- return !!record[field] && !!record[field].match(regex);
135
- });
136
-
137
- if((!parent || record[parent] == parentId) && matched) {
138
- data.results.push({ id: record.id, text: record[displayName].toString() });
139
- }
140
- });
195
+ }
141
196
 
142
- query.callback(data);
143
- };
144
-
145
- var select2Config = {
146
- width: width,
147
- containerCssClass: 'nested-select-container',
148
- minimumInputLength: minimumInputLength,
149
- initSelection: function(element, callback) {
150
- var id = $(element).val();
151
- var text = $(element).data('selected') || '';
152
- $(element).data('selected', '');
153
-
154
- callback({
155
- id: id,
156
- text: text
157
- });
158
- }
159
- };
160
-
161
- if (!!parent) {
162
- var parentSelector = '#' + model + '_' + parent;
163
-
164
- $(el).parent().find(parentSelector).on('change', function(e) {
165
- selectInstance.val(null).trigger('change');
166
- parentId = e.val;
167
-
168
- if(!parentId) {
169
- parentId = INVALID_PARENT_ID;
170
- }
197
+ function setupDefaultBehaviour() {
198
+ if(ActiveadminAddons.config.defaultSelect == 'select2') {
199
+ $('select:not(.default-select)', container).each(function(i, el) {
200
+ setupSelect2(el);
171
201
  });
172
202
  }
173
203
 
174
- if (collection) {
175
- select2Config.query = collectionOptions;
176
- } else {
177
- select2Config.ajax = ajaxOptions;
178
- }
179
-
180
- selectInstance = $(el).select2(select2Config);
181
- });
204
+ $('select.select2', container).each(function(i, el) {
205
+ setupSelect2(el);
206
+ });
207
+ }
182
208
  }
183
209
  });
@@ -19,19 +19,28 @@ class AjaxFilterInput < Formtastic::Inputs::StringInput
19
19
  opts = {}
20
20
  opts[:class] = ['select2-ajax'].concat([@options[:class]] || []).join(' ')
21
21
  opts["data-fields"] = (@options[:fields] || []).to_json
22
- opts["data-url"] = @options[:url] || ""
22
+ opts["data-url"] = url
23
23
  opts["data-response_root"] = @options[:response_root] || @options[:url].to_s.split('/').last
24
24
  opts["data-display_name"] = @options[:display_name] || "name"
25
25
  opts["data-minimum_input_length"] = @options[:minimum_input_length] || 1
26
26
  opts["data-width"] = @options[:width] || "100%"
27
27
  opts["data-selected"] = get_selected_value(opts["data-display_name"])
28
+ opts["data-order"] = @options[:order_by] if @options[:order_by]
28
29
  super.merge opts
29
30
  end
30
31
 
31
32
  # rubocop:disable Style/RescueModifier
32
33
  def get_selected_value(display_name)
33
- filter_class = method.to_s.chomp("_id").classify.constantize
34
- selected_value = @object.conditions.first.values.first.value rescue nil
34
+ filter_class = method.to_s.chomp("_id").classify.constantize rescue @object.klass
35
+ selected_value = @object.conditions.find { |c| c.attributes.map(&:name).include?(method.to_s) }.values.first.value rescue nil
35
36
  filter_class.find(selected_value).send(display_name) if !!selected_value
36
37
  end
38
+
39
+ def url
40
+ if @options[:url].is_a?(Proc)
41
+ template.instance_eval(&@options[:url]) || ""
42
+ else
43
+ @options[:url] || ""
44
+ end
45
+ end
37
46
  end
@@ -40,7 +40,11 @@ class DateTimePickerInput < Formtastic::Inputs::StringInput
40
40
  end
41
41
 
42
42
  def input_value(input_name = nil)
43
- v = object.public_send(input_name || method)
43
+ if options[:input_html] && options[:input_html].key?(:value)
44
+ return options[:input_html][:value]
45
+ end
46
+
47
+ v = !object.nil? ? object.public_send(input_name || method) : ''
44
48
 
45
49
  if v.is_a?(Time)
46
50
  return DateTime.new(v.year, v.month, v.day, v.hour, v.min, v.sec).strftime(format)
@@ -1,6 +1,6 @@
1
1
  class SearchSelectInput < Formtastic::Inputs::StringInput
2
2
  def input_html_options
3
- relation = @object.send(attributized_method_name)
3
+ relation = !@object.nil? ? @object.send(attributized_method_name) : ''
4
4
  opts = {}
5
5
  opts[:class] = ['select2-ajax'].concat([@options[:class]] || []).join(' ')
6
6
  opts["data-fields"] = (@options[:fields] || []).to_json
@@ -10,6 +10,7 @@ class SearchSelectInput < Formtastic::Inputs::StringInput
10
10
  opts["data-minimum_input_length"] = @options[:minimum_input_length] || 1
11
11
  opts["data-width"] = @options[:width] if @options[:width]
12
12
  opts["data-selected"] = relation.try(opts["data-display_name"].to_sym)
13
+ opts["data-order"] = @options[:order_by] if @options[:order_by]
13
14
  super.merge opts
14
15
  end
15
16
  end
@@ -52,7 +52,10 @@ class TagsInput < Formtastic::Inputs::StringInput
52
52
  end
53
53
 
54
54
  opts["data-collection"] = @options[:collection].map do |item|
55
- { id: item.id, text: item.send((@options[:display_name] || "name")) }
55
+ {
56
+ id: item.send((@options[:value] || "id")),
57
+ text: item.send((@options[:display_name] || "name"))
58
+ }
56
59
  end.to_json
57
60
 
58
61
  opts
@@ -79,6 +82,6 @@ class TagsInput < Formtastic::Inputs::StringInput
79
82
  end
80
83
 
81
84
  def model_name
82
- @object.class.to_s.underscore
85
+ @object.class.to_s.underscore.tr('/', '_')
83
86
  end
84
87
  end
@@ -1,4 +1,17 @@
1
1
  module ActiveadminAddons
2
+ extend self
3
+
4
+ attr_writer :default_select
5
+
6
+ def default_select
7
+ return "select2" unless @default_select
8
+ @default_select
9
+ end
10
+
11
+ def setup
12
+ yield self
13
+ require "activeadmin_addons"
14
+ end
2
15
  end
3
16
 
4
17
  require_relative './activeadmin_addons/engine'
@@ -0,0 +1,8 @@
1
+ class ActiveAdmin::Views::Pages::Base
2
+ alias_method :original_add_classes_to_body, :add_classes_to_body
3
+
4
+ def add_classes_to_body
5
+ original_add_classes_to_body
6
+ @body.set_attribute "data-default-select", ActiveadminAddons.default_select
7
+ end
8
+ end
@@ -15,6 +15,7 @@ module ActiveAdminAddons
15
15
  require_relative './addons/list'
16
16
  require_relative './support/enumerize_formtastic_support'
17
17
  require_relative './support/set_datepicker'
18
+ require_relative 'active_admin_config'
18
19
  app.config.assets.precompile += %w(select.scss fileicons/*.png)
19
20
  end
20
21
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveadminAddons
2
- VERSION = "0.10.1"
2
+ VERSION = "0.11.0"
3
3
  end
@@ -1,6 +1,12 @@
1
1
  module ActiveadminAddons
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_initializer
7
+ template "initializer.rb", "config/initializers/activeadmin_addons.rb"
8
+ end
9
+
4
10
  def add_javascripts
5
11
  file_path = 'app/assets/javascripts/active_admin.js.coffee'
6
12
  line_to_add = "#= require activeadmin_addons/all\n"
@@ -0,0 +1,4 @@
1
+ ActiveadminAddons.setup do |config|
2
+ # Change to "default" if you want to use ActiveAdmin's default select control.
3
+ # config.default_select = "select2"
4
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin_addons
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Platanus
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2016-12-09 00:00:00.000000000 Z
14
+ date: 2017-04-01 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: railties
@@ -370,6 +370,7 @@ files:
370
370
  - app/assets/images/fileicons/file_extension_zip.png
371
371
  - app/assets/javascripts/activeadmin_addons/all.js.coffee
372
372
  - app/assets/javascripts/activeadmin_addons/color-picker.js
373
+ - app/assets/javascripts/activeadmin_addons/config.js
373
374
  - app/assets/javascripts/activeadmin_addons/date-time-picker.js
374
375
  - app/assets/javascripts/activeadmin_addons/select2.js
375
376
  - app/assets/javascripts/palette-color-picker.js
@@ -383,6 +384,7 @@ files:
383
384
  - app/inputs/search_select_input.rb
384
385
  - app/inputs/tags_input.rb
385
386
  - lib/activeadmin_addons.rb
387
+ - lib/activeadmin_addons/active_admin_config.rb
386
388
  - lib/activeadmin_addons/addons/bool_values.rb
387
389
  - lib/activeadmin_addons/addons/enum_tag.rb
388
390
  - lib/activeadmin_addons/addons/list.rb
@@ -396,6 +398,7 @@ files:
396
398
  - lib/activeadmin_addons/support/set_datepicker.rb
397
399
  - lib/activeadmin_addons/version.rb
398
400
  - lib/generators/activeadmin_addons/install/install_generator.rb
401
+ - lib/generators/activeadmin_addons/install/templates/initializer.rb
399
402
  - lib/tasks/activeadmin_addons_tasks.rake
400
403
  homepage: https://github.com/platanus/activeadmin_addons
401
404
  licenses:
@@ -417,7 +420,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
417
420
  version: '0'
418
421
  requirements: []
419
422
  rubyforge_project:
420
- rubygems_version: 2.4.5.1
423
+ rubygems_version: 2.4.8
421
424
  signing_key:
422
425
  specification_version: 4
423
426
  summary: Set of addons to help with the activeadmin ui