Commit 6dca9da8b03d2f4871469a49503a6ee542901f8c

Authored by Rafael Manzo
1 parent f80c1659

Methods to help on listing user projects

app/models/project_ownership.rb
1 1 class ProjectOwnership < ActiveRecord::Base
2 2 belongs_to :user
3 3 validates :project_id, presence: true
  4 +
  5 + def project
  6 + Project.find(project_id)
  7 + end
4 8 end
... ...
app/models/user.rb
... ... @@ -10,4 +10,9 @@ class User &lt; ActiveRecord::Base
10 10  
11 11 has_many :project_ownerships
12 12 # Alert: when adding new parameters to this model, they should also be added to registrations_controller
  13 +
  14 + def projects
  15 + #raise project_ownerships.inspect
  16 + project_ownerships.map { |project_ownership| project_ownership.project }
  17 + end
13 18 end
... ...
spec/models/project_ownership_spec.rb
... ... @@ -4,4 +4,19 @@ describe ProjectOwnership do
4 4 describe 'associations' do
5 5 it { should belong_to(:user) }
6 6 end
  7 +
  8 + describe 'methods' do
  9 + describe 'project' do
  10 + subject {FactoryGirl.build(:project_ownership)}
  11 + let(:project) {FactoryGirl.build(:project)}
  12 +
  13 + before :each do
  14 + Project.expects(:find).with(subject.project_id).returns(project)
  15 + end
  16 +
  17 + it 'should return the project' do
  18 + subject.project.should eq(project)
  19 + end
  20 + end
  21 + end
7 22 end
... ...
spec/models/user_spec.rb
... ... @@ -12,4 +12,21 @@ describe User do
12 12 describe 'associations' do
13 13 it { should have_many(:project_ownerships) }
14 14 end
  15 +
  16 + describe 'methods' do
  17 + describe 'projects' do
  18 + subject { FactoryGirl.build(:user) }
  19 + let(:project) {FactoryGirl.build(:project)}
  20 + let(:project_ownership) {FactoryGirl.build(:project_ownership)}
  21 +
  22 + before :each do
  23 + project_ownership.expects(:project).returns(project)
  24 + subject.expects(:project_ownerships).returns([project_ownership])
  25 + end
  26 +
  27 + it 'should return a list of projects owned by the user' do
  28 + subject.projects.should eq([project])
  29 + end
  30 + end
  31 + end
15 32 end
... ...