Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option for an "id_appendix" #468

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Options:
- **:class**: Additional classes to apply to the best_in_place span. Accepts either a string or Array of strings
- **:value**: Customize the starting value of the inline input (defaults to to the field's value)
- **:id**: The HTML id of the best_in_place span. If not specified one is automatically generated.
- **:id_appendix**: Adds '_value' to the autogenerated id
- **:param**: If you wish to specific the object explicitly use this option.
- **:confirm**: If set to true displays a confirmation message when abandoning changes (pressing the escape key).
- **:skip_blur**: If set to true, blurring the input will not cause changes to be abandoned in textareas.
Expand Down Expand Up @@ -386,8 +387,8 @@ As of now, a total of four helpers are available. There is one for each of the f

These four helpers are listed below:

* **bip_area(model, attr, new_value)**
* **bip_text(model, attr, new_value)**
* **bip_area(model, attr, new_value, options = {})**
* **bip_text(model, attr, new_value, options = {})**
* **bip_bool(model, attr)**
* **bip_select(model, attr, name)**

Expand All @@ -396,6 +397,8 @@ The parameters are defined here (some are method-specific):
* **model**: the model to which this action applies.
* **attr**: the attribute of the model to which this action applies.
* **new_value** (only **bip_area** and **bip_text**): the new value with which to fill the BIP field.
* **options** (only **bip_area** and **bip_text**): Holds information like the id_appendix for id creation

* **name** (only **bip_select**): the name to select from the dropdown selector.

---
Expand Down
5 changes: 3 additions & 2 deletions lib/best_in_place/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def best_in_place(object, field, opts = {})
end

options[:class] = ['best_in_place'] + Array(opts[:class] || opts[:classes])
options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field)
options[:data]['id_appendix'] = opts[:id_appendix].presence
options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field, options[:data]['id_appendix'])

pass_through_html_options(opts, options)

Expand Down Expand Up @@ -85,7 +86,7 @@ def pass_through_html_options(opts, options)
:activator, :cancel_button, :cancel_button_class, :html_attrs, :inner_class, :nil,
:object_name, :ok_button, :ok_button_class, :display_as, :display_with, :path, :value,
:use_confirm, :confirm, :sanitize, :raw, :helper_options, :url, :place_holder, :class,
:as, :param, :container]
:as, :param, :container, :id_appendix]
uknown_keys = opts.keys - known_keys
uknown_keys.each { |key| options[key] = opts[key] }
end
Expand Down
8 changes: 4 additions & 4 deletions lib/best_in_place/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module BestInPlace
module TestHelpers
include ActionView::Helpers::JavaScriptHelper

def bip_area(model, attr, new_value)
id = BestInPlace::Utils.build_best_in_place_id model, attr
def bip_area(model, attr, new_value, options = {})
id = BestInPlace::Utils.build_best_in_place_id model, attr, options[:id_appendix]
find("##{id}").trigger('click')
execute_script <<-JS
$("##{id} form textarea").val('#{escape_javascript new_value.to_s}');
Expand All @@ -12,8 +12,8 @@ def bip_area(model, attr, new_value)
wait_for_ajax
end

def bip_text(model, attr, new_value)
id = BestInPlace::Utils.build_best_in_place_id model, attr
def bip_text(model, attr, new_value, options = {})
id = BestInPlace::Utils.build_best_in_place_id model, attr, options[:id_appendix]
find("##{id}").click
execute_script <<-JS
$("##{id} input[name='#{attr}']").val('#{escape_javascript new_value.to_s}');
Expand Down
3 changes: 2 additions & 1 deletion lib/best_in_place/utils.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module BestInPlace
module Utils #:nodoc:
module_function
def build_best_in_place_id(object, field)
def build_best_in_place_id(object, field, appendix = nil)
case object
when Symbol, String
"best_in_place_#{object}_#{field}"
else
id = "best_in_place_#{object_to_key(object)}"
id << "_#{object.id}" if object.persisted?
id << "_#{field}"
id << "_#{appendix}" if !appendix.nil?
id
end
end
Expand Down