mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-03-24 01:32:40 -04:00
Added associations and tested them
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
class Comment < ActiveRecord::Base
|
||||
attr_accessible :author_id, :body, :post_id
|
||||
belongs_to :author, :class_name => 'Member'
|
||||
belongs_to :post
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ class Member < ActiveRecord::Base
|
||||
friendly_id :login_name, use: :slugged
|
||||
|
||||
has_many :posts, :foreign_key => 'author_id'
|
||||
has_many :comments, :foreign_key => 'author_id'
|
||||
has_many :gardens, :foreign_key => 'owner_id'
|
||||
|
||||
# Include default devise modules. Others available are:
|
||||
|
||||
@@ -3,6 +3,7 @@ class Post < ActiveRecord::Base
|
||||
friendly_id :author_date_subject, use: :slugged
|
||||
attr_accessible :body, :subject, :author_id
|
||||
belongs_to :author, :class_name => 'Member'
|
||||
has_many :comments
|
||||
default_scope order("created_at desc")
|
||||
|
||||
def author_date_subject
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
# Read about factories at https://github.com/thoughtbot/factory_girl
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :comment do
|
||||
post_id 1
|
||||
author_id 1
|
||||
body "MyText"
|
||||
post
|
||||
author
|
||||
body "OMG LOL"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,2 +1,16 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Comment do
|
||||
|
||||
before(:each) do
|
||||
@comment = FactoryGirl.create(:comment)
|
||||
end
|
||||
|
||||
it "belongs to a post" do
|
||||
@comment.post.should be_an_instance_of Post
|
||||
end
|
||||
|
||||
it "belongs to an author" do
|
||||
@comment.author.should be_an_instance_of Member
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,6 +44,12 @@ describe 'member' do
|
||||
@member.gardens.first.name.should eq "Garden"
|
||||
end
|
||||
|
||||
it "has many comments" do
|
||||
@member.save
|
||||
@comment1 = FactoryGirl.create(:comment, :author => @member)
|
||||
@comment2 = FactoryGirl.create(:comment, :author => @member)
|
||||
@member.comments.length.should == 2
|
||||
end
|
||||
end
|
||||
|
||||
context 'no TOS agreement' do
|
||||
|
||||
@@ -20,4 +20,11 @@ describe Post do
|
||||
@datestr.length.should == 4 + @time.year.to_s.size
|
||||
@post.slug.should == "#{@member.login_name}-#{@datestr}-a-post"
|
||||
end
|
||||
|
||||
it "has many comments" do
|
||||
@post = FactoryGirl.create(:post, :author => @member)
|
||||
@comment1 = FactoryGirl.create(:comment, :post => @post)
|
||||
@comment2 = FactoryGirl.create(:comment, :post => @post)
|
||||
@post.comments.length.should == 2
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user