Skip to content

Commit cd979c3

Browse files
committed
Cleaned up setting the :published attribute
* No more Article#is_published. Just use the built-in #published?
1 parent b38aecb commit cd979c3

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

app/controllers/admin/articles.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ def new
1818

1919
def create(article)
2020
@article = Article.new(article)
21-
@article.published = true if @article.published == "1"
2221
@article.user_id = self.current_user.id
2322
if @article.save
2423
# Expire the article index to reflect the newly published article
2524
expire_index if @article.published
2625
render_then_call(redirect(url(:admin_articles))) do
2726
# Call events after the redirect
28-
Hooks::Events.after_publish_article_request(@article, request) if @article.is_published?
27+
Hooks::Events.after_publish_article_request(@article, request) if @article.published?
2928
Hooks::Events.after_create_article_request(@article, request)
3029
end
3130
else
@@ -38,14 +37,13 @@ def edit
3837
end
3938

4039
def update(article)
41-
article["published"] = true if article["published"] == "1"
4240
if @article.update_attributes(article)
4341
# Expire the index and article to reflect the updated article
4442
expire_index
4543
expire_article(@article)
4644
render_then_call(redirect(url(:admin_article, @article))) do
4745
# Call events after the redirect
48-
Hooks::Events.after_publish_article_request(@article, request) if @article.is_published?
46+
Hooks::Events.after_publish_article_request(@article, request) if @article.published?
4947
Hooks::Events.after_update_article_request(@article, request)
5048
end
5149
else

app/models/article.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class Article
3636
# This sets the published date and permalink when an article is published
3737
def set_published_permalink
3838
# Check to see if we are publishing
39-
if self.is_published?
39+
if self.published?
4040
# Set the date, only if we haven't already
41-
self.published_at = Time.now if self.published_at.nil?
41+
self.published_at ||= Time.now
4242

4343
# Set the permalink, only if we haven't already
4444
self.permalink = create_permalink
@@ -68,7 +68,7 @@ def fire_before_update_event
6868

6969
def fire_before_save_event
7070
Hooks::Events.before_save_article(self)
71-
Hooks::Events.before_publish_article(self) if self.is_published?
71+
Hooks::Events.before_publish_article(self) if self.published?
7272
end
7373

7474
def fire_after_create_event
@@ -81,12 +81,12 @@ def fire_after_update_event
8181

8282
def fire_after_save_event
8383
Hooks::Events.after_save_article(self)
84-
Hooks::Events.after_publish_article(self) if self.is_published?
84+
Hooks::Events.after_publish_article(self) if self.published?
8585
end
8686

87-
def is_published?
87+
def published=(binary_string)
8888
# We need this beacuse the values get populated from the params
89-
self.published == "1" || self.published
89+
attribute_set(:published, binary_string == "1")
9090
end
9191

9292
def create_permalink

0 commit comments

Comments
 (0)