nacho 0.0.2
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +34 -0
- data/app/assets/javascripts/nacho.js +197 -0
- data/lib/nacho.rb +13 -0
- data/lib/nacho/common.rb +90 -0
- data/lib/nacho/form_builder.rb +59 -0
- data/lib/nacho/helper.rb +31 -0
- data/lib/nacho/version.rb +3 -0
- data/lib/tasks/nacho_tasks.rake +4 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +26 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +0 -0
- data/test/dummy/log/test.log +5 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/nacho_test.rb +7 -0
- data/test/test_helper.rb +20 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 55069f49f226b3b6b7e0e87f1a5494a23c269c7e
|
4
|
+
data.tar.gz: a27937c4b05b03b623534b4c5bf7bae828e05184
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4206322c9b667de5b569bc8971408cd198a41ab5888df6621fcff7e7918d7766decb27b2dfe6fbbab71b5f691dfed3e993e852ff93280af600206ca7ca2f5c4d
|
7
|
+
data.tar.gz: 3351c6fc05c495258243ab2872b178313467709f155d5583cf5101c11a0a4d1555a4f3dfb9c33ed42cb01f101f28825a134f80376047aa57016bbec207d444cf
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Daniel Ray Ward
|
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/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 = 'Nacho'
|
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,197 @@
|
|
1
|
+
(function ( $ ) {
|
2
|
+
$.nacho = function(element, options) {
|
3
|
+
|
4
|
+
const HTML5_OPTION_VALUE_KEY = "data-nacho-option-value-key";
|
5
|
+
const HTML5_OPTION_TEXT_KEY = "data-nacho-option-text-key";
|
6
|
+
const HTML5_NEW_ITEM_VALUE_KEY = "data-nacho-new-value-key";
|
7
|
+
const HTML5_MODAL_ID_KEY = "data-nacho-modal-id";
|
8
|
+
const HTML5_MODAL_HTML_KEY = "data-nacho-modal";
|
9
|
+
const HTML5_ADD_NEW_VALUE_KEY = "data-nacho-add-new-value";
|
10
|
+
const HTML5_PREVIOUS_VALUE_KEY = "data-nacho-previous-value";
|
11
|
+
|
12
|
+
/*
|
13
|
+
These defautls will be overwritten if the HTML5 attributes are present
|
14
|
+
*/
|
15
|
+
var defaults = {
|
16
|
+
optionValueKey: 'value',
|
17
|
+
optionTextKey: 'text',
|
18
|
+
newItemValueKey: 'id',
|
19
|
+
formElementsToRemove: ['input[type=submit]']
|
20
|
+
};
|
21
|
+
|
22
|
+
var plugin = this;
|
23
|
+
|
24
|
+
plugin.settings = {};
|
25
|
+
|
26
|
+
var $element = $(element);
|
27
|
+
|
28
|
+
var modalId, $modal;
|
29
|
+
|
30
|
+
/*
|
31
|
+
Initialize the plugin
|
32
|
+
*/
|
33
|
+
plugin.init = function() {
|
34
|
+
plugin.settings = $.extend({}, defaults, options);
|
35
|
+
|
36
|
+
/*
|
37
|
+
Set the settings if the specified HTML5 attributes are present
|
38
|
+
*/
|
39
|
+
if ($element.attr(HTML5_OPTION_VALUE_KEY)) {
|
40
|
+
plugin.settings.optionValueKey = $element.attr(HTML5_OPTION_VALUE_KEY);
|
41
|
+
}
|
42
|
+
|
43
|
+
if ($element.attr(HTML5_OPTION_TEXT_KEY)) {
|
44
|
+
plugin.settings.optionTextKey = $element.attr(HTML5_OPTION_TEXT_KEY);
|
45
|
+
}
|
46
|
+
|
47
|
+
if ($element.attr(HTML5_NEW_ITEM_VALUE_KEY)) {
|
48
|
+
plugin.settings.newItemValueKey = $element.attr(HTML5_NEW_ITEM_VALUE_KEY);
|
49
|
+
}
|
50
|
+
|
51
|
+
$element.attr(HTML5_PREVIOUS_VALUE_KEY, $element.val());
|
52
|
+
|
53
|
+
modalId = $element.attr(HTML5_MODAL_ID_KEY);
|
54
|
+
$modal = $($element.attr(HTML5_MODAL_HTML_KEY));
|
55
|
+
|
56
|
+
insertModal();
|
57
|
+
initListeners();
|
58
|
+
};
|
59
|
+
|
60
|
+
var initListeners = function() {
|
61
|
+
/*
|
62
|
+
When the add new item is selected, then show the modal
|
63
|
+
*/
|
64
|
+
$element.on('change', function () {
|
65
|
+
if ($(this).attr(HTML5_ADD_NEW_VALUE_KEY) == $(this).val()) {
|
66
|
+
$modal.modal('show');
|
67
|
+
}
|
68
|
+
});
|
69
|
+
|
70
|
+
/*
|
71
|
+
Store the currently selected value when the select is focused on
|
72
|
+
*/
|
73
|
+
$element.on('focus', function(){
|
74
|
+
$(this).attr(HTML5_PREVIOUS_VALUE_KEY, $(this).val());
|
75
|
+
});
|
76
|
+
|
77
|
+
/*
|
78
|
+
When the referenced dialog is closed, we need to see if the select
|
79
|
+
box needs to be reset.
|
80
|
+
*/
|
81
|
+
$modal.on('hidden.bs.modal', function (e) {
|
82
|
+
/*
|
83
|
+
If the value of the box is still the the 'add new' option, then
|
84
|
+
set to the first value.
|
85
|
+
*/
|
86
|
+
if ($element.attr(HTML5_ADD_NEW_VALUE_KEY) == $element.val()) {
|
87
|
+
$element.val($element.attr(HTML5_PREVIOUS_VALUE_KEY));
|
88
|
+
}
|
89
|
+
});
|
90
|
+
|
91
|
+
/*
|
92
|
+
Add the listener for the modal submit button to create the new record
|
93
|
+
*/
|
94
|
+
$modal.find('.modal-footer button').on('click', function () {
|
95
|
+
var form = $modal.find('form');
|
96
|
+
|
97
|
+
/*
|
98
|
+
Create the ajax request with type JSON create the new record
|
99
|
+
*/
|
100
|
+
$.ajax({
|
101
|
+
type: "POST",
|
102
|
+
dataType: 'json',
|
103
|
+
url: form.attr('action'),
|
104
|
+
data: form.serializeJSON(),
|
105
|
+
success: function (data) {
|
106
|
+
|
107
|
+
var selected = $element.val() || [];
|
108
|
+
|
109
|
+
/*
|
110
|
+
When the select is a multiple selection, then we can add the new item id to the selected
|
111
|
+
array and clear out all the current options.
|
112
|
+
|
113
|
+
If the select is a single select, then we need to delete all the options
|
114
|
+
except for the last one, which is the option to add a new record
|
115
|
+
*/
|
116
|
+
if ($element.attr('multiple') == 'multiple') {
|
117
|
+
selected.push('' + data[plugin.settings.newItemValueKey]);
|
118
|
+
$element.find("option").remove()
|
119
|
+
|
120
|
+
} else {
|
121
|
+
$element.find("option:lt(-1)").remove();
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
/*
|
126
|
+
Add the new options from the data set we recieved from the server
|
127
|
+
*/
|
128
|
+
$.each(data.options, function (index, item) {
|
129
|
+
$element.prepend($('<option />').attr('value', item[plugin.settings.optionValueKey]).text(item[plugin.settings.optionTextKey]));
|
130
|
+
});
|
131
|
+
|
132
|
+
/*
|
133
|
+
When the select is set to multiple, then we set the val with the updated array.
|
134
|
+
Taking a unique in case there is an issue with repeated values.
|
135
|
+
|
136
|
+
For single select we need to add the blank option back to the top.
|
137
|
+
Set the value to the returned key.
|
138
|
+
*/
|
139
|
+
if ($element.attr('multiple') == 'multiple') {
|
140
|
+
$element.val($.unique(selected));
|
141
|
+
|
142
|
+
} else {
|
143
|
+
$element.prepend($('<option />').attr('value', '').text(''));
|
144
|
+
$element.val('' + data[plugin.settings.newItemValueKey]);
|
145
|
+
|
146
|
+
}
|
147
|
+
|
148
|
+
/*
|
149
|
+
Hide the modal
|
150
|
+
*/
|
151
|
+
$modal.modal('hide');
|
152
|
+
|
153
|
+
// trigger the updated event.
|
154
|
+
$element.trigger(jQuery.Event("nacho-select:updated"))
|
155
|
+
},
|
156
|
+
error: function (e) {
|
157
|
+
alert(e);
|
158
|
+
}
|
159
|
+
});
|
160
|
+
});
|
161
|
+
};
|
162
|
+
|
163
|
+
/*
|
164
|
+
Append the modal to the body, making it the last item in the DOM.
|
165
|
+
This is due to the HTML spec that prevents nested forms.
|
166
|
+
*/
|
167
|
+
var insertModal = function() {
|
168
|
+
// configure the modal.
|
169
|
+
var selector = plugin.settings.formElementsToRemove;
|
170
|
+
|
171
|
+
if (plugin.settings.formElementsToRemove instanceof Array) {
|
172
|
+
selector = plugin.settings.formElementsToRemove.join(',');
|
173
|
+
}
|
174
|
+
|
175
|
+
$modal.find('form').find(selector).remove();
|
176
|
+
$modal.appendTo('body');
|
177
|
+
};
|
178
|
+
|
179
|
+
plugin.init();
|
180
|
+
|
181
|
+
};
|
182
|
+
|
183
|
+
/**
|
184
|
+
* Add the nacho function to the jQuery namespace
|
185
|
+
*
|
186
|
+
* @param options The options for the plugin
|
187
|
+
* @returns {*}
|
188
|
+
*/
|
189
|
+
$.fn.nacho = function(options) {
|
190
|
+
return this.each(function() {
|
191
|
+
if (undefined == $(this).data('nacho') && $(this)[0].tagName.toLowerCase() == 'select') {
|
192
|
+
var plugin = new $.nacho(this, options);
|
193
|
+
$(this).data('nacho', plugin);
|
194
|
+
}
|
195
|
+
});
|
196
|
+
}
|
197
|
+
}( jQuery ));
|
data/lib/nacho.rb
ADDED
data/lib/nacho/common.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# The common code module for the FormBuilder helper and the form tag helper.
|
2
|
+
#
|
3
|
+
# @author Daniel Ward
|
4
|
+
module Nacho::Common
|
5
|
+
include ActionView::Context
|
6
|
+
|
7
|
+
HTML5_OPTION_VALUE_KEY = "data-nacho-option-value-key"
|
8
|
+
HTML5_OPTION_TEXT_KEY = "data-nacho-option-text-key"
|
9
|
+
HTML5_NEW_ITEM_VALUE_KEY = "data-nacho-new-value-key"
|
10
|
+
HTML5_MODAL_ID_KEY = "data-nacho-modal-id"
|
11
|
+
HTML5_MODAL_HTML_KEY = "data-nacho-modal"
|
12
|
+
HTML5_ADD_NEW_VALUE_KEY = "data-nacho-add-new-value"
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
# The helper to build the options for the form element, based on the given options.
|
17
|
+
#
|
18
|
+
# @param [Symbol] method See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select.
|
19
|
+
# @param [Array] choices The list of choices for the select, shoudl be in [[val, display],...] format
|
20
|
+
# @param [Hash] options the options to create a message with.
|
21
|
+
# @option options [Boolean] :include_blank Include a blank option (Forced to <tt>true</tt> when <tt>choices.count</tt> == 0)
|
22
|
+
# @option options [String] :new_option_text Text to display as the <tt>option</tt> that will trigger the new modal (Default "-- Add new --", will be ignored if <tt>html_options[:multiple]</tt> is set to <tt>true</tt>)
|
23
|
+
# @option options [Symbol] :value_key The attribute of the model that will be used as the <tt>option</tt> value from the JSON return when a new record is created
|
24
|
+
# @option options [Symbol] :text_key The attribute of the model that will be used as the <tt>option</tt> display content from the JSON return when a new record is created
|
25
|
+
# @option options [Symbol] :new_key The JSON key that will contain the value of the record that was created with the modal
|
26
|
+
# @option options [String] :modal_title The title of the modal (Default to "Add new <model.class.name>")
|
27
|
+
# @option options [String] :partial The form partial for the modal body
|
28
|
+
# @param [Hash] html_options See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select.
|
29
|
+
def build_options(method, choices = nil, options = {}, html_options = {})
|
30
|
+
|
31
|
+
new_option_value = Random.new.rand(-10000...-100)
|
32
|
+
modal_id_suffix = Random.new.rand(100...10000)
|
33
|
+
|
34
|
+
choices ||= []
|
35
|
+
choices << [(options[:new_option_text] || '-- Add new --'), new_option_value] unless html_options[:multiple]
|
36
|
+
|
37
|
+
options[:include_blank] = (choices.count == 1)
|
38
|
+
html_options[:include_blank] = options[:include_blank]
|
39
|
+
|
40
|
+
html_options[HTML5_ADD_NEW_VALUE_KEY] = new_option_value
|
41
|
+
html_options[HTML5_MODAL_ID_KEY] = "##{method.to_s}_nacho_modal_#{modal_id_suffix}"
|
42
|
+
html_options[HTML5_OPTION_VALUE_KEY] = options[:value_key] if options[:value_key].present?
|
43
|
+
html_options[HTML5_OPTION_TEXT_KEY] = options[:text_key] if options[:text_key].present?
|
44
|
+
html_options[HTML5_NEW_ITEM_VALUE_KEY] = options[:new_key] if options[:new_key].present?
|
45
|
+
|
46
|
+
modal_options = {
|
47
|
+
id: html_options[HTML5_MODAL_ID_KEY].gsub(/^#/, ''),
|
48
|
+
title: options[:modal_title] || "Add new #{method.to_s.humanize}",
|
49
|
+
body: options[:partial].html_safe
|
50
|
+
}
|
51
|
+
|
52
|
+
html_options[HTML5_MODAL_HTML_KEY] = escape_once nacho_form_modal(modal_options)
|
53
|
+
|
54
|
+
{
|
55
|
+
choices: choices,
|
56
|
+
html_options: html_options,
|
57
|
+
options: options,
|
58
|
+
button: button_tag((options[:new_option_text] || 'Add new'), type: :button, data: { toggle: 'modal', target: html_options[HTML5_MODAL_ID_KEY] })
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
def nacho_form_modal(options = {})
|
63
|
+
content_tag :div, id: options[:id], class: "modal fade #{options[:class]}", role: 'dialog', tabindex: '-1' do
|
64
|
+
content_tag :div, class: 'modal-dialog' do
|
65
|
+
content_tag :div, class: 'modal-content' do
|
66
|
+
|
67
|
+
head = content_tag :div, class: 'modal-header' do
|
68
|
+
[
|
69
|
+
content_tag(:button, content_tag(:span, '×', aria: {hideen: true} ), class: 'close', type: :button, data: {dismiss: :modal}, aria: {label: 'Close'}),
|
70
|
+
content_tag(:h4, options[:title], class: 'modal-title')
|
71
|
+
].join(' ')
|
72
|
+
end
|
73
|
+
|
74
|
+
body = content_tag :div, class: 'modal-body' do
|
75
|
+
options[:body]
|
76
|
+
end
|
77
|
+
|
78
|
+
footer = content_tag :div, class: 'modal-footer' do
|
79
|
+
[
|
80
|
+
content_tag(:button, 'Close', class: 'btn btn-default', type: :button, data: {dismiss: :modal}),
|
81
|
+
content_tag(:button, 'Save', class: 'btn btn-default', type: :button)
|
82
|
+
].join(' ')
|
83
|
+
end
|
84
|
+
|
85
|
+
head + body + footer
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require_relative 'common'
|
2
|
+
|
3
|
+
# module Nacho
|
4
|
+
# class FormBuilder < ActionView::Helpers::FormBuilder
|
5
|
+
module ActionView
|
6
|
+
module Helpers
|
7
|
+
module FormHelper
|
8
|
+
include Nacho::Common
|
9
|
+
|
10
|
+
# The helper for a FormBuilder class. Will create the select and the needed modal that contains the given
|
11
|
+
# form for the target model to create.
|
12
|
+
#
|
13
|
+
# A multiple select will generate a button to trigger the modal.
|
14
|
+
#
|
15
|
+
# @param [Object] object_name The form object that the select helper is being called for
|
16
|
+
# @param [Symbol] method See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
|
17
|
+
# @param [Array] choices The list of choices for the select, shoudl be in [[val, display],...] format
|
18
|
+
# @param [Hash] options the options to create a message with.
|
19
|
+
# @option options [Boolean] :include_blank Include a blank option (Forced to <tt>true</tt> when <tt>choices.count</tt> == 0)
|
20
|
+
# @option options [String] :new_option_text Text to display as the <tt>option</tt> that will trigger the new modal (Default "-- Add new --", will be ignored if <tt>html_options[:multiple]</tt> is set to <tt>true</tt>)
|
21
|
+
# @option options [Symbol] :value_key The attribute of the model that will be used as the <tt>option</tt> value from the JSON return when a new record is created
|
22
|
+
# @option options [Symbol] :text_key The attribute of the model that will be used as the <tt>option</tt> display content from the JSON return when a new record is created
|
23
|
+
# @option options [Symbol] :new_key The JSON key that will contain the value of the record that was created with the modal
|
24
|
+
# @option options [String] :modal_title The title of the modal (Default to "Add new <model.class.name>")
|
25
|
+
# @option options [String] :partial The form partial for the modal body
|
26
|
+
# @param [Hash] html_options See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
|
27
|
+
def nacho_select(object_name, method, choices = nil, options = {}, html_options = {})
|
28
|
+
nacho_options = build_options(method, choices, options, html_options)
|
29
|
+
select_element = select(object_name, method, nacho_options[:choices], nacho_options[:options], nacho_options[:html_options])
|
30
|
+
select_element += nacho_options[:button] if nacho_options[:html_options][:multiple]
|
31
|
+
|
32
|
+
select_element
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class ActionView::Helpers::FormBuilder
|
39
|
+
|
40
|
+
# The helper for a FormBuilder class. Will create the select and the needed modal that contains the given
|
41
|
+
# form for the target model to create.
|
42
|
+
#
|
43
|
+
# A multiple select will generate a button to trigger the modal.
|
44
|
+
#
|
45
|
+
# @param [Symbol] method See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select.
|
46
|
+
# @param [Array] choices The list of choices for the select, shoudl be in [[val, display],...] format
|
47
|
+
# @param [Hash] options the options to create a message with.
|
48
|
+
# @option options [Boolean] :include_blank Include a blank option (Forced to <tt>true</tt> when <tt>choices.count</tt> == 0)
|
49
|
+
# @option options [String] :new_option_text Text to display as the <tt>option</tt> that will trigger the new modal (Default "-- Add new --", will be ignored if <tt>html_options[:multiple]</tt> is set to <tt>true</tt>)
|
50
|
+
# @option options [Symbol] :value_key The attribute of the model that will be used as the <tt>option</tt> value from the JSON return when a new record is created
|
51
|
+
# @option options [Symbol] :text_key The attribute of the model that will be used as the <tt>option</tt> display content from the JSON return when a new record is created
|
52
|
+
# @option options [Symbol] :new_key The JSON key that will contain the value of the record that was created with the modal
|
53
|
+
# @option options [String] :modal_title The title of the modal (Default to "Add new <model.class.name>")
|
54
|
+
# @option options [String] :partial The form partial for the modal body
|
55
|
+
# @param [Hash] html_options See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select.
|
56
|
+
def nacho_select(method, choices = nil, options = {}, html_options = {})
|
57
|
+
@template.nacho_select(@object_name, method, choices, objectify_options(options), objectify_options(html_options))
|
58
|
+
end
|
59
|
+
end
|
data/lib/nacho/helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'common'
|
2
|
+
|
3
|
+
module Nacho
|
4
|
+
module Helper
|
5
|
+
include Common
|
6
|
+
|
7
|
+
# A tag helper version for a FormBuilder class. Will create the select and the needed modal that contains the given
|
8
|
+
# form for the target model to create.
|
9
|
+
#
|
10
|
+
# A multiple select will generate a button to trigger the modal.
|
11
|
+
#
|
12
|
+
# @param [Symbol] method See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select.
|
13
|
+
# @param [Array] choices The list of choices for the select, shoudl be in [[val, display],...] format
|
14
|
+
# @param [Hash] options the options to create a message with.
|
15
|
+
# @option options [Boolean] :include_blank Include a blank option (Forced to <tt>true</tt> when <tt>choices.count</tt> == 0)
|
16
|
+
# @option options [String] :new_option_text Text to display as the <tt>option</tt> that will trigger the new modal (Default "-- Add new --", will be ignored if <tt>html_options[:multiple]</tt> is set to <tt>true</tt>)
|
17
|
+
# @option options [Symbol] :value_key The attribute of the model that will be used as the <tt>option</tt> value from the JSON return when a new record is created
|
18
|
+
# @option options [Symbol] :text_key The attribute of the model that will be used as the <tt>option</tt> display content from the JSON return when a new record is created
|
19
|
+
# @option options [Symbol] :new_key The JSON key that will contain the value of the record that was created with the modal
|
20
|
+
# @option options [String] :modal_title The title of the modal (Default to "Add new <model.class.name>")
|
21
|
+
# @option options [String] :partial The form partial for the modal body
|
22
|
+
# @param [Hash] html_options See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select.
|
23
|
+
def nacho_select_tag(name, choices = nil, options = {}, html_options = {})
|
24
|
+
nacho_options = build_options(name, choices, options, html_options)
|
25
|
+
select_element = select_tag(name, options_for_select(nacho_options[:choices]), nacho_options[:options], nacho_options[:html_options])
|
26
|
+
select_element += nacho_options[:button] if nacho_options[:html_options][:multiple]
|
27
|
+
|
28
|
+
select_element
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|