Last active
December 28, 2015 05:39
-
-
Save bookis/7451033 to your computer and use it in GitHub Desktop.
Supporting class for Canvas Methods quiz
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 Comment | |
attr_accessor :content, :created_at | |
attr_reader :post_id, :author_id | |
def initialize(row) | |
@content = row[0] | |
@created_at = row[1] | |
@post_id = row[2] | |
@author_id = row[3] | |
end | |
def formatted_content | |
"#{content}\n#{created_at.strftime('%A, %b %d')} | by #{author.name}" | |
end | |
def post | |
Post.find(@post_id) | |
end | |
def author | |
Author.find(@author_id) unless anonymous? | |
end | |
def anonymous? | |
@author_id.nil? | |
end | |
def self.find_by_post(id) | |
all.find_all { |comment| comment.post_id == id } | |
end | |
def self.all | |
CSV.read('path/to.csv').map {|row| Comment.new(row) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment