-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
We plan to make it required in the future but it is optional now to make easier to implement backwards compatibility
Only users that defines custom inputs will see this deprecation.
This make easier to share the same behavior between all the components
@@ -1,7 +1,7 @@ | |||
module SimpleForm | |||
module Components | |||
module Errors | |||
def error | |||
def error(context=nil) |
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 argument=values
was the guideline in Simple Form for a while but I prefer to change it now since it is not consistent anymore
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.
❤️
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.
I'd like spaces here too
❤️ 💚 💙 💛 💜 |
@@ -0,0 +1,28 @@ | |||
module SimpleForm | |||
module Wrappers | |||
class Leaf |
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.
It took me a while to figure out that the context
object that will be passed to the components was an instance of this class - dunno if matching names or something like that could make this more straightforward.
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.
I wonder if it would be best to simply pass the options?
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.
I thought that too. I was more thinking about the future if we need to pass more things, but I think we will not need it in the near feature.
There are some docs here that need to be updated: https://github.com/plataformatec/simple_form/blob/rm-wrapper-classes/lib/simple_form/wrappers/many.rb#L3-L4 I saw them when checking if we could rename |
module WrapperOptions | ||
private | ||
|
||
def merge_wrapper_options(options, context) |
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.
dunno if we need a module just for this method. Pushing it to SimpleForm::Inputs::Base
is an option?
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.
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.
Helpers are related to HTML attributes. merge_wrapper_options is not related to HTML
Right now we only care about the options so lets make the code simpler
@josevalim |
DEPRECATION WARNING: input method now accepts a `wrapper_options` argument. The method definition without the argument is deprecated and will be removed in the next Simple Form version. Change your code from: def input to def input(wrapper_options) See heartcombo/simple_form#997 for more information.
See heartcombo/simple_form#997 for an explanation.
Since heartcombo/simple_form#997, custom input adapters must accept a hash of wrapper options
Since heartcombo/simple_form#997, custom input adapters must accept a hash of wrapper options
Since heartcombo/simple_form#997, custom input adapters must accept a hash of wrapper options
Quiets a deprecation notice from heartcombo/simple_form#997
DEPRECATION WARNING: input method now accepts a `wrapper_options` argument. The method definition without the argument is deprecated and will be removed in the next Simple Form version. Change your code from: def input to def input(wrapper_options) See heartcombo/simple_form#997 for more information. (called from block in _app_views_tips_new_html_haml__844955797404246493_70326972009780 at /Users/dimitar/projects/fmi/evans/app/views/tips/new.html.haml:6)
These methods now accept a `wrappers_options` argument. heartcombo/simple_form#997
These methods now accept a `wrappers_options` argument. heartcombo/simple_form#997
These methods now accept a `wrappers_options` argument. heartcombo/simple_form#997
Hello, can anyone help me I'm still learning rails and was trying to create a input to accept datepicker-bootstrap, I found this tutorial https://github.com/plataformatec/simple_form/wiki/Custom-inputs-examples, I createad as it was declared and worked for a form but when I load another don't, in my terminal I found the reason, that must be this issue and this deprecated, can you help me about this broblem? |
(This is a alternative solution to #622)
Motivation
To integrate Simple Form with Bootstrap 3 we need to make possible to define classes to input only in some wrappers.
We added
input_class
some time ago but that broke some components like checkboxes (see heartcombo/simple_form-bootstrap#28).Instead of changing a global class to be applicated in every input we allow users to choose in which wrappers they want to define a class.
How to use
Now users can define attributes for the components in the component definition of the Wrappers API.
The
class: 'form-label'
option will be used every time a label component is rendered.Implementation
To make this possible I had to change who the Wrappers API is defined and how components are rendered.
Wrapper API storage change
Before the work on this branch, the API components were stored in a mix of Simple Form classes and symbols.
This added some conditionals in our rendering code and made harder to store state in our components. This create a lot of code in Simple Form that rely in the fact that Ruby hashes are mutables and we have a global state. The implementation in #622 had this problem were the changes in a global hash make the hint and errors classes to getting duplicated.
I choose to avoid completely this problem storing the attributes in a class and making it implement the same protocol as
SimpleForm::Wrappers::Many
andSimpleForm::Wrapper::Single
. This also make possible to remove some conditionals to deal with Symbols in the components tree.Input rendering
Before this work, when rendering the input, there was no way to pass information about the context where it is being rendered. I changed the implementation to always pass the wrapper options for the component rendering method itself.
To understand better lets look how a custom input was implemented before:
So when rendering a wrapper Simple Form will call the method
input
of that class. As you can see we don't have a clean way to get information about the wrapper that is being rendered inside that method.Now, Simple Form will pass an argument
wrapper_options
that hold the options defined in the Wrappers API. So, for our previous example,wrapper_options
will containclass: 'form-input'
.So, our custom input will look like:
Now we can use a new helper
merge_wrapper_options
to merge the options present in theform.input
call and the options defined in the wrapper.Finally, our custom input will look like:
Deprecations
The custom input/custom components methods now have to accept one argument with the wrapper options. The inputs/components defined using the old API will work but with a deprecation message.
These examples are deprecated:
To fix the deprecation message use:
Next steps
Closes #622