mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-24 08:52:14 -04:00
added associations between forums, posts, and members
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
class Forum < ActiveRecord::Base
|
||||
attr_accessible :description, :name, :owner_id
|
||||
has_many :posts
|
||||
belongs_to :owner, :class_name => "Member"
|
||||
end
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
5
db/migrate/20130213015708_add_forum_to_posts.rb
Normal file
5
db/migrate/20130213015708_add_forum_to_posts.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddForumToPosts < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :posts, :forum_id, :integer
|
||||
end
|
||||
end
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user