Skip to content

Conversation

@okcomputer93
Copy link

Add support for the input tag for usage as input("post", "title").

def input(record_name, method, options = {})
raise_broken_code_error
InstanceTag.new(record_name, method, self).to_tag(options)
# # => <input id="post_title" name="post[title]" size="30" maxlength="30" type="text" value="Hello World" />
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find more value in adding the maxlength attribute as well, in addition to the size.

# # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
def input(record_name, method, options = {})
raise_broken_code_error
InstanceTag.new(record_name, method, self).to_tag(options)
Copy link
Author

@okcomputer93 okcomputer93 Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was inspired by the deprecated class InstanceTag and relied on methods such as to_input_field_tag that are no longer available. Instead I'm using Tags::TextField or similars.

@okcomputer93 okcomputer93 marked this pull request as draft April 2, 2024 17:07

def options_with_default
@options.tap do |options|
options["maxlength"] = DEFAULT_MAXLENGTH unless @options.key?("maxlength")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the render methods sets the size attribute based on the maxlength option https://github.com/rails/rails/blob/main/actionview/lib/action_view/helpers/tags/text_field.rb#L11-L18

when :time
Tags::TimeField.new(*generic_args).render
when :boolean
Tags::CheckBox.new(@object_name, @method, @context, "1", "0", @options).render
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, setting the maxlength attribute for a checkbox input doesn't make much sense

when :time
Tags::TimeField.new(*generic_args).render
when :boolean
Tags::CheckBox.new(@object_name, @method, @context, "1", "0", @options).render
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense for this tag to be a checkbox input instead of a select, as previously https://api.rubyonrails.org/v3.1.3/classes/ActionView/Helpers/InstanceTag.html#method-i-to_boolean_select_tag

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@okcomputer93 Although the description was never updated... I don't believe we support Rails 3 anymore... Only Rails 7, 8 and onward.

6 is already dead. A train can only wait for a single most important person.
Nobody can stop a Shinkansen.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am working on a PR to add testing for Rails 8.0. Rails 8.0 only supports Ruby 3.2+ so I think it might be best to cut a major release for that one, and officially drop support for Ruby < 3.2 and Rails < 7.0

@okcomputer93 okcomputer93 marked this pull request as ready for review April 3, 2024 04:12
@mathieujobin mathieujobin requested a review from Copilot June 29, 2025 06:59
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new input helper that supports a wide range of form field types (text, password, integer, date, datetime-local, time, boolean) with a default maxlength of 30, and updates the accompanying tests and model stubs to cover these cases.

  • Replace the old InstanceTag API with a new InputBuilder that dispatches to the appropriate Tags::* component and applies default maxlength.
  • Add and update tests in dynamic_form_test.rb to cover new input types and the maxlength option.
  • Extend Post and User test stubs and their content_columns definitions to include new attributes for date, time, and boolean fields.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test/dynamic_form_test.rb Extended model structs and content_columns; added tests for new field types and maxlength behavior
lib/action_view/helpers/dynamic_form.rb Introduced InputBuilder and InputBuilderMethods; implemented default maxlength logic and type-based rendering
Comments suppressed due to low confidence (2)

lib/action_view/helpers/dynamic_form.rb:18

  • [nitpick] The doc comment for input only shows a text field example; please expand it to document support for other types (date, datetime-local, time, boolean) and the default maxlength behavior so readers understand all the new capabilities.
      #   # => <input id="post_title" name="post[title]" size="30" maxlength="30" type="text" value="Hello World" />

test/dynamic_form_test.rb:196

  • Currently only the true case is tested for boolean inputs; consider adding a test where the attribute is false (or nil) to verify the hidden field and unchecked checkbox rendering.
  def test_input_tag_for_boolean

Comment on lines +301 to 303
@options.tap do |options|
options["maxlength"] = DEFAULT_MAXLENGTH unless @options.key?("maxlength")
end
Copy link

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutating the original @options hash can introduce unexpected side effects when reused; consider duplicating (@options.dup) or merging into a new hash before setting defaults to keep the input builder stateless.

Suggested change
@options.tap do |options|
options["maxlength"] = DEFAULT_MAXLENGTH unless @options.key?("maxlength")
end
@options.merge("maxlength" => DEFAULT_MAXLENGTH) { |key, old_val, new_val| old_val }

Copilot uses AI. Check for mistakes.
@mathieujobin
Copy link

@joe-sharp Care to chime in ? or any other user of this gem ?

@joe-sharp
Copy link

joe-sharp commented Jun 29, 2025

@joe-sharp Care to chime in ? or any other user of this gem ?

I will double-check our codebase when I get back to work after this week, however I am pretty sure we don't use this functionality. So I don't have an opinion on this other than it might be nice to get this in (if it's approved) before adding testing for Rails 8.0; it's probably best we drop old versions of Ruby and Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants