diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 53b49dfb4..7af50446a 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -55,7 +55,7 @@ module Abilities can [:read, :create, :update, :destroy], Budget::Group can [:read, :create, :update, :destroy], Budget::Heading can [:hide, :update, :toggle_selection], Budget::Investment - can :valuate, Budget::Investment + can [:valuate, :comment_valuation], Budget::Investment can :create, Budget::ValuatorAssignment can [:search, :edit, :update, :create, :index, :destroy], Banner diff --git a/app/models/abilities/valuator.rb b/app/models/abilities/valuator.rb index 614869665..449a42410 100644 --- a/app/models/abilities/valuator.rb +++ b/app/models/abilities/valuator.rb @@ -5,8 +5,8 @@ module Abilities def initialize(user) valuator = user.valuator can [:read, :update, :valuate], SpendingProposal - can [:read, :update, :valuate], Budget::Investment, id: valuator.investment_ids - cannot [:update, :valuate], Budget::Investment, budget: { phase: 'finished' } + can [:read, :update, :valuate, :comment_valuation], Budget::Investment, id: valuator.investment_ids + cannot [:update, :valuate, :comment_valuation], Budget::Investment, budget: { phase: 'finished' } end end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 3c58f966b..d9329d3c0 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -20,6 +20,7 @@ class Comment < ActiveRecord::Base validates :commentable_type, inclusion: { in: COMMENTABLE_TYPES } validate :validate_body_length + validate :comment_valuation, if: -> { valuation } belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true belongs_to :user, -> { with_hidden } @@ -133,4 +134,9 @@ class Comment < ActiveRecord::Base validator.validate(self) end + def comment_valuation + unless author.can?(:comment_valuation, commentable) + errors.add(:valuation, :cannot_comment_valuation) + end + end end diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 81b25dcad..bb402946c 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -296,6 +296,10 @@ en: image: image_width: "Width must be %{required_width}px" image_height: "Height must be %{required_height}px" + comment: + attributes: + valuation: + cannot_comment_valuation: 'You cannot comment a valuation' messages: record_invalid: "Validation failed: %{errors}" restrict_dependent_destroy: diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index b604224cf..176e575cb 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -292,6 +292,10 @@ es: image: image_width: "Debe tener %{required_width}px de ancho" image_height: "Debe tener %{required_height}px de alto" + comment: + attributes: + valuation: + cannot_comment_valuation: 'No puedes comentar una evaluación' messages: record_invalid: 'Error de validación: %{errors}' restrict_dependent_destroy: diff --git a/spec/factories.rb b/spec/factories.rb index fe9bccd6c..8d299bcd8 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -468,6 +468,12 @@ FactoryBot.define do trait :valuation do valuation true + association :commentable, factory: :budget_investment + before :create do |valuation| + valuator = create(:valuator) + valuation.author = valuator.user + valuation.commentable.valuators << valuator + end end end