Skip to content

Commit

Permalink
Refactor Article and Comment with Visible concern
Browse files Browse the repository at this point in the history
Refactor out the repeated constant, validation, and instance methods in
the Article and Comment models into a concern module Visible that
extends ActiveSupport::Concern.

Add a new class method called public_count to the Visible module and use
the pluralize helper from ActiveSupport to include the count of public
articles in the Articles index action.

Completes Rails Getting Started Guide:
* 9 Refactoring
  * 9.3 Using Concerns
  • Loading branch information
msducheminjr committed Jan 1, 2022
1 parent f173bdf commit ee778f6
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 19 deletions.
10 changes: 2 additions & 8 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
class Article < ApplicationRecord
include Visible

has_many :comments, dependent: :destroy

validates :title, presence: true
validates :body, presence: true, length: { minimum: 10 }

VALID_STATUSES = %w(public private archived)

validates :status, inclusion: { in: VALID_STATUSES }

def archived?
status == "archived"
end
end
10 changes: 2 additions & 8 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
class Comment < ApplicationRecord
belongs_to :article

VALID_STATUSES = %w(public private archived)
include Visible

validates :status, inclusion: { in: VALID_STATUSES }

def archived?
status == "archived"
end
belongs_to :article
end
19 changes: 19 additions & 0 deletions app/models/concerns/visible.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Visible
extend ActiveSupport::Concern

VALID_STATUSES = %w(public private archived)

included do
validates :status, inclusion: { in: VALID_STATUSES }
end

class_methods do
def public_count
where(status: "public").count
end
end

def archived?
status == "archived"
end
end
5 changes: 5 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<% end %>
</div>

<div>
<%= form.label :status %><br>
<%= form.select :status, %w(public private archived), selected: "public" %>
</div>

<div>
<%= form.submit %>
</div>
Expand Down
4 changes: 4 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<h1>Articles</h1>

<p class="public-count">
Our blog has <%= pluralize(Article.public_count, "article") %> and counting!
</p>

<ul>
<% @articles.each do |article| %>
<% unless article.archived? %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.label :status %><br>
<%= form.select :status, %w(public private archived), selected: "public" %>
</p>
<p>
<%= form.submit %>
</p>
Expand Down
7 changes: 4 additions & 3 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def setup
assert_select "h1", "Articles"
assert_select "li", Article.count - 1
assert_select "a", "New Article"
assert_select "p.public-count", "Our blog has 1 article and counting!"
end

test "should get show" do
Expand All @@ -31,15 +32,15 @@ def setup
assert_select "p.comment-body", @article.comments.count - 1
assert_select "h2", "Add a comment:"
assert_select "form"
assert_select "form p", 3
assert_select "form p", 4
end

test "should get new" do
get new_article_url
assert_response :success
assert_select "h1", "New Article"
assert_select "form"
assert_select "form div", 3
assert_select "form div", 4
end

test "should create article" do
Expand Down Expand Up @@ -76,7 +77,7 @@ def setup
assert_response :success
assert_select "h1", "Edit Article"
assert_select "form"
assert_select "form div", 3
assert_select "form div", 4
end

test "should update article" do
Expand Down
4 changes: 4 additions & 0 deletions test/models/article_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ def setup
@article.status = "private"
assert_not @article.archived?
end

test "public count matches number of public records" do
assert_equal 1, Article.public_count
end
end
4 changes: 4 additions & 0 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ def setup
@comment.status = "private"
assert_not @comment.archived?
end

test "public count matches number of public records" do
assert_equal 2, Comment.public_count
end
end

0 comments on commit ee778f6

Please sign in to comment.