The human logic is:
This is then used to tell readers on the front end (with pretty 'original updated' or 'updates added' labels) about if or how the original news story has been updated since it was first published, which is important if you're really interested in a story.
So… 1) The following logic works but the top half seems very messy. How do you include three possibilities in one == array?
<% if @articleupdates.blank? && @article.status == "published" %>
<% elsif @articleupdates.blank? && @article.status == "editing" %>
<% elsif @articleupdates.blank? && @article.status == "draft" %>
<% elsif @articleupdates.blank? && @article.status == 'updated' %>
<div class="alert-update"><span class="updated-label">ORIGINAL UPDATED</span></div>
<% else %>
<div class="alert-update"><span class="updated-label">UPDATES ADDED</span></div>
<% end %>
And… 2) while the above works, is there any simple way of doing this…
IF original-article updated AND additional-updates not added, print 'original updated'
ELSIF original-article not updated AND additional-updates added, print 'updates added'
ELSIF original-article updated AND additional-updates added, print both 'original updated' AND 'updates added'
ELSE don't print anything
Or maybe:
IF original-article updated, print 'original updated'
ELSIF additional-updates added, print 'updates added'
ELSIF original-article updated AND additional-updates added, print both 'original updated' AND 'updates added'
ELSE don't print anything
?? I think the missing bit is: what's the opposite of @articleupdates.blank?
?
UPDATE following kj's quick answer below. The simpler version almost works now:
<% if @articleupdates.present? %>
UPDATES ADDED
<% elsif @article.status == "updated" %>
ORIGINAL UPDATED
<% elsif @articleupdates.present? && @article.status == "updated" %>
UPDATES ADDED + ORIGINAL UPDATED
<% else %>
<% end %>
The third part doesn't work for some reason: <% elsif @articleupdates.present? && @article.status == "updated" %>
<% elsif @articleupdates.present? && @article.status == "updated" %>
You would be catching on status == "updated"
before you ever got to your third condition. Move it up to the top as it is the most complex conditional.
<% if @articleupdates.present? && @article.status == "updated" %>
UPDATES ADDED + ORIGINAL UPDATED
<% elsif @articleupdates.present? %>
UPDATES ADDED
<% elsif @article.status == "updated" %>
ORIGINAL UPDATED
<% end %>
EDIT
# Article model
def updated?
status == 'updated'
end
# article helper
def article_label(article)
if article.newsitems.present? && article.updated?
label = "UPDATES ADDED + ORIGINAL UPDATED"
elsif article.newsitems.present?
label = "UPDATES ADDED"
elsif article.updated?
label = "ORIGINAL UPDATED"
end
content_tag :div, content_tag(:span, label, class: 'updated-label'), class: 'alert-update'
end
A decorator would work too I guess... :)
Your code contains the following anti patterns:
@articleupdates.blank?
(DRY) First create methods in your model:
def published?
status == "published"
end
#etc...
Then I recommend you create a presenter using a gem like draper
class ArticleDecorator < Draper::Decorator
delegate_all
def publication_status
if newsitems.present?
if updated?
"UPDATES ADDED + ORIGINAL UPDATED"
else
"UPDATES ADDED"
end
elsif updated?
"ORIGINAL UPDATED"
else
""
end
end
end
And in you controller:
@article = @article.decorate
Finally in your view:
<div class="alert-update"><span class="updated-label"><%= @article.publication_status %></span></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.