From 446eea30169bd2debc6b5d1f9d1635e765c43175 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 19 Dec 2007 17:18:39 +0000 Subject: [PATCH] ActionItem22: adding comment class --- app/models/comment.rb | 5 +++++ db/migrate/019_create_comments.rb | 26 ++++++++++++++++++++++++++ test/unit/comment_test.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 0 deletions(-) create mode 100644 app/models/comment.rb create mode 100644 db/migrate/019_create_comments.rb create mode 100644 test/unit/comment_test.rb diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..a34b704 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,5 @@ +class Comment < ActiveRecord::Base + validates_presence_of :title, :body + belongs_to :article + belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' +end diff --git a/db/migrate/019_create_comments.rb b/db/migrate/019_create_comments.rb new file mode 100644 index 0000000..b6314ac --- /dev/null +++ b/db/migrate/019_create_comments.rb @@ -0,0 +1,26 @@ +class CreateComments < ActiveRecord::Migration + def self.up + create_table :comments do |t| + + # acts as filesystem + t.column :title, :string + t.column :body, :text + + # belongs to an article + t.column :article_id, :integer + + # belongs to a person, maybe unauthenticated + t.column :author_id, :integer + t.column :name, :string + t.column :email, :string + + # keep track of changes + t.column :created_on, :datetime + end + + end + + def self.down + drop_table :comments + end +end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb new file mode 100644 index 0000000..797ebe9 --- /dev/null +++ b/test/unit/comment_test.rb @@ -0,0 +1,40 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CommentTest < Test::Unit::TestCase + + should 'have a name and require it' do + assert_mandatory(Comment.new, :title) + end + + should 'have a body and require it' do + assert_mandatory(Comment.new, :body) + end + + should 'belong to an article' do + c = Comment.new + assert_raise ActiveRecord::AssociationTypeMismatch do + c.article = 1 + end + assert_nothing_raised do + c.article = Article.new + end + end + + should 'record authenticated author' do + c = Comment.new + assert_raise ActiveRecord::AssociationTypeMismatch do + c.author = 1 + end + assert_raise ActiveRecord::AssociationTypeMismatch do + c.author = Profile + end + assert_nothing_raised do + c.author = Person.new + end + end + + should 'record unauthenticated author' do + flunk 'not yet' + end + +end -- libgit2 0.21.2