1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Make context optional

We plan to make it required in the future but it is optional now to make
easier to implement backwards compatibility
This commit is contained in:
Rafael Mendonça França 2014-03-11 15:22:45 -03:00
parent d821015813
commit 44f22ca3fb
24 changed files with 116 additions and 43 deletions

View file

@ -1,7 +1,7 @@
module SimpleForm
module Components
module Errors
def error(context)
def error(context=nil)
error_text if has_errors?
end

View file

@ -5,7 +5,7 @@ module SimpleForm
@html5 = false
end
def html5(context)
def html5(context=nil)
@html5 = true
if has_required?
input_html_options[:required] = true

View file

@ -7,7 +7,7 @@ module SimpleForm
include SimpleForm::Components::Labels
end
def label_input(context)
def label_input(context=nil)
options[:label] == false ? input(context) : (label(context) + input(context))
end
end

View file

@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Maxlength
def maxlength(context)
def maxlength(context=nil)
input_html_options[:maxlength] ||= maximum_length_from_validation || limit
nil
end

View file

@ -1,7 +1,7 @@
module SimpleForm
module Components
module MinMax
def min_max(context)
def min_max(context=nil)
if numeric_validator = find_numericality_validator
validator_options = numeric_validator.options
input_html_options[:min] ||= minimum_value(validator_options)

View file

@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Pattern
def pattern(context)
def pattern(context=nil)
input_html_options[:pattern] ||= pattern_source
nil
end

View file

@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Placeholders
def placeholder(context)
def placeholder(context=nil)
input_html_options[:placeholder] ||= placeholder_text
nil
end

View file

@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Readonly
def readonly(context)
def readonly(context=nil)
if readonly_attribute? && !has_readonly?
input_html_options[:readonly] ||= true
input_html_classes << :readonly

View file

@ -79,7 +79,7 @@ module SimpleForm
end
end
def input(context)
def input(context=nil)
raise NotImplementedError
end

View file

@ -6,7 +6,7 @@ module SimpleForm
@block = block
end
def input(context)
def input(context=nil)
template.capture(&@block)
end
end

View file

@ -1,19 +1,25 @@
module SimpleForm
module Inputs
class BooleanInput < Base
def input(context)
def input(context=nil)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
if nested_boolean_style?
build_hidden_field_for_checkbox +
template.label_tag(nil, class: "checkbox") {
build_check_box_without_hidden_field(merged_input_options(context.options)) +
build_check_box_without_hidden_field(merged_input_options) +
inline_label
}
else
build_check_box(unchecked_value, merged_input_options(context.options))
build_check_box(unchecked_value, merged_input_options)
end
end
def label_input(context)
def label_input(context=nil)
if options[:label] == false
input(context)
elsif nested_boolean_style?
@ -21,9 +27,15 @@ module SimpleForm
html_options[:class] ||= []
html_options[:class].push(:checkbox)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
build_hidden_field_for_checkbox +
@builder.label(label_target, html_options) {
build_check_box_without_hidden_field(merged_input_options(context.options)) + label_text
build_check_box_without_hidden_field(merged_input_options) + label_text
}
else
input(context) + label(context)

View file

@ -12,7 +12,7 @@ module SimpleForm
end
end
def input(context)
def input(context=nil)
raise NotImplementedError,
"input should be implemented by classes inheriting from CollectionInput"
end

View file

@ -1,12 +1,18 @@
module SimpleForm
module Inputs
class CollectionRadioButtonsInput < CollectionInput
def input(context)
def input(context=nil)
label_method, value_method = detect_collection_methods
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.send("collection_#{input_type}",
attribute_name, collection, value_method, label_method,
input_options, merged_input_options(context.options),
input_options, merged_input_options,
&collection_block_for_nested_boolean_style
)
end

View file

@ -1,12 +1,18 @@
module SimpleForm
module Inputs
class CollectionSelectInput < CollectionInput
def input(context)
def input(context=nil)
label_method, value_method = detect_collection_methods
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.collection_select(
attribute_name, collection, value_method, label_method,
input_options, merged_input_options(context.options)
input_options, merged_input_options
)
end
end

View file

@ -1,11 +1,17 @@
module SimpleForm
module Inputs
class DateTimeInput < Base
def input(context)
if use_html5_inputs?
@builder.send(:"#{input_type}_field", attribute_name, merged_input_options(context.options))
def input(context=nil)
if context
merged_input_options = merged_input_options(context.options)
else
@builder.send(:"#{input_type}_select", attribute_name, input_options, merged_input_options(context.options))
merged_input_options = input_html_options
end
if use_html5_inputs?
@builder.send(:"#{input_type}_field", attribute_name, merged_input_options)
else
@builder.send(:"#{input_type}_select", attribute_name, input_options, merged_input_options)
end
end

View file

@ -1,8 +1,14 @@
module SimpleForm
module Inputs
class FileInput < Base
def input(context)
@builder.file_field(attribute_name, merged_input_options(context.options))
def input(context=nil)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.file_field(attribute_name, merged_input_options)
end
end
end

View file

@ -1,11 +1,18 @@
module SimpleForm
module Inputs
class GroupedCollectionSelectInput < CollectionInput
def input(context)
def input(context=nil)
label_method, value_method = detect_collection_methods
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.grouped_collection_select(attribute_name, grouped_collection,
group_method, group_label_method, value_method, label_method,
input_options, merged_input_options(context.options))
input_options, merged_input_options)
end
private

View file

@ -3,8 +3,14 @@ module SimpleForm
class HiddenInput < Base
disable :label, :errors, :hint, :required
def input(context)
@builder.hidden_field(attribute_name, merged_input_options(context.options))
def input(context=nil)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.hidden_field(attribute_name, merged_input_options)
end
private

View file

@ -3,19 +3,21 @@ module SimpleForm
class NumericInput < Base
enable :placeholder, :min_max
def input(context)
def input(context=nil)
input_html_classes.unshift("numeric")
if html5?
input_html_options[:type] ||= "number"
input_html_options[:step] ||= integer? ? 1 : "any"
end
merged_input_options = merged_input_options(context.options)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.text_field(attribute_name, merged_input_options)
end
private
end
end
end

View file

@ -3,8 +3,14 @@ module SimpleForm
class PasswordInput < Base
enable :placeholder, :maxlength
def input(context)
@builder.password_field(attribute_name, merged_input_options(context.options))
def input(context=nil)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.password_field(attribute_name, merged_input_options)
end
end
end

View file

@ -1,9 +1,15 @@
module SimpleForm
module Inputs
class PriorityInput < CollectionSelectInput
def input(context)
def input(context=nil)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.send(:"#{input_type}_select", attribute_name, input_priority,
input_options, merged_input_options(context.options))
input_options, merged_input_options)
end
def input_priority

View file

@ -1,7 +1,7 @@
module SimpleForm
module Inputs
class RangeInput < NumericInput
def input(context)
def input(context=nil)
if html5?
input_html_options[:type] ||= "range"
input_html_options[:step] ||= 1

View file

@ -3,13 +3,17 @@ module SimpleForm
class StringInput < Base
enable :placeholder, :maxlength, :pattern
def input(context)
def input(context=nil)
unless string?
input_html_classes.unshift("string")
input_html_options[:type] ||= input_type if html5?
end
merged_input_options = merged_input_options(context.options)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.text_field(attribute_name, merged_input_options)
end

View file

@ -3,8 +3,14 @@ module SimpleForm
class TextInput < Base
enable :placeholder, :maxlength
def input(context)
@builder.text_area(attribute_name, merged_input_options(context.options))
def input(context=nil)
if context
merged_input_options = merged_input_options(context.options)
else
merged_input_options = input_html_options
end
@builder.text_area(attribute_name, merged_input_options)
end
end
end