-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Article and Comment with Visible concern
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
1 parent
f173bdf
commit ee778f6
Showing
9 changed files
with
48 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters