-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add support for the input
tag
#4
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" /> | ||
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 | ||
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 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 |
||
end | ||
end | ||
|
||
private | ||
|
||
def options_with_default | ||
@options.tap do |options| | ||
options["maxlength"] = DEFAULT_MAXLENGTH unless @options.key?("maxlength") | ||
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 | ||
end | ||
|
||
def generic_args | ||
[ @object_name, @method, @context, options_with_default ] | ||
end | ||
|
||
def object | ||
@context.instance_variable_get("@#{@object_name}") | ||
end | ||
|
||
def column_type | ||
object.send(:column_for_attribute, @method_name).type | ||
object.send(:column_for_attribute, @method).type | ||
end | ||
end | ||
|
||
|
@@ -299,8 +327,8 @@ def error_messages(options = {}) | |
end | ||
end | ||
|
||
class InstanceTag | ||
include DynamicForm::InstanceTagMethods | ||
class InputBuilder | ||
include DynamicForm::InputBuilderMethods | ||
end | ||
|
||
class FormBuilder | ||
|
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
InstanceTag
and relied on methods such asto_input_field_tag
that are no longer available. Instead I'm usingTags::TextField
or similars.