Hi there!

I too find myself frequently needing to set and/or merge conditional 
classes in my Rails views. Common use cases include setting "selected" on a 
list of nav links to indicate the current page, "active" on a tab or 
carousel component, etc. It's a common pattern across almost all of my 
Rails projects. 

Rather than modifying all the individual helper methods which take a :class 
option (e.g. link_to, content_tag), why not create a separate helper method 
which will spit out a finalized string of classes. 

For example, 

link_to("Hello", root_path, class: *class_set(btn: true, selected: 
selected?)*)

This same helper could be used wherever a set of conditional classes were 
needed, and no modification to the method itself would be necessary.

It turns out that there's already a gem which implements this functionality 
called css-class-string <https://github.com/nLight/css-class-string>. You 
can add it to your Gemfile and use it as above (except the helper method 
name is class_string, not class_set).

What does everyone think? 

On Wednesday, November 9, 2016 at 6:26:45 AM UTC-5, Gabriel Sobrinho wrote:
>
> I believe you can open the pull request, after all the core team only says 
> yes or no after seeing the code :/
>
> On 8 Nov 2016, at 22:06, [email protected] <javascript:> wrote:
>
> The question im asking is only about conditions doing of classes when 
> things are true in the hash. My `selected?` etc whatever are just examples. 
> The logic im describing is like gabriels. below.
>
> Op dinsdag 25 oktober 2016 16:08:55 UTC+2 schreef Rafael Mendonça França:
>>
>> 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]> 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].
>>> 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.
>>>
>>
> -- 
> 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
> .
> To unsubscribe from this group and all its topics, send an email to 
> [email protected] <javascript:>.
> To post to this group, send email to [email protected] 
> <javascript:>.
> Visit this group at https://groups.google.com/group/rubyonrails-core.
> For more options, visit 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.

Reply via email to