diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 98357e96e..0f5cb680b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -45,12 +45,13 @@ class CommentsController < ApplicationController def comment_params params.require(:comment).permit(:commentable_type, :commentable_id, :parent_id, - :body, :as_moderator, :as_administrator) + :body, :as_moderator, :as_administrator, :valuation) end def build_comment @comment = Comment.build(@commentable, current_user, comment_params[:body], - comment_params[:parent_id].presence) + comment_params[:parent_id].presence, + comment_params[:valuation]) check_for_special_comments end diff --git a/app/controllers/valuation/budget_investments_controller.rb b/app/controllers/valuation/budget_investments_controller.rb index c8d61215b..643ccbc74 100644 --- a/app/controllers/valuation/budget_investments_controller.rb +++ b/app/controllers/valuation/budget_investments_controller.rb @@ -1,11 +1,14 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController include FeatureFlags + include CommentableActions + feature_flag :budgets before_action :restrict_access_to_assigned_items, only: [:show, :edit, :valuate] before_action :load_budget before_action :load_investment, only: [:show, :edit, :valuate] + has_orders %w{oldest}, only: [:show, :edit] has_filters %w{valuating valuation_finished}, only: :index load_and_authorize_resource :investment, class: "Budget::Investment" @@ -36,8 +39,30 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController end end + def show + load_comments + end + + def edit + load_comments + end + private + def load_comments + @commentable = @investment + @comment_tree = CommentTree.new(@commentable, params[:page], @current_order, valuations: true) + set_comment_flags(@comment_tree.comments) + end + + def resource_model + Budget::Investment + end + + def resource_name + resource_model.parameterize('_') + end + def load_budget @budget = Budget.find(params[:budget_id]) end 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/budget/investment.rb b/app/models/budget/investment.rb index 58674be9b..f909a9746 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -34,7 +34,10 @@ class Budget has_many :valuator_assignments, dependent: :destroy has_many :valuators, through: :valuator_assignments - has_many :comments, as: :commentable + + has_many :comments, -> {where(valuation: false)}, as: :commentable, class_name: 'Comment' + has_many :valuations, -> {where(valuation: true)}, as: :commentable, class_name: 'Comment' + has_many :milestones validates :title, presence: true @@ -86,6 +89,10 @@ class Budget before_validation :set_responsible_name before_validation :set_denormalized_ids + def comments_count + comments.count + end + def url budget_investment_path(budget, self) end diff --git a/app/models/comment.rb b/app/models/comment.rb index 50f6cef66..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 } @@ -33,7 +34,8 @@ class Comment < ActiveRecord::Base end scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :public_for_api, -> do - where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or + not_valuations + .where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or (comments.commentable_type = 'Proposal' and comments.commentable_id in (?)) or (comments.commentable_type = 'Poll' and comments.commentable_id in (?))}, Debate.public_for_api.pluck(:id), @@ -50,13 +52,16 @@ class Comment < ActiveRecord::Base scope :sort_by_oldest, -> { order(created_at: :asc) } scope :sort_descendants_by_oldest, -> { order(created_at: :asc) } + scope :not_valuations, -> { where(valuation: false) } + after_create :call_after_commented - def self.build(commentable, user, body, p_id = nil) - new commentable: commentable, + def self.build(commentable, user, body, p_id = nil, valuation = false) + new(commentable: commentable, user_id: user.id, body: body, - parent_id: p_id + parent_id: p_id, + valuation: valuation) end def self.find_commentable(c_type, c_id) @@ -129,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/app/views/comments/_actions.html.erb b/app/views/comments/_actions.html.erb index 80302a70a..2852fa09d 100644 --- a/app/views/comments/_actions.html.erb +++ b/app/views/comments/_actions.html.erb @@ -1,6 +1,8 @@ - - <%= render 'comments/flag_actions', comment: comment %> - +<% if local_assigns.fetch(:allow_flagging, true) %> + + <%= render 'comments/flag_actions', comment: comment %> + +<% end %> <% if can? :hide, comment %> diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 8d45b5469..f57876030 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,7 @@ <% comment_flags ||= @comment_flags %> +<% valuation = local_assigns.fetch(:valuation, false) %> +<% allow_votes = local_assigns.fetch(:allow_votes, true) %> +<% allow_flagging = local_assigns.fetch(:allow_flagging, true) %> <% cache [locale_and_user_status(comment), comment, commentable_cache_key(comment.commentable), comment.author, (comment_flags[comment.id] if comment_flags)] do %>