This shouldn't impact the implementation, it could be literally anything
(helper method, object method, local variable or anything).
The specification would be { classname: true/false }.
link_to user.name, user_path(user), class: { btn: true, active: user.active? }
// => object method call
link_to user.name, user_path(user), class: { btn: true, active:
active_user?(user) } // => helper method call
link_to user.name, user_path(user), class: { btn: true, active: active } // =>
local variable call
Also, the link_to can be in the view or in a helper method.
If you want to see this working, you can implement a prototype on application
helper like this (untested and not fully compatible with current link_to
implementation (need to accepts blocks, string keys, etc)):
module ApplicationHelper
def link_to(content, path, options = {})
klass = if Hash === options[:class]
parse_class_hash(options[:class])
else
options[:class]
end
super(content, path, options.merge(class: klass))
end
def parse_class_hash(classes)
string = []
classes.each_pair do |name, value|
string << name if value
end
string.join(' ')
end
end
> On 25 Oct 2016, at 12:08, Rafael Mendonça França <[email protected]>
> wrote:
>
> There is an open question here. From here those conditional methods are
> coming from? From helpers modules? This will add a lot of this kind of
> methods in the global namespace since helpers are global and will make code
> harder to understand.
>
> On Mon, Oct 24, 2016 at 10:12 PM <[email protected]
> <mailto:[email protected]>> wrote:
> Heya,
>
> Setting condition classes is a huge pain in rails at the moment, or maybe i
> am missing it but i can't see to find it. borrowed from react components kind
> of style.
>
> I want something like this (the class selected is only added if the value is
> true)
>
> link_to("Homepage", root_path, class: {selected: selected?})
>
> -> a href class="selected" and other
>
> You can do the same thing to add multiple classes too
>
> link_to("Hello", root_path, class: {class: {btn:true, selected: selected?}}
>
> I think this would be awesome, anything reasons why we shouldn't implement
> this?
>
> Cheers,
>
> - Emile
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/rubyonrails-core
> <https://groups.google.com/group/rubyonrails-core>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to a topic in the Google
> Groups "Ruby on Rails: Core" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/rubyonrails-core/GtAQDk0SCA0/unsubscribe
> <https://groups.google.com/d/topic/rubyonrails-core/GtAQDk0SCA0/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to
> [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/rubyonrails-core
> <https://groups.google.com/group/rubyonrails-core>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.