Skip to content

Commit

Permalink
Make ingredient editor form_field_id unique
Browse files Browse the repository at this point in the history
Before the ID was not unique producing duplicated DOM ids for multiple instances
of the same element type on the same page.

Since we want to be able to access the input via JS
we need to have a unique DOM ID.
  • Loading branch information
tvdeyen committed Aug 10, 2021
1 parent 13f8c22 commit ed79a28
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/decorators/alchemy/ingredient_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ def form_field_name(column = "value")
"element[ingredients_attributes][#{form_field_counter}][#{column}]"
end

# Returns a unique string to be passed to a form field id.
#
# @param column [String] A Ingredient column_name. Default is 'value'
#
def form_field_id(column = "value")
"element_ingredients_attributes_#{form_field_counter}_#{column}"
"element_#{element.id}_ingredient_#{id}_#{column}"
end

# Fixes Rails partial renderer calling to_model on the object
Expand Down
4 changes: 2 additions & 2 deletions spec/decorators/alchemy/ingredient_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@

describe "#form_field_id" do
it "returns a id value for form fields with ingredient as default" do
expect(ingredient_editor.form_field_id).to eq("element_ingredients_attributes_0_value")
expect(ingredient_editor.form_field_id).to eq("element_#{element.id}_ingredient_#{ingredient.id}_value")
end

context "with a value given" do
it "returns a id value for form fields for that column" do
expect(ingredient_editor.form_field_id(:link_title)).to eq("element_ingredients_attributes_0_link_title")
expect(ingredient_editor.form_field_id(:link_title)).to eq("element_#{element.id}_ingredient_#{ingredient.id}_link_title")
end
end
end
Expand Down

0 comments on commit ed79a28

Please sign in to comment.