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

Changing how inputs with Array attributes are generated. #1523

Merged
merged 1 commit into from Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions spec/lucky_avram/input_helpers_spec.cr
@@ -0,0 +1,31 @@
require "../spec_helper"
include ContextHelper

private class TestPage
include Lucky::HTMLPage

def render
end
end

describe Lucky::InputHelpers do
it "generates the proper name for array attributes" do
view { |page|
page.text_input(array_attribute)
page.text_input(array_attribute)
page.text_input(array_attribute)
}.should contain <<-HTML
<input type="text" id="key_group_0" name="key:group[]" value="one"><input type="text" id="key_group_1" name="key:group[]" value="two"><input type="text" id="key_group_2" name="key:group[]" value="">
HTML
end
end

private def array_attribute
Avram::PermittedAttribute.new(name: :group, param: nil, value: ["one", "two"], param_key: "key")
end

private def view
TestPage.new(build_context).tap do |page|
yield page
end.view.to_s
end
31 changes: 30 additions & 1 deletion src/lucky_avram/ext/lucky/input_helpers.cr
Expand Up @@ -208,16 +208,45 @@ module Lucky::InputHelpers
"type" => type,
"id" => input_id(field),
"name" => input_name(field),
"value" => field.param.to_s,
"value" => input_value(field),
}.merge(input_overrides)
update_array_id_counter!(field)
input attrs, merge_options(html_options, input_options)
end

private property array_id_counter : Hash(Symbol, Int32) do
Hash(Symbol, Int32).new { |h, k| h[k] = 0 }
end

private def update_array_id_counter!(field) : Nil
nil
end

private def update_array_id_counter!(field : Avram::PermittedAttribute(Array)) : Nil
array_id_counter[field.name] += 1
end

private def input_value(field) : String
field.param.to_s
end

private def input_value(field : Avram::PermittedAttribute(Array)) : String
field.value.try(&.[array_id_counter[field.name]]?).to_s
end

private def input_name(field)
"#{field.param_key}:#{field.name}"
end

private def input_name(field : Avram::PermittedAttribute(Array))
"#{field.param_key}:#{field.name}[]"
end

private def input_id(field)
"#{field.param_key}_#{field.name}"
end

private def input_id(field : Avram::PermittedAttribute(Array))
"#{field.param_key}_#{field.name}_#{array_id_counter[field.name]}"
end
end
4 changes: 4 additions & 0 deletions src/lucky_avram/ext/lucky/select_helpers.cr
Expand Up @@ -25,4 +25,8 @@ module Lucky::SelectHelpers
private def input_name(field)
"#{field.param_key}:#{field.name}"
end

private def input_name(field : Avram::PermittedAttribute(Array))
"#{field.param_key}:#{field.name}[]"
end
end