views:

33

answers:

3

If post.published?

.post
  / Post stuff

Otherwise

.post.gray
  / Post stuff

I've implemented this with rails helper and it seems ugly.

= content_tag :div, :class => "post" + (" gray" unless post.published?).to_s do
  / Post stuff

Second variant:

= content_tag :div, :class => "post" + (post.published? ? "" : " gray") do
  / Post stuff

Is there a more simple and haml-specified way?

UPD. Haml-specified, but still not simple:

%div{:class => "post" + (" gray" unless post.published?).to_s}
  / Post stuff
+1  A: 

Really the best thing is to put it into a helper.

%div{ :class => published_class(post) }

#some_helper.rb

def published_class(post)
  "post #{post.published? ? '' : 'gray'}"
end
mark
I've put this in my helper file, but my app tells me, that there is no "post" variable.
Semyon Perepelitsa
Oops sorry. Have edited answer.
mark
+1  A: 
= content_tag :div, :class => ["post", ("gray" unless post.published?)].compact.join(" ") do
  /Post stuff

def post_tag post, &block
  classes = ["post", ("gray" unless post.published?)]
  content_tag :div, :class => classes.compact.join(" "), &block
end

= post_tag post
  /Post stuff
Justice
Not so concise, but looks better than other ways if put into a helper.
Semyon Perepelitsa
+2  A: 
.post{:class => ("gray" unless post.published?)}
Nathan Weizenbaum