I’ve set up projects, tasks, and subtasks, all of wich are commentable. I have created comments model and connected the models but don’t know how to properly validate for user id and how to test model code using Rpsec and Factory Girl. I am also using Devise.
Comments Migration
1 class CreateComments < ActiveRecord::Migration
2 def change
3 create_table :comments do |t|
4 t.references :user, :null => false
5 t.text :comment, :null => false,
6 :limit => 500
7 t.references :commentable, :polymorphic => true
8 # upper line is a shortcut for this
9 # t.integer :commentable_id
10 # t.string :commentable_type
11 t.timestamps
12 end
13 add_index :comments, :user_id
14 add_index :comments, :commentable_id
15 end
16 end
Comment Model
1 class Comment < ActiveRecord::Base
2
3 # RELATIONSHIPS
4 belongs_to :commentable, :polymorphic => true
5 belongs_to :user
6 # VALIDATIONS
7 validates :user, :presence => true
validates :commentable, :presence => true
8 validates :comment, :presence => true,
9 :length => { :maximum => 500 }
10
11 # ATTRIBUTE ASSIGNMENT
12 attr_accessible :comment
13
14 end
User Model
1 class User < ActiveRecord::Base
2
3 # RELATIONSHIPS
4 has_many :synapses, :dependent => :destroy
5 has_many :projects, :through => :synapses
6
7 has_many :vesicles, :dependent => :destroy
8 has_many :tasks, :through => :vesicles
9
10 has_many :subvesicles, :dependent => :destroy
11 has_many :subtasks, :through => :subvesicles, :dependent => :nullify
12
13 has_many :comments, :dependent => :destroy
14
15 # VALIDATIONS
16 validates :name, :presence => true,
17 :uniqueness => true
18
19 # ATTRIBUTE ASSIGNMENT
20 attr_accessible :name, :email, :password, :password_confirmation, :remember_me
21
22 # DEVISE MODULES
23 # Include default devise modules. Others available are:
24 # :token_authenticatable, :encryptable, :lockable, :timeoutable and :omniauthable
25 devise :database_authenticatable, :registerable,
26 :recoverable, :rememberable, :trackable, :validatable,
27 :confirmable
28
29 end
Comment Factory
105 factory :comment do
106 comment "This is some generic comment with some generic content"
107 end
Comment Model Specs
1 require 'spec_helper'
2
3 describe Comment do
4
5 it 'Should create new comment' do
6 FactoryGirl.build(:comment).should be_valid
7 end
8
9 it 'Should respond to method provided by polymorphism to find its parent' do
10 FactoryGirl.build(:comment).should respond_to(:commentable)
11 end
12
13 end
This 1st test currently fails with error message saying undefined method `user’ for #Comment:0xa88c354>. But if I pass in user id like this…
FactoryGirl.build(:comment, :user => confirmed_user).should be_valid
Than I should have user id set up as mass assignable attribute, and I don’t want that (figure that some users might mess with that attribute and change it). How to test and validate this properly? Also, this is my 1st time doing polymorphic, so if you see anything silly, let me know.
Edit. I have now done this as one answer suggested. Unfortunately, it returns the same error.
5 it 'Should create new comment' do
6 confirmed_user = FactoryGirl.build(:confirmed_user)
7 FactoryGirl.build(:comment, :commentable => confirmed_user).should be_valid
8 end
Try to replace:
with:
UPD:
also has to be: