I'm giving Draper a try as an alternative to helpers. I get the cases where I am just formatting the information. But what about interacting with the Rails form builder. For example if I wanted to output a string or a select box depending on some context. Do I pass the form builder as an argument. So in my decorator:
def role_or_select form
available_roles = h.policy_scope User::ROLES
if available_roles.include? role
form.input :role, collection: available_roles, include_blank: false
else
role
end
end
Then in my view:
= simple_form_for user do |form|
...
= user.role_or_select_on form
...
Is there a more elegant method?
I think I found a somewhat elegant solution to this. I created the following module and mixed it into my decorators:
Now my view can be this:
The
input
method is proxied off to the form builder. The role_or_select is defined in my helper to look something like this:This even allows me to do things like add the form options in the decorator. For example adding the following to my UserDecorator will turn off the auto-complete: