Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valuation comments #2403

Merged
merged 10 commits into from
Jan 31, 2018
5 changes: 3 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def unflag

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

Expand Down
25 changes: 25 additions & 0 deletions app/controllers/valuation/budget_investments_controller.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -36,8 +39,30 @@ def valuate
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
Expand Down
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
9 changes: 8 additions & 1 deletion app/models/budget/investment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class Investment < ActiveRecord::Base

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
Expand Down Expand Up @@ -86,6 +89,10 @@ class Investment < ActiveRecord::Base
before_validation :set_responsible_name
before_validation :set_denormalized_ids

def comments_count
comments.count
end

def url
budget_investment_path(budget, self)
end
Expand Down
18 changes: 14 additions & 4 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 All @@ -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),
Expand All @@ -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)
Expand Down Expand Up @@ -129,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
8 changes: 5 additions & 3 deletions app/views/comments/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<span id="flag-actions-<%= dom_id(comment) %>" class="js-flag-actions">
<%= render 'comments/flag_actions', comment: comment %>
</span>
<% if local_assigns.fetch(:allow_flagging, true) %>
<span id="flag-actions-<%= dom_id(comment) %>" class="js-flag-actions">
<%= render 'comments/flag_actions', comment: comment %>
</span>
<% end %>

<span class='js-moderation-actions'>
<% if can? :hide, comment %>
Expand Down
24 changes: 18 additions & 6 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
<ul id="<%= dom_id(comment) %>" class="comment no-bullet small-12">
<li class="comment-body">
Expand Down Expand Up @@ -67,9 +70,11 @@
</div>

<div id="<%= dom_id(comment) %>_reply" class="reply">
<div id="<%= dom_id(comment) %>_votes" class="comment-votes float-right">
<%= render 'comments/votes', comment: comment %>
</div>
<% if allow_votes %>
<div id="<%= dom_id(comment) %>_votes" class="comment-votes float-right">
<%= render 'comments/votes', comment: comment %>
</div>
<% end %>

<% if comment.children.size > 0 %>
<%= link_to "", class: "js-toggle-children relative", data: {'id': "#{dom_id(comment)}"} do %>
Expand All @@ -86,9 +91,13 @@
<%= link_to(comment_link_text(comment), "",
class: "js-add-comment-link", data: {'id': dom_id(comment)}) %>

<%= render 'comments/actions', comment: comment %>
<%= render 'comments/actions', { comment: comment,
allow_flagging: allow_flagging } %>

<%= render 'comments/form', {commentable: comment.commentable, parent_id: comment.id, toggeable: true} %>
<%= render 'comments/form', {commentable: comment.commentable,
parent_id: comment.id,
toggeable: true,
valuation: valuation } %>
<% end %>
</div>
<% end %>
Expand All @@ -98,7 +107,10 @@
<ul id="<%= dom_id(comment) %>_children" class="no-bullet comment-children">
<% child_comments_of(comment).each do |child| %>
<li>
<%= render 'comments/comment', comment: child %>
<%= render 'comments/comment', { comment: child,
valuation: valuation,
allow_votes: allow_votes,
allow_flagging: allow_flagging } %>
</li>
<% end %>
</ul>
Expand Down
11 changes: 8 additions & 3 deletions app/views/comments/_comment_tree.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% commentable = comment_tree.commentable %>

<% valuation = local_assigns.fetch(:valuation, false) %>
<% cache [locale_and_user_status, comment_tree.order, commentable_cache_key(commentable), comment_tree.comments, comment_tree.comment_authors, commentable.comments_count, comment_flags] do %>
<section class="expanded comments">
<div class="row">
Expand Down Expand Up @@ -27,7 +27,8 @@
<% else %>
<%= render 'comments/form', { commentable: commentable,
parent_id: nil,
toggeable: false } %>
toggeable: false,
valuation: valuation } %>
<% end %>
<% else %>
<br>
Expand All @@ -39,7 +40,11 @@
<% end %>

<% comment_tree.root_comments.each do |comment| %>
<%= render 'comments/comment', {comment: comment, comment_flags: comment_flags} %>
<%= render 'comments/comment', { comment: comment,
comment_flags: comment_flags,
valuation: valuation,
allow_votes: !valuation,
allow_flagging: !valuation } %>
<% end %>
<%= paginate comment_tree.root_comments %>
</div>
Expand Down
8 changes: 6 additions & 2 deletions app/views/comments/_commentable_tree.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<%= render 'shared/wide_order_selector', i18n_namespace: "comments" %>

<% if user_signed_in? %>
<%= render 'comments/form', {commentable: @investment, parent_id: nil, toggeable: false} %>
<%= render 'comments/form', { commentable: @investment,
parent_id: nil,
toggeable: false,
valuation: local_assigns.fetch(:valuation, false) } %>
<% else %>
<br>

Expand All @@ -22,7 +25,8 @@
<% end %>

<% @comment_tree.root_comments.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<%= render 'comments/comment', { comment: comment,
valuation: local_assigns.fetch(:valuation, false) } %>
<% end %>
<%= paginate @comment_tree.root_comments %>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= f.hidden_field :commentable_type, value: commentable.class.name %>
<%= f.hidden_field :commentable_id, value: commentable.id %>
<%= f.hidden_field :parent_id, value: parent_id %>
<%= f.hidden_field :valuation, value: local_assigns.fetch(:valuation, false) %>

<%= f.submit comment_button_text(parent_id, commentable), class: "button", id: "publish_comment" %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@
<h2><%= t("valuation.budget_investments.show.internal_comments") %></h2>
<%= explanation_field @investment.internal_comments %>
<% end %>

<div class="tabs-panel is-active" id="tab-comments">
<% unless @comment_tree.nil? %>
<%= render partial: '/comments/comment_tree', locals: { comment_tree: @comment_tree,
comment_flags: @comment_flags,
display_comments_count: false,
valuation: true } %>
<% end %>
</div>
9 changes: 9 additions & 0 deletions app/views/valuation/budget_investments/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@
</div>
<% end %>

<div class="tabs-panel is-active" id="tab-comments">
<% unless @comment_tree.nil? %>
<%= render partial: '/comments/comment_tree', locals: { comment_tree: @comment_tree,
comment_flags: @comment_flags,
display_comments_count: false,
valuation: true } %>
<% end %>
</div>

<h1><%= @investment.title %></h1>

<%= safe_html_with_links @investment.description %>
Expand Down
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
5 changes: 5 additions & 0 deletions db/migrate/20180129190931_add_valuation_flag_to_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddValuationFlagToComments < ActiveRecord::Migration
def change
add_column :comments, :valuation, :boolean, default: false
end
end
7 changes: 7 additions & 0 deletions db/migrate/20180129190950_add_index_to_valuation_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddIndexToValuationComments < ActiveRecord::Migration
disable_ddl_transaction!

def change
add_index :comments, :valuation, algorithm: :concurrently
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180119073228) do
ActiveRecord::Schema.define(version: 20180129190950) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -234,7 +234,7 @@
t.string "commentable_type"
t.text "body"
t.string "subject"
t.integer "user_id", null: false
t.integer "user_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "hidden_at"
Expand All @@ -247,7 +247,8 @@
t.integer "cached_votes_down", default: 0
t.datetime "confirmed_hide_at"
t.string "ancestry"
t.integer "confidence_score", default: 0, null: false
t.integer "confidence_score", default: 0, null: false
t.boolean "valuation", default: false
end

add_index "comments", ["ancestry"], name: "index_comments_on_ancestry", using: :btree
Expand All @@ -257,6 +258,7 @@
add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type", using: :btree
add_index "comments", ["hidden_at"], name: "index_comments_on_hidden_at", using: :btree
add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree
add_index "comments", ["valuation"], name: "index_comments_on_valuation", using: :btree

create_table "communities", force: :cascade do |t|
t.datetime "created_at", null: false
Expand Down