Skip to content

Commit

Permalink
Create valuation comments creation ability
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bertocq committed Jan 30, 2018
1 parent 5dd0ef0 commit c87351d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/abilities/administrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize(user)
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
Expand Down
4 changes: 2 additions & 2 deletions app/models/abilities/valuator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Valuator
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
6 changes: 6 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -133,4 +134,9 @@ def validate_body_length
validator.validate(self)
end

def comment_valuation
unless author.can?(:comment_valuation, commentable)
errors.add(:valuation, :cannot_comment_valuation)
end
end
end
4 changes: 4 additions & 0 deletions config/locales/en/activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions config/locales/es/activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@

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

Expand Down

0 comments on commit c87351d

Please sign in to comment.