htmlcssruby-on-railsrubyhyperstack

Hyperstack add dynamic class to manual class names


I need to add a dynamic class to some regular classes while updating syntax for Hyperstack:

div.upload_header.text_left(class: ('uploaded' if 
FileUploads.complete?)) do

Should become something like this:

DIV(class: 'upload-header text-left (dynamic 'uploaded' should go 
here)') do

I just can't seem to figure out how/if regular and dynamic classes can be declared together.


Solution

  • String interpolation can be done conditionally:

    DIV(class: "upload-header text-left #{'uploaded' if FileUploads.complete?}")
    

    The class parameter can also accept an array:

    def upload_header_classes
      ['upload-header', 'text-left'].tap do |classes|
        classes << 'uploaded' if FileUploads.complete?
      end
    end
    
    DIV(class: upload_header_classes)
    

    I'm sure there are plenty of other ways to do it too, this is ruby!