diff --git a/app/models/abilities/valuator.rb b/app/models/abilities/valuator.rb index 15add866a..85c598d12 100644 --- a/app/models/abilities/valuator.rb +++ b/app/models/abilities/valuator.rb @@ -3,7 +3,9 @@ module Abilities include CanCan::Ability def initialize(user) + valuator = user.valuator can [:read, :update, :valuate], SpendingProposal + can [:update, :valuate], Budget::Investment, id: valuator.investment_ids, budget: { valuating: true } end end -end \ No newline at end of file +end diff --git a/app/models/valuator.rb b/app/models/valuator.rb index 8b82d20b1..5df6ea030 100644 --- a/app/models/valuator.rb +++ b/app/models/valuator.rb @@ -4,6 +4,8 @@ class Valuator < ActiveRecord::Base has_many :valuation_assignments, dependent: :destroy has_many :spending_proposals, through: :valuation_assignments + has_many :valuator_assignments, dependent: :destroy, class_name: 'Budget::ValuatorAssignment' + has_many :investments, through: :valuator_assignments, class_name: 'Budget::Investment' validates :user_id, presence: true, uniqueness: true diff --git a/spec/models/abilities/valuator_spec.rb b/spec/models/abilities/valuator_spec.rb index ce108200c..9fa285f19 100644 --- a/spec/models/abilities/valuator_spec.rb +++ b/spec/models/abilities/valuator_spec.rb @@ -5,8 +5,24 @@ describe "Abilities::Valuator" do subject(:ability) { Ability.new(user) } let(:user) { valuator.user } let(:valuator) { create(:valuator) } + let(:non_assigned_investment) { create(:budget_investment) } + + let(:assigned_investment) { create(:budget_investment, budget: create(:budget, valuating: true)) } + before(:each) { assigned_investment.valuators << valuator } + + let(:assigned_investment_not_valuating) { create(:budget_investment, budget: create(:budget, valuating: false)) } + before(:each) { assigned_investment_not_valuating.valuators << valuator } it { should be_able_to(:read, SpendingProposal) } it { should be_able_to(:update, SpendingProposal) } it { should be_able_to(:valuate, SpendingProposal) } + + it { should_not be_able_to(:update, non_assigned_investment) } + it { should_not be_able_to(:valuate, non_assigned_investment) } + + it { should be_able_to(:update, assigned_investment) } + it { should be_able_to(:valuate, assigned_investment) } + + it { should_not be_able_to(:update, assigned_investment_not_valuating) } + it { should_not be_able_to(:valuate, assigned_investment_not_valuating) } end