Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/assets/javascripts/err_supply-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ ErrSupply Twitter Bootstrap3 Adapter
Copyright (c) 2014 Coroutine LLC
Licensed under the MIT license
*/
jQuery(function($) {
$("form").on("err_supply:loaded", function(event, errors) {
(function() {
var errSupplyPlugin = function(event, errors) {

// define how errors are applied to dom elements
var applyFn = function(error_hash) {
Expand Down Expand Up @@ -38,7 +38,7 @@ jQuery(function($) {
};

// add class
$field.addClass('error');
$field.addClass('error').closest('.input-group').addClass('has-error');

// add popover
$field.popover({
Expand All @@ -63,7 +63,7 @@ jQuery(function($) {
// find all contained elements with a style of error and remove the class.
// this is typically more important for ajax submissions. html submissions
// tend not to have this problem.
$form.find('.error').removeClass('error');
$form.find('.error').removeClass('error').closest('.input-group').removeClass('has-error');

// hide all fields that have been explicitly destroyed. when an html submission
// has errors, the _destroy value renders as true rather than 1, which may or may not
Expand All @@ -82,8 +82,13 @@ jQuery(function($) {
$form.find('.error').filter(':first') :
$form.find(':not(.filter) :input:visible:enabled:first');
$focus_field.focus()

// cancel event
return false;
});
});
};

$.fn.err_supply = function() {
$(this).on("err_supply:loaded", errSupplyPlugin.bind(this));
}
})()

jQuery(function($) {
$("form").err_supply();
});
10 changes: 8 additions & 2 deletions lib/err_supply/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ module ViewHelpers
# triggers a custom event on the associated form element.
#
def err_supply(obj, options={})
id = obj.new_record? ? dom_id(obj) : dom_id(obj, :edit)
form_css_selector = options[:form_css_selector]

if form_css_selector.blank?
id = obj.new_record? ? dom_id(obj) : dom_id(obj, :edit)
form_css_selector = "##{id}"
end

prefix = obj.class.name.underscore.split('/').last
payload = err_supply_hash(obj, options.merge({ :prefix => prefix }))

"$('##{ id }').trigger('err_supply:loaded', #{ payload.to_json });".html_safe
"$('#{ form_css_selector }').trigger('err_supply:loaded', #{ payload.to_json });".html_safe
end

end
Expand Down