Add can_comment and can_edit_dossier abilities to valuators

This commit is contained in:
lalo
2019-04-11 10:59:07 +02:00
parent d266cb33ce
commit c2860dda0e
39 changed files with 207 additions and 151 deletions

View File

@@ -47,7 +47,8 @@ class Admin::ValuatorsController < Admin::BaseController
def valuator_params
params[:valuator][:description] = nil if params[:valuator][:description].blank?
params.require(:valuator).permit(:user_id, :description, :valuator_group_id)
params.require(:valuator).permit(:user_id, :description, :valuator_group_id,
:can_comment, :can_edit_dossier)
end
end

View File

@@ -4,4 +4,9 @@ module ValuatorsHelper
truncate([valuator.name, valuator.email, valuator.description].compact.join(" - "), length: 100)
end
def valuator_abilities(valuator)
[ valuator.can_comment ? I18n.t("admin.valuators.index.can_comment") : nil ,
valuator.can_edit_dossier ? I18n.t("admin.valuators.index.can_edit_dossier") : nil
].compact.join(", ")
end
end

View File

@@ -65,6 +65,7 @@ module Abilities
can [:hide, :update, :toggle_selection], Budget::Investment
can [:valuate, :comment_valuation], Budget::Investment
can :create, Budget::ValuatorAssignment
can [:edit_dossier], Budget::Investment
can(:read_admin_stats, Budget) { |budget| budget.balloting_or_later? }

View File

@@ -4,10 +4,20 @@ module Abilities
def initialize(user)
valuator = user.valuator
assigned_investment_ids = valuator.assigned_investment_ids
finished = { phase: "finished" }
can [:read, :update, :comment_valuation], Budget::Investment, id: valuator.assigned_investment_ids
can [:valuate], Budget::Investment, { id: valuator.assigned_investment_ids, valuation_finished: false }
cannot [:update, :valuate, :comment_valuation], Budget::Investment, budget: { phase: "finished" }
can [:read, :update], Budget::Investment, id: assigned_investment_ids
can [:valuate], Budget::Investment, { id: assigned_investment_ids, valuation_finished: false }
cannot [:update, :valuate, :comment_valuation], Budget::Investment, budget: finished
if valuator.can_edit_dossier?
can [:edit_dossier], Budget::Investment, id: assigned_investment_ids
end
if valuator.can_comment?
can [:comment_valuation], Budget::Investment, id: assigned_investment_ids
end
end
end
end

View File

@@ -0,0 +1,14 @@
<tr>
<td><%= link_to user.name, user_path(user), target: "_blank" %></td>
<td><%= user.email %></td>
<td></td>
<td></td>
<td></td>
<td>
<%= form_for Valuator.new(user: user), url: admin_valuators_path do |f| %>
<%= f.hidden_field :user_id %>
<%= f.submit t("admin.valuators.valuator.add"),
class: "button success expanded" %>
<% end %>
</td>
</tr>

View File

@@ -0,0 +1,30 @@
<tr>
<td><%= link_to valuator.name, admin_valuator_path(valuator) %></td>
<td><%= valuator.email %></td>
<td>
<% if valuator.description.present? %>
<%= valuator.description %>
<% else %>
<%= t("admin.valuators.index.no_description") %>
<% end %>
</td>
<td>
<% if valuator.valuator_group.present? %>
<%= valuator.valuator_group.try(:name) %>
<% else %>
<%= t("admin.valuators.index.no_group") %>
<% end %>
</td>
<td>
<%= valuator_abilities(valuator) %>
</td>
<td>
<%= link_to t("admin.actions.edit"),
edit_admin_valuator_path(valuator),
class: "button hollow" %>
<%= link_to t("admin.valuators.valuator.delete"),
admin_valuator_path(valuator),
method: :delete,
class: "button hollow alert" %>
</td>
</tr>

View File

@@ -0,0 +1,21 @@
<table>
<thead>
<th scope="col"><%= t("admin.valuators.index.name") %></th>
<th scope="col"><%= t("admin.valuators.index.email") %></th>
<th scope="col"><%= t("admin.valuators.index.description") %></th>
<th scope="col"><%= t("admin.valuators.index.group") %></th>
<th scope="col"><%= t("admin.valuators.index.abilities") %></th>
<th scope="col" class="small-3"><%= t("admin.actions.actions") %></th>
</thead>
<tbody>
<% valuators.each do |valuator| %>
<% if valuator.is_a?(Valuator) %>
<%= render partial: 'valuator_row', locals: {valuator: valuator} %>
<% elsif valuator.valuator? %>
<%= render partial: 'valuator_row', locals: {valuator: valuator.valuator} %>
<% else %>
<%= render partial: 'user_row', locals: {user: valuator} %>
<% end %>
<% end %>
</tbody>
</table>

View File

@@ -15,6 +15,12 @@
@valuator_groups.map {|group| [group.name, group.id] },
{ include_blank: true } %>
</div>
<%= f.submit t("admin.valuators.form.update"), class: "button success" %>
<div>
<div class="filter">
<%= f.check_box :can_comment %>
<%= f.check_box :can_edit_dossier %>
</div>
</div>
<%= f.submit class: "button success" %>
<% end %>
</div>

View File

@@ -8,46 +8,7 @@
<% if @valuators.any? %>
<h3 class="margin"><%= page_entries_info @valuators %></h3>
<table>
<thead>
<th scope="col"><%= t("admin.valuators.index.name") %></th>
<th scope="col"><%= t("admin.valuators.index.email") %></th>
<th scope="col"><%= t("admin.valuators.index.description") %></th>
<th scope="col"><%= t("admin.valuators.index.group") %></th>
<th scope="col" class="small-3"><%= t("admin.actions.actions") %></th>
</thead>
<tbody>
<% @valuators.each do |valuator| %>
<tr>
<td><%= link_to valuator.name, admin_valuator_path(valuator) %></td>
<td><%= valuator.email %></td>
<td>
<% if valuator.description.present? %>
<%= valuator.description %>
<% else %>
<%= t("admin.valuators.index.no_description") %>
<% end %>
</td>
<td>
<% if valuator.valuator_group.present? %>
<%= valuator.valuator_group.try(:name) %>
<% else %>
<%= t("admin.valuators.index.no_group") %>
<% end %>
</td>
<td>
<%= link_to t("admin.actions.edit"),
edit_admin_valuator_path(valuator),
class: "button hollow" %>
<%= link_to t("admin.valuators.valuator.delete"),
admin_valuator_path(valuator),
method: :delete,
class: "button hollow alert" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= render partial: "valuators", locals: {valuators: @valuators} %>
<%= paginate @valuators %>
<% else %>

View File

@@ -5,51 +5,7 @@
<div id="valuators">
<% if @users.any? %>
<h3 class="margin"><%= page_entries_info @users %></h3>
<table>
<thead>
<th scope="col"><%= t("admin.valuators.index.name") %></th>
<th scope="col"><%= t("admin.valuators.index.email") %></th>
<th scope="col"><%= t("admin.valuators.index.description") %></th>
<th scope="col"><%= t("admin.shared.actions") %></th>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td>
<% if user.valuator? %>
<% if user.valuator.description.present? %>
<%= user.valuator.description %>
<% else %>
<%= t("admin.valuators.index.no_description") %>
<% end %>
<% else %>
<%= t("admin.valuators.index.no_description") %>
<% end %>
<td>
<% if user.valuator? %>
<%= link_to t("admin.actions.edit"),
edit_admin_valuator_path(user.valuator),
class: "button hollow" %>
<%= link_to t("admin.valuators.valuator.delete"),
admin_valuator_path(user),
method: :delete,
class: "button hollow alert expanded" %>
<% else %>
<%= form_for Valuator.new(user: user), url: admin_valuators_path do |f| %>
<%= f.hidden_field :user_id %>
<%= f.submit t("admin.valuators.valuator.add"),
class: "button success expanded" %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= render partial: "valuators", locals: {valuators: @users} %>
<% else %>
<div class="callout alert margin">
<%= t("admin.shared.no_search_results") %>

View File

@@ -27,4 +27,13 @@
<%= t("admin.valuators.show.no_group") %>
<% end %>
</p>
<p>
<strong><%= t("admin.valuators.show.abilities") %></strong><br>
<% if valuator_abilities(@valuator).present? %>
<%= valuator_abilities(@valuator) %>
<% else %>
<%= t("admin.valuators.show.no_abilities") %>
<% end %>
</p>
</div>

View File

@@ -1,10 +1,11 @@
<h2><%= t("valuation.budget_investments.valuation_comments") %></h2>
<% unless @comment_tree.nil? %>
<%= render partial: "/comments/comment_tree", locals: {
comment_tree: @comment_tree,
<% can_comment = (!@budget.finished? && can?(:comment_valuation, @investment)) %>
<%= render partial: "/comments/comment_tree", locals: { comment_tree: @comment_tree,
comment_flags: @comment_flags,
display_comments_count: false,
valuation: true,
allow_comments: !@budget.finished?,
allow_comments: can_comment,
admin_layout: true } %>
<% end %>

View File

@@ -1,10 +1,10 @@
<%= link_to valuation_budget_budget_investment_path(@budget, @investment), class: "back" do %>
<span class="icon-angle-left"></span>
<%= "#{t("valuation.budget_investments.show.title")} #{@investment.id}"%>
<%= "#{t("valuation.budget_investments.show.title")} #{@investment.id}" %>
<% end %>
<h2><%= t("valuation.budget_investments.edit.dossier") %></h2>
<% if can?(:valuate, @investment) %>
<% if can?(:valuate, @investment) && can?(:edit_dossier, @investment) %>
<%= render partial: "/valuation/budget_investments/dossier_form", locals: {investment: @investment} %>
<% else %>
<%= render partial: "/valuation/budget_investments/dossier_detail", locals: {investment: @investment} %>

View File

@@ -912,7 +912,6 @@ ar:
cost: التكلفة
form:
edit_title: "المقيّمون: تعديل المقيّمين"
update: "تحديث المقيّم"
updated: "تم تحديث المقيّمين بنجاح"
show:
description: "الوصف"

View File

@@ -787,7 +787,6 @@ de:
cost: Gesamtkosten
form:
edit_title: "Begutachter*innen: Begutachter*in bearbeiten"
update: "Begutachter*in aktualisieren"
updated: "Begutachter*in erfolgreich aktualisiert"
show:
description: "Beschreibung"

View File

@@ -352,6 +352,11 @@ en:
link:
label: Title
url: Link
valuator:
description: Description
valuator_group_id: Valuator group
can_comment: Can create comments
can_edit_dossier: Can edit dossiers
errors:
models:
user:

View File

@@ -903,6 +903,9 @@ en:
valuator_groups: "Valuator Groups"
group: "Group"
no_group: "No group"
abilities: "Abilities"
can_comment: "Can comment"
can_edit_dossier: "Can edit dossier"
valuator:
add: Add to valuators
delete: Delete
@@ -910,14 +913,15 @@ en:
title: "Valuators: User search"
form:
edit_title: "Valuators: Edit valuator"
update: "Update valuator"
updated: "Valuator updated successfully"
show:
description: "Description"
email: "Email"
group: "Group"
abilities: "Abilities"
no_description: "Without description"
no_group: "Without group"
no_abilities: "Without abilities"
valuator_groups:
index:
title: "Valuator groups"

View File

@@ -354,6 +354,11 @@ es:
link:
label: Título
url: Enlace
valuator:
description: Descripción
valuator_group_id: Grupo de evaluación
can_comment: Puede comentar
can_edit_dossier: Puede editar informes
errors:
models:
user:

View File

@@ -902,6 +902,9 @@ es:
valuator_groups: "Grupo de evaluadores"
group: "Grupo"
no_group: "Sin grupo"
abilities: "Habilidades"
can_comment: "Puede comentar"
can_edit_dossier: "Puede editar informes"
valuator:
add: Añadir como evaluador
delete: Borrar
@@ -909,14 +912,15 @@ es:
title: "Evaluadores: Búsqueda de usuarios"
form:
edit_title: "Evaluadores: Editar evaluador"
update: "Actualizar evaluador"
updated: "Evaluador actualizado correctamente"
show:
description: "Descripción"
email: "Email"
group: "Grupo"
abilities: "Habilidades"
no_description: "Sin descripción"
no_group: "Sin grupo"
no_abilities: "Sin habilidades"
valuator_groups:
index:
title: "Grupos de evaluadores"

View File

@@ -630,7 +630,6 @@ fa:
cost: "هزینه\n"
form:
edit_title: "ارزیابی کنندگان: ویرایش ارزیاب"
update: "به روزرسانی ارزیابی "
updated: " ارزیابی با موفقیت به روز رسانی شده"
show:
description: "توضیحات"

View File

@@ -868,7 +868,6 @@ fr:
cost: Coût
form:
edit_title: "Évaluateurs : modifier l'évaluateur"
update: "Modifier l'évaluateur"
updated: "L'évaluateur a bien été modifié"
show:
description: "Description"

View File

@@ -798,7 +798,6 @@ gl:
cost: Custo total
form:
edit_title: "Avaliadores: Editar avaliador"
update: "Actualizar avaliador"
updated: "Avaliador actualizado correctamente"
show:
description: "Descrición"

View File

@@ -710,7 +710,6 @@ it:
cost: Costi
form:
edit_title: "Stimatori: Modificare stimatore"
update: "Aggiorna stimatore"
updated: "Stimatore aggiornato con successo"
show:
description: "Descrizione"

View File

@@ -826,7 +826,6 @@ nl:
cost: Kosten
form:
edit_title: "Beoordelaars: Bewerk Beoordelaar"
update: "Sla op"
updated: "Beoordelaar aangepast"
show:
description: "Beschrijving"

View File

@@ -823,7 +823,6 @@ pl:
cost: Koszt
form:
edit_title: "Wyceniający: Edytuj wyceniającego"
update: "Aktualizuj wyceniającego"
updated: "Wyceniający zaktualizowany pomyślnie"
show:
description: "Opis"

View File

@@ -709,7 +709,6 @@ pt-BR:
cost: Custo
form:
edit_title: "Avaliadores: editar avaliador"
update: "Atualizar avaliador"
updated: "Avalaiador atualizado com sucesso"
show:
description: "Descrição"

View File

@@ -826,7 +826,6 @@ ru:
cost: Стоимость
form:
edit_title: "Оценщики: Редактировать оценщика"
update: "Обновить оценщика"
updated: "Оценщик обновлен успешно"
show:
description: "Описание"

View File

@@ -525,7 +525,6 @@ sl:
cost: Cena
form:
edit_title: "Cenilci: Uredi cenilce"
update: "Posodobi cenilca"
updated: "Cenilec uspešno posodobljen"
show:
description: "Opis"

View File

@@ -715,7 +715,6 @@ so:
cost: Kharashaad
form:
edit_title: "Qiimeeyayaasha: Tafatiraha qiimeeyaha"
update: "Cusboneysi Qimeeyaha"
updated: "Siguula ayaa loo cusboneysiyey"
show:
description: "Sharaxaad"

View File

@@ -717,7 +717,6 @@ sq:
cost: Kosto
form:
edit_title: "Vlerësuesit: Ndrysho vlerësuesin"
update: "Përditëso vlerësuesin"
updated: "Vlerësuesi u përditësua me sukses"
show:
description: "Përshkrimi"

View File

@@ -764,7 +764,6 @@ val:
cost: Cost total
form:
edit_title: "Avaluadors: Editar avaluador"
update: "Actualitzar avaluador"
updated: "Avaluador actualitzat correctament"
show:
description: "Descripció"

View File

@@ -718,7 +718,6 @@ zh-CN:
cost: 成本
form:
edit_title: "评估员:编辑评估员"
update: "更新评估员"
updated: "评估员已成功更新"
show:
description: "说明"

View File

@@ -708,7 +708,6 @@ zh-TW:
cost: 成本
form:
edit_title: "評估員:編輯評估員"
update: "更新評估員"
updated: "評估員已成功更新"
show:
description: "描述"

View File

@@ -0,0 +1,6 @@
class AddActionsToValuators < ActiveRecord::Migration
def change
add_column :valuators, :can_comment, :boolean, default: true
add_column :valuators, :can_edit_dossier, :boolean, default: true
end
end

View File

@@ -1487,6 +1487,8 @@ ActiveRecord::Schema.define(version: 20190607160900) do
t.string "description"
t.integer "budget_investments_count", default: 0
t.integer "valuator_group_id"
t.boolean "can_comment", default: true
t.boolean "can_edit_dossier", default: true
t.index ["user_id"], name: "index_valuators_on_user_id", using: :btree
end

View File

@@ -88,7 +88,7 @@ describe "Valuator groups" do
visit edit_admin_valuator_path(valuator)
select "Health", from: "valuator_valuator_group_id"
click_button "Update valuator"
click_button "Update Valuator"
expect(page).to have_content "Valuator updated successfully"
expect(page).to have_content "Health"
@@ -102,7 +102,7 @@ describe "Valuator groups" do
visit edit_admin_valuator_path(valuator)
select "Economy", from: "valuator_valuator_group_id"
click_button "Update valuator"
click_button "Update Valuator"
expect(page).to have_content "Valuator updated successfully"
expect(page).to have_content "Economy"
@@ -115,7 +115,7 @@ describe "Valuator groups" do
visit edit_admin_valuator_path(valuator)
select "", from: "valuator_valuator_group_id"
click_button "Update valuator"
click_button "Update Valuator"
expect(page).to have_content "Valuator updated successfully"
expect(page).not_to have_content "Health"

View File

@@ -2,104 +2,112 @@ require "rails_helper"
describe "Admin valuators" do
let(:admin) { create(:administrator) }
let!(:user) { create(:user, username: "Jose Luis Balbin") }
let!(:valuator) { create(:valuator) }
before do
@admin = create(:administrator)
@user = create(:user, username: "Jose Luis Balbin")
@valuator = create(:valuator)
login_as(@admin.user)
login_as(admin.user)
visit admin_valuators_path
end
scenario "Show" do
visit admin_valuator_path(@valuator)
visit admin_valuator_path(valuator)
expect(page).to have_content @valuator.name
expect(page).to have_content @valuator.description
expect(page).to have_content @valuator.email
expect(page).to have_content valuator.name
expect(page).to have_content valuator.description
expect(page).to have_content valuator.email
expect(page).to have_content "Can comment, Can edit dossier"
end
scenario "Index" do
expect(page).to have_content(@valuator.name)
expect(page).to have_content(@valuator.email)
expect(page).not_to have_content(@user.name)
expect(page).to have_content(valuator.name)
expect(page).to have_content(valuator.email)
expect(page).not_to have_content(user.name)
end
scenario "Create", :js do
fill_in "name_or_email", with: @user.email
fill_in "name_or_email", with: user.email
click_button "Search"
expect(page).to have_content(@user.name)
expect(page).to have_content(user.name)
click_button "Add to valuators"
within("#valuators") do
expect(page).to have_content(@user.name)
expect(page).to have_content(user.name)
end
end
scenario "Edit" do
visit edit_admin_valuator_path(@valuator)
visit edit_admin_valuator_path(valuator)
expect(page).to have_field("Can create comments", checked: true)
expect(page).to have_field("Can edit dossiers", checked: true)
fill_in "valuator_description", with: "Valuator for health"
click_button "Update valuator"
uncheck "Can edit dossiers"
click_button "Update Valuator"
expect(page).to have_content "Valuator updated successfully"
expect(page).to have_content @valuator.email
expect(page).to have_content valuator.email
expect(page).to have_content "Valuator for health"
expect(page).to have_content "Can comment"
expect(page).not_to have_content "Can edit dossier"
end
scenario "Destroy" do
click_link "Delete"
within("#valuators") do
expect(page).not_to have_content(@valuator.name)
expect(page).not_to have_content(valuator.name)
end
end
context "Search" do
let!(:user1) { create(:user, username: "David Foster Wallace", email: "david@wallace.com") }
let!(:user2) { create(:user, username: "Steven Erikson", email: "steven@erikson.com") }
let!(:valuator1) { create(:valuator, user: user1) }
let!(:valuator2) { create(:valuator, user: user2) }
before do
user = create(:user, username: "David Foster Wallace", email: "david@wallace.com")
user2 = create(:user, username: "Steven Erikson", email: "steven@erikson.com")
@valuator1 = create(:valuator, user: user)
@valuator2 = create(:valuator, user: user2)
visit admin_valuators_path
end
scenario "returns no results if search term is empty" do
expect(page).to have_content(@valuator1.name)
expect(page).to have_content(@valuator2.name)
expect(page).to have_content(valuator1.name)
expect(page).to have_content(valuator2.name)
fill_in "name_or_email", with: " "
click_button "Search"
expect(page).to have_content("Valuators: User search")
expect(page).to have_content("No results found")
expect(page).not_to have_content(@valuator1.name)
expect(page).not_to have_content(@valuator2.name)
expect(page).not_to have_content(valuator1.name)
expect(page).not_to have_content(valuator2.name)
end
scenario "search by name" do
expect(page).to have_content(@valuator1.name)
expect(page).to have_content(@valuator2.name)
expect(page).to have_content(valuator1.name)
expect(page).to have_content(valuator2.name)
fill_in "name_or_email", with: "Foster"
click_button "Search"
expect(page).to have_content("Valuators: User search")
expect(page).to have_content(@valuator1.name)
expect(page).not_to have_content(@valuator2.name)
expect(page).to have_content(valuator1.name)
expect(page).not_to have_content(valuator2.name)
end
scenario "search by email" do
expect(page).to have_content(@valuator1.email)
expect(page).to have_content(@valuator2.email)
expect(page).to have_content(valuator1.email)
expect(page).to have_content(valuator2.email)
fill_in "name_or_email", with: @valuator2.email
fill_in "name_or_email", with: valuator2.email
click_button "Search"
expect(page).to have_content("Valuators: User search")
expect(page).to have_content(@valuator2.email)
expect(page).not_to have_content(@valuator1.email)
expect(page).to have_content(valuator2.email)
expect(page).not_to have_content(valuator1.email)
end
end

View File

@@ -44,4 +44,18 @@ describe Abilities::Valuator do
it { should_not be_able_to(:update, finished_assigned_investment) }
it { should_not be_able_to(:valuate, finished_assigned_investment) }
it "can update dossier information if not set can_edit_dossier attribute" do
should be_able_to(:edit_dossier, assigned_investment)
allow(valuator).to receive(:can_edit_dossier?).and_return(false)
ability = Ability.new(user)
expect(ability.can?(:edit_dossier, assigned_investment)).to be_falsey
end
it "cannot create valuation comments if not set not can_comment attribute" do
should be_able_to(:comment_valuation, assigned_investment)
allow(valuator).to receive(:can_comment?).and_return(false)
ability = Ability.new(user)
expect(ability.can?(:comment_valuation, assigned_investment)).to be_falsey
end
end

View File

@@ -50,4 +50,13 @@ describe Valuator do
expect(assigned_investment_ids).not_to include investment3.id
end
end
describe "abilities" do
context "by default" do
let(:valuator) { Valuator.new }
it { expect(valuator.can_comment).to be_truthy }
it { expect(valuator.can_edit_dossier).to be_truthy }
end
end
end