-
Notifications
You must be signed in to change notification settings - Fork 12
Add support for the input tag
#4
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,10 +15,12 @@ module DynamicForm | |||||||||
| # has an attribute +title+ mapped to a +VARCHAR+ column that holds "Hello World": | ||||||||||
| # | ||||||||||
| # input("post", "title") | ||||||||||
| # # => <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) | ||||||||||
| # # => <input id="post_title" name="post[title]" size="30" maxlength="30" type="text" value="Hello World" /> | ||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("post", "title", "maxlength" => 10) | ||||||||||
| # # => <input id="post_title" name="post[title]" size="10" maxlength="10" type="text" value="Hello World" /> | ||||||||||
| def input(object_name, method, options = {}) | ||||||||||
| InputBuilder.new(object_name, method, self, options).to_tag | ||||||||||
| end | ||||||||||
|
|
||||||||||
| # Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt> | ||||||||||
|
|
@@ -262,29 +264,55 @@ def default_input_block | |||||||||
| Proc.new { |record, column| %(<p><label for="#{record}_#{column.name}">#{column.human_name}</label><br />#{input(record, column.name)}</p>) } | ||||||||||
| end | ||||||||||
|
|
||||||||||
| module InstanceTagMethods | ||||||||||
| def to_tag(options = {}) | ||||||||||
|
|
||||||||||
| module InputBuilderMethods | ||||||||||
| DEFAULT_MAXLENGTH = 30 | ||||||||||
|
|
||||||||||
| def initialize(object_name, method, context, options) | ||||||||||
| @object_name = object_name | ||||||||||
| @context = context | ||||||||||
| @method = method | ||||||||||
| @options = options | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def to_tag | ||||||||||
| case column_type | ||||||||||
| when :string | ||||||||||
| field_type = @method_name.include?("password") ? "password" : "text" | ||||||||||
| to_input_field_tag(field_type, options) | ||||||||||
| when :text | ||||||||||
| to_text_area_tag(options) | ||||||||||
| when :integer, :float, :decimal | ||||||||||
| to_input_field_tag("text", options) | ||||||||||
| when :date | ||||||||||
| to_date_select_tag(options) | ||||||||||
| when :datetime, :timestamp | ||||||||||
| to_datetime_select_tag(options) | ||||||||||
| when :time | ||||||||||
| to_time_select_tag(options) | ||||||||||
| when :boolean | ||||||||||
| to_boolean_select_tag(options).html_safe | ||||||||||
| when :string | ||||||||||
| @options["type"] = @method.include?("password") ? "password" : "text" | ||||||||||
| Tags::TextField.new(*generic_args).render | ||||||||||
| when :text | ||||||||||
| Tags::TextArea.new(*generic_args).render | ||||||||||
| when :integer, :float, :decimal | ||||||||||
| Tags::TextField.new(*generic_args).render | ||||||||||
| when :date | ||||||||||
| Tags::DateField.new(*generic_args).render | ||||||||||
| when :datetime, :timestamp | ||||||||||
| Tags::DatetimeLocalField.new(*generic_args).render | ||||||||||
| when :time | ||||||||||
| Tags::TimeField.new(*generic_args).render | ||||||||||
| when :boolean | ||||||||||
| Tags::CheckBox.new(@object_name, @method, @context, "1", "0", @options).render | ||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| private | ||||||||||
|
|
||||||||||
| def options_with_default | ||||||||||
| @options.tap do |options| | ||||||||||
| options["maxlength"] = DEFAULT_MAXLENGTH unless @options.key?("maxlength") | ||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||||||||||
| end | ||||||||||
|
Comment on lines
+301
to
303
|
||||||||||
| @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 } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
InstanceTagand relied on methods such asto_input_field_tagthat are no longer available. Instead I'm usingTags::TextFieldor similars.