added associations between forums, posts, and members

This commit is contained in:
gnattery
2013-02-13 15:17:40 +11:00
parent e10ea7f180
commit de85a6dcce
10 changed files with 43 additions and 5 deletions

View File

@@ -1,3 +1,5 @@
class Forum < ActiveRecord::Base
attr_accessible :description, :name, :owner_id
has_many :posts
belongs_to :owner, :class_name => "Member"
end

View File

@@ -5,6 +5,7 @@ class Member < ActiveRecord::Base
has_many :posts, :foreign_key => 'author_id'
has_many :comments, :foreign_key => 'author_id'
has_many :gardens, :foreign_key => 'owner_id'
has_many :forums, :foreign_key => 'owner_id'
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,

View File

@@ -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'
belongs_to :forum
has_many :comments, :dependent => :destroy
default_scope order("created_at desc")

View File

@@ -0,0 +1,5 @@
class AddForumToPosts < ActiveRecord::Migration
def change
add_column :posts, :forum_id, :integer
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130213014511) do
ActiveRecord::Schema.define(:version => 20130213015708) do
create_table "comments", :force => true do |t|
t.integer "post_id", :limit => 255, :null => false
@@ -107,6 +107,7 @@ ActiveRecord::Schema.define(:version => 20130213014511) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "slug"
t.integer "forum_id"
end
add_index "posts", ["created_at", "author_id"], :name => "index_updates_on_created_at_and_user_id"

View File

@@ -2,8 +2,8 @@
FactoryGirl.define do
factory :forum do
name "MyString"
description "MyText"
owner_id 1
name "Permaculture"
description "*Everything* about permaculture!"
owner
end
end

View File

@@ -15,6 +15,10 @@ FactoryGirl.define do
factory :html_post do
body '<a href="http://evil.com">EVIL</a>'
end
factory :forum_post do
forum
end
end
end

View File

@@ -1,5 +1,17 @@
require 'spec_helper'
describe Forum do
pending "add some examples to (or delete) #{__FILE__}"
before(:each) do
@forum = FactoryGirl.create(:forum)
end
it "belongs to an owner" do
@forum.owner.should be_an_instance_of Member
end
it "has many posts" do
@post1 = FactoryGirl.create(:forum_post, :forum => @forum)
@post2 = FactoryGirl.create(:forum_post, :forum => @forum)
@forum.posts.length.should == 2
end
end

View File

@@ -56,6 +56,13 @@ describe 'member' do
@member.comments.length.should == 2
end
it "has many forums" do
@member.save
@forum1 = FactoryGirl.create(:forum, :owner => @member)
@forum2 = FactoryGirl.create(:forum, :owner => @member)
@member.forums.length.should == 2
end
it 'has location and lat/long fields' do
@member.update_attributes(:location => 'Greenwich, UK')
@member.location.should eq 'Greenwich, UK'

View File

@@ -37,4 +37,9 @@ describe Post do
@post.destroy
Comment.count.should == all - 2
end
it "belongs to a forum" do
@post = FactoryGirl.create(:forum_post)
@post.forum.should be_an_instance_of Forum
end
end