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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user