Created
February 12, 2013 07:26
-
-
Save rosylilly/4760786 to your computer and use it in GitHub Desktop.
Answer for http://twitter.com/VoQn/status/301223276636692480 あえてまわりくどい書き方をしています。
This file contains hidden or 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
# 記事クラス | |
class Article < AR::Base | |
belongs_to :user | |
end |
This file contains hidden or 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
class DeleteArticle | |
module Author | |
# このユーザーは削除可能かどうか | |
def can_delete_to?(article) | |
article.written_by?(self) | |
end | |
end | |
module Post | |
# あるユーザーが作者かどうか | |
def written_by?(user) | |
self.user == user | |
end | |
# 削除したユーザーを記録しておくのと、論理削除運用 | |
def delete_by!(user) | |
self.deleted_user = user | |
self.deleted_at = Time.now | |
self.save! | |
end | |
end | |
def initialize(user, article) | |
@author = user.extend(Author) | |
@post = article.extend(Post) | |
end | |
# ユースケースの実行 | |
def execute! | |
return unless @author.can_delete_to?(@post) | |
@post.delete_by!(@author) | |
end | |
end |
This file contains hidden or 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
# ユーザークラス | |
class User < AR::Base | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment