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

Make possible to use the Wrappers API to define attributes for the components #997

Merged
merged 35 commits into from
Mar 17, 2014
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8268412
Deal only with objets in the Wrapper API
rafaelfranca Mar 10, 2014
7a167ca
Component alwyas respond to render now
rafaelfranca Mar 10, 2014
fa061b6
Allow the component to render itself
rafaelfranca Mar 10, 2014
95da393
Add test case for the feature of applying attributes in the wrapper API
rafaelfranca Mar 10, 2014
dda2006
Store the wrapper API attributes in the leaves
rafaelfranca Mar 10, 2014
645988a
Pass the context when rendering the component
rafaelfranca Mar 10, 2014
6976fba
Options should be always a hash
rafaelfranca Mar 11, 2014
df47a85
Merge the context options with the input options
rafaelfranca Mar 11, 2014
51bdd20
Accept options in label component too
rafaelfranca Mar 11, 2014
93161d7
Test with label_input too
rafaelfranca Mar 11, 2014
d821015
Test data attributes in the DSL
rafaelfranca Mar 11, 2014
44f22ca
Make context optional
rafaelfranca Mar 11, 2014
9e3aae1
Deprecate component methods without the context argument
rafaelfranca Mar 11, 2014
6186512
Extract the deprecation message to avoid duplication
rafaelfranca Mar 11, 2014
ed18620
Remove duplication at the deprecated methods
rafaelfranca Mar 11, 2014
3992cfc
Extract the wrapper options merge behavior to their own module
rafaelfranca Mar 11, 2014
4a13b2f
Move the context check down to the method
rafaelfranca Mar 11, 2014
135465f
Add link to pull requests in the deprecation message
rafaelfranca Mar 11, 2014
e9384fb
Whitespaces
rafaelfranca Mar 11, 2014
58220b2
Define merge_wrapper_options in Base instead in a Helper
rafaelfranca Mar 11, 2014
15f0101
Pass the options hash instead of the Leaf object
rafaelfranca Mar 11, 2014
0fc7ea8
Update Many documentation
rafaelfranca Mar 11, 2014
86d0923
Forgot to send some files :bomb:
rafaelfranca Mar 11, 2014
724dff5
No need to return
rafaelfranca Mar 14, 2014
3d53595
Store the component on the instance
rafaelfranca Mar 14, 2014
58f14c3
Store the wrappers with string keys
rafaelfranca Mar 14, 2014
be5badb
Add deprecation to CHANGELOG
rafaelfranca Mar 14, 2014
2acb2c1
lookup_action doesn't need to be symbols
rafaelfranca Mar 14, 2014
464932c
Consistent use of the Simple Form name
rafaelfranca Mar 14, 2014
44a5dba
wrapper_options argument is required
rafaelfranca Mar 14, 2014
266ade3
:lipstick:
rafaelfranca Mar 14, 2014
2fa4b7a
Rails 2.3 is dead
rafaelfranca Mar 14, 2014
f14fed4
Document the options to components
rafaelfranca Mar 14, 2014
bb2fd3b
Add CHANGELOG entry
rafaelfranca Mar 14, 2014
3f47fd0
Merge remote-tracking branch 'origin/master' into rm-wrapper-classes
rafaelfranca Mar 14, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def self.eager_load!
SimpleForm::Components.eager_load!
end

CUSTOM_INPUT_DEPRECATION_WARN = <<-WARN
%{name} method now accepts a `context` argument. The method definition without the argument is deprecated and will be removed in the next Simple Form version. Change your code from:

def %{name}

to

def %{name}(context)
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding this Pull Request URL on this message could be useful too.

WARN

## CONFIGURATION OPTIONS

# Method used to tidy up errors.
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/errors.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SimpleForm
module Components
module Errors
def error
def error(context=nil)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This argument=values was the guideline in Simple Form for a while but I prefer to change it now since it is not consistent anymore

Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like spaces here too

error_text if has_errors?
end

Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Hints
def hint
def hint(context=nil)
@hint ||= begin
hint = options[:hint]

Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/html5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def initialize(*)
@html5 = false
end

def html5
def html5(context=nil)
@html5 = true
if has_required?
input_html_options[:required] = true
Expand Down
22 changes: 20 additions & 2 deletions lib/simple_form/components/label_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@ module LabelInput
include SimpleForm::Components::Labels
end

def label_input
options[:label] == false ? input : (label + input)
def label_input(context=nil)
if options[:label] == false
deprecated_component(:input ,context)
Copy link
Contributor

Choose a reason for hiding this comment

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

space

else
deprecated_component(:label, context) + deprecated_component(:input, context)
end
end

private

def deprecated_component(namespace ,context)
method = method(namespace)

if method.arity == 0
ActiveSupport::Deprecation.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: namespace })

method.call
else
method.call(context)
end
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/simple_form/components/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def translate_required_mark
end
end

def label
def label(context=nil)
label_options = merge_wrapper_options(label_html_options, context)

if generate_label_for_attribute?
@builder.label(label_target, label_text, label_html_options)
@builder.label(label_target, label_text, label_options)
else
template.label_tag(nil, label_text, label_html_options)
template.label_tag(nil, label_text, label_options)
end
end

Expand All @@ -46,6 +48,7 @@ def label_html_options
if options.key?(:input_html) && options[:input_html].key?(:id)
label_options[:for] = options[:input_html][:id]
end

label_options
end

Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/maxlength.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Maxlength
def maxlength
def maxlength(context=nil)
input_html_options[:maxlength] ||= maximum_length_from_validation || limit
nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/min_max.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SimpleForm
module Components
module MinMax
def min_max
def min_max(context=nil)
if numeric_validator = find_numericality_validator
validator_options = numeric_validator.options
input_html_options[:min] ||= minimum_value(validator_options)
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Pattern
def pattern
def pattern(context=nil)
input_html_options[:pattern] ||= pattern_source
nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/placeholders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Placeholders
def placeholder
def placeholder(context=nil)
input_html_options[:placeholder] ||= placeholder_text
nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/readonly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Readonly
def readonly
def readonly(context=nil)
if readonly_attribute? && !has_readonly?
input_html_options[:readonly] ||= true
input_html_classes << :readonly
Expand Down
3 changes: 2 additions & 1 deletion lib/simple_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def input_field(attribute_name, options={})

input = find_input(attribute_name, options)
wrapper = find_wrapper(input.input_type, options)
components = (wrapper.components & ATTRIBUTE_COMPONENTS) + [:input]
components = (wrapper.components.map(&:namespace) & ATTRIBUTE_COMPONENTS) + [:input]
components = components.map { |component| SimpleForm::Wrappers::Leaf.new(component) }

SimpleForm::Wrappers::Root.new(components, wrapper.options.merge(wrapper: false)).render input
end
Expand Down
11 changes: 6 additions & 5 deletions lib/simple_form/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module SimpleForm
# For instance, disabled cannot be turned on automatically, it requires the
# user to explicitly pass the option disabled: true so it may work.
module Helpers
autoload :Autofocus, 'simple_form/helpers/autofocus'
autoload :Disabled, 'simple_form/helpers/disabled'
autoload :Readonly, 'simple_form/helpers/readonly'
autoload :Required, 'simple_form/helpers/required'
autoload :Validators, 'simple_form/helpers/validators'
autoload :Autofocus, 'simple_form/helpers/autofocus'
autoload :Disabled, 'simple_form/helpers/disabled'
autoload :Readonly, 'simple_form/helpers/readonly'
autoload :Required, 'simple_form/helpers/required'
autoload :Validators, 'simple_form/helpers/validators'
autoload :WrapperOptions, 'simple_form/helpers/wrapper_options'
end
end
19 changes: 19 additions & 0 deletions lib/simple_form/helpers/wrapper_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module SimpleForm
module Helpers
module WrapperOptions
private

def merge_wrapper_options(options, context)
Copy link
Contributor

Choose a reason for hiding this comment

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

dunno if we need a module just for this method. Pushing it to SimpleForm::Inputs::Base is an option?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is, but I don't see problem to extract to a module.

I'm removing because helpers are related to the HTML attributes and this one is not related to any attribute at all.

if context
options.merge(context.options) do |_, oldval, newval|
if Array === oldval
oldval + Array(newval)
end
end
else
options
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Base
include SimpleForm::Helpers::Readonly
include SimpleForm::Helpers::Required
include SimpleForm::Helpers::Validators
include SimpleForm::Helpers::WrapperOptions

include SimpleForm::Components::Errors
include SimpleForm::Components::Hints
Expand Down Expand Up @@ -79,7 +80,7 @@ def initialize(builder, attribute_name, column, input_type, options = {})
end
end

def input
def input(context=nil)
raise NotImplementedError
end

Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/block_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(*args, &block)
@block = block
end

def input
def input(context=nil)
template.capture(&@block)
end
end
Expand Down
25 changes: 15 additions & 10 deletions lib/simple_form/inputs/boolean_input.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
module SimpleForm
module Inputs
class BooleanInput < Base
def input
def input(context=nil)
merged_input_options = merge_wrapper_options(input_html_options, context)

if nested_boolean_style?
build_hidden_field_for_checkbox +
template.label_tag(nil, class: "checkbox") {
build_check_box_without_hidden_field + inline_label
build_check_box_without_hidden_field(merged_input_options) +
inline_label
}
else
build_check_box
build_check_box(unchecked_value, merged_input_options)
end
end

def label_input
def label_input(context=nil)
if options[:label] == false
input
input(context)
elsif nested_boolean_style?
html_options = label_html_options.dup
html_options[:class] ||= []
html_options[:class].push(:checkbox)

merged_input_options = merge_wrapper_options(input_html_options, context)

build_hidden_field_for_checkbox +
@builder.label(label_target, html_options) {
build_check_box_without_hidden_field + label_text
build_check_box_without_hidden_field(merged_input_options) + label_text
}
else
input + label
input(context) + label(context)
end
end

Expand All @@ -35,14 +40,14 @@ def label_input
# reuse the method for nested boolean style, but with no unchecked value,
# which won't generate the hidden checkbox. This is the default functionality
# in Rails > 3.2.1, and is backported in SimpleForm AV helpers.
def build_check_box(unchecked_value = unchecked_value)
def build_check_box(unchecked_value, options)
@builder.check_box(attribute_name, input_html_options, checked_value, unchecked_value)
end

# Build a checkbox without generating the hidden field. See
# #build_hidden_field_for_checkbox for more info.
def build_check_box_without_hidden_field
build_check_box(nil)
def build_check_box_without_hidden_field(options)
build_check_box(nil, options)
end

# Create a hidden field for the current checkbox, so we can simulate Rails
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/collection_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.boolean_collection
end
end

def input
def input(context=nil)
raise NotImplementedError,
"input should be implemented by classes inheriting from CollectionInput"
end
Expand Down
7 changes: 5 additions & 2 deletions lib/simple_form/inputs/collection_radio_buttons_input.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module SimpleForm
module Inputs
class CollectionRadioButtonsInput < CollectionInput
def input
def input(context=nil)
label_method, value_method = detect_collection_methods

merged_input_options = merge_wrapper_options(input_html_options, context)

@builder.send("collection_#{input_type}",
attribute_name, collection, value_method, label_method,
input_options, input_html_options, &collection_block_for_nested_boolean_style
input_options, merged_input_options,
&collection_block_for_nested_boolean_style
)
end

Expand Down
6 changes: 4 additions & 2 deletions lib/simple_form/inputs/collection_select_input.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module SimpleForm
module Inputs
class CollectionSelectInput < CollectionInput
def input
def input(context=nil)
label_method, value_method = detect_collection_methods

merged_input_options = merge_wrapper_options(input_html_options, context)

@builder.collection_select(
attribute_name, collection, value_method, label_method,
input_options, input_html_options
input_options, merged_input_options
)
end
end
Expand Down
8 changes: 5 additions & 3 deletions lib/simple_form/inputs/date_time_input.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module SimpleForm
module Inputs
class DateTimeInput < Base
def input
def input(context=nil)
merged_input_options = merge_wrapper_options(input_html_options, context)

if use_html5_inputs?
@builder.send(:"#{input_type}_field", attribute_name, input_html_options)
@builder.send(:"#{input_type}_field", attribute_name, merged_input_options)
else
@builder.send(:"#{input_type}_select", attribute_name, input_options, input_html_options)
@builder.send(:"#{input_type}_select", attribute_name, input_options, merged_input_options)
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/simple_form/inputs/file_input.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module SimpleForm
module Inputs
class FileInput < Base
def input
@builder.file_field(attribute_name, input_html_options)
def input(context=nil)
merged_input_options = merge_wrapper_options(input_html_options, context)

@builder.file_field(attribute_name, merged_input_options)
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/simple_form/inputs/grouped_collection_select_input.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module SimpleForm
module Inputs
class GroupedCollectionSelectInput < CollectionInput
def input
def input(context=nil)
label_method, value_method = detect_collection_methods

merged_input_options = merge_wrapper_options(input_html_options, context)

@builder.grouped_collection_select(attribute_name, grouped_collection,
group_method, group_label_method, value_method, label_method,
input_options, input_html_options)
input_options, merged_input_options)
end

private
Expand Down
6 changes: 4 additions & 2 deletions lib/simple_form/inputs/hidden_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module Inputs
class HiddenInput < Base
disable :label, :errors, :hint, :required

def input
@builder.hidden_field(attribute_name, input_html_options)
def input(context=nil)
merged_input_options = merge_wrapper_options(input_html_options, context)

@builder.hidden_field(attribute_name, merged_input_options)
end

private
Expand Down
Loading