From 26cf91c61a9883f8d727de1b6cef65ab5d8c7a5a Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 30 Jan 2018 02:55:21 +0100 Subject: [PATCH] Create valuation comments creation ability Why: Only admins or valuators (for those investments they've assigned) can create internal valuation comments on them. How: * Creating a new `comment_valuation` ability for admins and valuators in the same manner the `valuate` ability works. * Adding a validation at Comment model for those with `valuation` flag active that checks if the author can make a valuation comment on the commentable, as well as the respective active record error messages. This will prevent comments from being created at a controller level as well. * Improving comment factory trait `valuation` to have an associated investment, author that is a valuator and setting the valuator on the valuators list of the investment --- app/models/abilities/administrator.rb | 2 +- app/models/abilities/valuator.rb | 4 ++-- app/models/comment.rb | 6 ++++++ config/locales/en/activerecord.yml | 4 ++++ config/locales/es/activerecord.yml | 4 ++++ spec/factories.rb | 6 ++++++ 6 files changed, 23 insertions(+), 3 deletions(-) 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