Малко парче код, което ми се струва достатъчно интересно че да го постна.
development=# \d comments
Table "public.comments"
Column | Type | Modifiers
------------+-----------------------------+--------------------
....
spam | boolean | not null default false
approved | boolean | not null default false
...
Трябва ми име за CSS клас (pending, spam, published), като и текстов низ да покажа на потребителя (Очаква одобрение, Спам, Публикуван). Може би нещо от тоя род?
Ruby
-
module Admin::CommentsHelper
-
def status(comment)
-
if comment.approved?
-
return :published
-
else
-
if comment.spam?
-
return :spam
-
else
-
return :pending
-
end
-
end
-
end
-
-
def status_name(comment)
-
if comment.approved?
-
#…
-
End
-
end
-
end
Не, стана грозно. Я да пробваме по друг начин:
Ruby
-
module Admin::CommentsHelper
-
def status(comment)
-
{
-
[false, false] => :pending,
-
[false, true] => :spam,
-
[true, false] => :published,
-
[true, true] => :published
-
}[[comment.approved?, comment.spam?]]
-
end
-
-
def status_name(comment)
-
{
-
:pending => "Очаква одобрение",
-
:spam => "Спам",
-
:published => "Публикуван"
-
}[status(comment)]
-
end
-
еnd
Секси! Май съм го споменавал?
One Comment
Готино