From 8ee3d0d137600a6ddaccae114ebc06c71afb7457 Mon Sep 17 00:00:00 2001 From: Chris Howlett Date: Wed, 16 Mar 2016 13:30:10 +0000 Subject: [PATCH] Allow best_in_place field to specify more: * HTTP method to submit form by * extra data fields to submit with form --- README.md | 6 ++++-- lib/assets/javascripts/best_in_place.js | 10 ++++++++-- lib/best_in_place/helper.rb | 3 ++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d0116bed..300b0b3f 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ Options: - **:as** It can be only [:input, :textarea, :select, :checkbox, :date] or if undefined it defaults to :input. - **:collection**: If you are using the :select type then you must specify the collection of values it takes as a hash where values represent the display text and keys are the option's value when selected. If you are using the :checkbox type you can specify the two values it can take, or otherwise they will default to Yes and No. - **:url**: URL to which the updating action will be sent. If not defined it defaults to the :object path. +- **:method**: HTTP method used to submit the updating action. If not defined it defaults to :put. +- **:other_fields**: Hash of additional data fields to submit along with the updating action. - **:place_holder**: The nil param defines the content displayed in case no value is defined for that field. It can be something like "click me to edit". If not defined it will show *"-"*. - **:activator**: Is the DOM object that can activate the field. If not defined the user will making editable by clicking on it. @@ -166,7 +168,7 @@ The value will always be converted to a string for display. If you use array as a collection, the first value is always the negative boolean value and the second the positive. Structure: `["false value", "true value"]`. If not defined, it will default to *Yes* and *No* options. Default true and false values are stored in locales - + t(:'best_in_place.yes', default: 'Yes') t(:'best_in_place.no', default: 'No') @@ -340,7 +342,7 @@ You can configure some global options for best_in_place. Currently these options config.container = :div config.skip_blur = true end - + ## Notification diff --git a/lib/assets/javascripts/best_in_place.js b/lib/assets/javascripts/best_in_place.js index db3f8bde..b7f54ee9 100644 --- a/lib/assets/javascripts/best_in_place.js +++ b/lib/assets/javascripts/best_in_place.js @@ -103,7 +103,7 @@ BestInPlaceEditor.prototype = { } editor.ajax({ - "type": BestInPlaceEditor.defaults.ajaxMethod, + "type": editor.ajaxMethod, "dataType": BestInPlaceEditor.defaults.ajaxDataType, "data": editor.requestData(), "success": function (data, status, xhr) { @@ -189,6 +189,8 @@ BestInPlaceEditor.prototype = { self.cancelButton = self.element.data("bipCancelButton") || self.cancelButton; self.cancelButtonClass = self.element.data("bipCancelButtonClass") || self.cancelButtonClass || BestInPlaceEditor.defaults.cancelButtonClass; self.skipBlur = self.element.data("bipSkipBlur") || self.skipBlur || BestInPlaceEditor.defaults.skipBlur; + self.ajaxMethod = self.element.data("bipMethod") || self.ajaxMethod || BestInPlaceEditor.defaults.ajaxMethod; + self.otherFields = self.element.data("bipOtherFields") || self.otherFields || null; // Fix for default values of 0 if (self.element.data("bipPlaceholder") == null) { @@ -253,9 +255,13 @@ BestInPlaceEditor.prototype = { var csrf_token = jQuery('meta[name=csrf-token]').attr('content'), csrf_param = jQuery('meta[name=csrf-param]').attr('content'); - var data = "_method=" + BestInPlaceEditor.defaults.ajaxMethod; + var data = "_method=" + this.ajaxMethod; data += "&" + this.objectName + '[' + this.attributeName + ']=' + encodeURIComponent(this.getValue()); + if (this.otherFields !== undefined && this.otherFields !== null) { + data += "&" + this.otherFields; + } + if (csrf_param !== undefined && csrf_token !== undefined) { data += "&" + csrf_param + "=" + encodeURIComponent(csrf_token); } diff --git a/lib/best_in_place/helper.rb b/lib/best_in_place/helper.rb index 74d5e6e7..dfadfd3a 100644 --- a/lib/best_in_place/helper.rb +++ b/lib/best_in_place/helper.rb @@ -31,7 +31,6 @@ def best_in_place(object, field, opts = {}) options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field) pass_through_html_options(opts, options) - options[:data]['bip-activator'] = opts[:activator].presence options[:data]['bip-html-attrs'] = opts[:html_attrs].to_json unless opts[:html_attrs].blank? @@ -49,9 +48,11 @@ def best_in_place(object, field, opts = {}) options[:data]['bip-skip-blur'] = opts.has_key?(:skip_blur) ? opts[:skip_blur].presence : BestInPlace.skip_blur options[:data]['bip-url'] = url_for(opts[:url] || object) + options[:data]['bip-method'] = opts[:method].presence options[:data]['bip-confirm'] = opts[:confirm].presence options[:data]['bip-value'] = html_escape(value).presence + options[:data]['bip-other-fields'] = opts[:other_fields].to_query unless opts[:other_fields].blank? if opts[:raw] options[:data]['bip-raw'] = 'true'