Merge pull request #3389 from LextrendIT/feature/add_description_field_to_administrator_users_like_evaluators

Add description field to administrator users like evaluators description
This commit is contained in:
Javier Martín
2019-06-05 18:36:16 +02:00
committed by GitHub
21 changed files with 265 additions and 57 deletions

View File

@@ -28,4 +28,22 @@ class Admin::AdministratorsController < Admin::BaseController
redirect_to admin_administrators_path
end
def edit
end
def update
if @administrator.update(update_administrator_params)
notice = t("admin.administrators.form.updated")
redirect_to admin_administrators_path, notice: notice
else
render :edit
end
end
private
def update_administrator_params
params.require(:administrator).permit(:description)
end
end

View File

@@ -77,7 +77,9 @@ module AdminHelper
end
def admin_select_options
Administrator.all.order("users.username asc").includes(:user).collect { |v| [ v.name, v.id ] }
Administrator.with_user
.collect { |v| [ v.description_or_name, v.id ] }
.sort_by { |a| a[0] }
end
def admin_submit_action(resource)

View File

@@ -51,7 +51,7 @@ module Abilities
can :comment_as_administrator, [Debate, Comment, Proposal, Poll::Question, Budget::Investment,
Legislation::Question, Legislation::Proposal, Legislation::Annotation, Topic]
can [:search, :create, :index, :destroy], ::Administrator
can [:search, :create, :index, :destroy, :edit, :update], ::Administrator
can [:search, :create, :index, :destroy], ::Moderator
can [:search, :show, :edit, :update, :create, :index, :destroy, :summary], ::Valuator
can [:search, :create, :index, :destroy], ::Manager

View File

@@ -3,4 +3,14 @@ class Administrator < ApplicationRecord
delegate :name, :email, :name_and_email, to: :user
validates :user_id, presence: true, uniqueness: true
scope :with_user, -> { includes(:user) }
def description_or_name
description.presence || name
end
def description_or_name_and_email
"#{description_or_name} (#{email})"
end
end

View File

@@ -0,0 +1,15 @@
<%= back_link_to admin_administrators_path %>
<h2><%= t("admin.administrators.form.edit_title") %></h2>
<div class="callout highlight">
<strong><%= @administrator.name %></strong><br>
<%= @administrator.email %>
</div>
<div class="margin-top">
<%= form_for [:admin, @administrator] do |f| %>
<%= f.text_field :description %>
<%= f.submit class: "button success" %>
<% end %>
</div>

View File

@@ -11,6 +11,7 @@
<th scope="col" class="text-center"><%= t("admin.administrators.index.id") %></th>
<th scope="col"><%= t("admin.administrators.index.name") %></th>
<th scope="col"><%= t("admin.administrators.index.email") %></th>
<th scope="col"><%= t("admin.administrators.index.description") %></th>
<th scope="col" class="small-3"><%= t("admin.shared.actions") %></th>
</thead>
<% @administrators.each do |administrator| %>
@@ -24,13 +25,18 @@
<td>
<%= administrator.email %>
</td>
<td>
<%= administrator.description %>
</td>
<td>
<% if administrator.persisted? %>
<%= link_to t("admin.actions.edit"),
edit_admin_administrator_path(administrator),
class: "button hollow" %>
<%= link_to t("admin.administrators.administrator.delete"),
admin_administrator_path(administrator),
method: :delete,
class: "button hollow alert expanded"
%>
class: "button hollow alert" %>
<% else %>
<%= link_to t("admin.administrators.administrator.add"),
{ controller: "admin/administrators", action: :create,

View File

@@ -13,9 +13,9 @@
</td>
<td class="small">
<% if investment.administrator.present? %>
<span title="<%= t("admin.budget_investments.index.assigned_admin") %>">
<%= investment.administrator.name %>
</span>
<span title="<%= t("admin.budget_investments.index.assigned_admin") %>">
<%= investment.administrator.description_or_name %>
</span>
<% else %>
<%= t("admin.budget_investments.index.no_admin_assigned") %>
<% end %>

View File

@@ -46,7 +46,7 @@
<div class="small-12 medium-6">
<%= f.select(:administrator_id,
@admins.collect{ |a| [a.name_and_email, a.id ] },
@admins.collect{ |a| [a.description_or_name_and_email, a.id ] },
{ include_blank: t("admin.budget_investments.edit.undefined") }) %>
</div>
</div>

View File

@@ -3,7 +3,8 @@
<% allow_votes = local_assigns.fetch(:allow_votes, true) %>
<% allow_actions = local_assigns.fetch(:allow_actions, true) %>
<% allow_comments = local_assigns.fetch(:allow_comments, true) %>
<% cache [locale_and_user_status(comment), comment, commentable_cache_key(comment.commentable), comment.author, (comment_flags[comment.id] if comment_flags)] do %>
<% admin_layout = local_assigns.fetch(:admin_layout, false) %>
<% cache [locale_and_user_status(comment), comment, commentable_cache_key(comment.commentable), comment.author, (comment_flags[comment.id] if comment_flags), (admin_layout if admin_layout)] do %>
<ul id="<%= dom_id(comment) %>" class="comment no-bullet small-12">
<li class="comment-body">
<% if comment.hidden? || comment.user.hidden? %>
@@ -30,7 +31,14 @@
<div class="comment-info">
<% if comment.as_administrator? %>
<span class="user-name"><%= t("comments.comment.admin") %> #<%= comment.administrator_id %></span>
<span class="user-name">
<%= t("comments.comment.admin") %>
<% if admin_layout %>
<%= Administrator.find(comment.administrator_id).description_or_name %>
<% else %>
#<%= comment.administrator_id %>
<% end %>
</span>
<% elsif comment.as_moderator? %>
<span class="user-name"><%= t("comments.comment.moderator") %> #<%= comment.moderator_id %></span>
<% else %>

View File

@@ -1,7 +1,8 @@
<% commentable = comment_tree.commentable %>
<% valuation = local_assigns.fetch(:valuation, false) %>
<% allow_comments = local_assigns.fetch(:allow_comments, true) %>
<% 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 %>
<% admin_layout = local_assigns.fetch(:admin_layout, 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, admin_layout] do %>
<section class="expanded comments">
<div class="row">
<div id="comments" class="small-12 column">
@@ -46,7 +47,8 @@
valuation: valuation,
allow_votes: !valuation,
allow_actions: !valuation,
allow_comments: allow_comments } %>
allow_comments: allow_comments,
admin_layout: admin_layout } %>
<% end %>
<%= paginate comment_tree.root_comments %>
</div>

View File

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

View File

@@ -728,6 +728,7 @@ en:
title: Administrators
name: Name
email: Email
description: Description
id: Administrator ID
no_administrators: There are no administrators.
administrator:
@@ -736,6 +737,9 @@ en:
restricted_removal: "Sorry, you can't remove yourself from the administrators"
search:
title: "Administrators: User search"
form:
edit_title: "Edit administrator"
updated: "Administrator updated successfully"
moderators:
index:
title: Moderators

View File

@@ -126,6 +126,8 @@ es:
one: Enlace
other: Enlaces
attributes:
administrator:
description: Descripción
budget:
name: "Nombre"
description_accepting: "Descripción durante la fase de presentación de proyectos"

View File

@@ -727,6 +727,7 @@ es:
title: Administradores
name: Nombre
email: Email
description: Descripción
id: ID de Administrador
no_administrators: No hay administradores.
administrator:
@@ -735,6 +736,9 @@ es:
restricted_removal: "Lo sentimos, no puedes eliminarte a ti mismo de la lista"
search:
title: "Administradores: Búsqueda de usuarios"
form:
edit_title: "Editar administrador"
updated: "Administrador actualizado correctamente"
moderators:
index:
title: Moderadores

View File

@@ -106,7 +106,7 @@ namespace :admin do
get :search, on: :collection
end
resources :administrators, only: [:index, :create, :destroy] do
resources :administrators, only: [:index, :create, :destroy, :edit, :update] do
get :search, on: :collection
end

View File

@@ -0,0 +1,5 @@
class AddDescriptionToAdministrator < ActiveRecord::Migration
def change
add_column :administrators, :description, :string
end
end

View File

@@ -67,6 +67,7 @@ ActiveRecord::Schema.define(version: 20190429125842) do
create_table "administrators", force: :cascade do |t|
t.integer "user_id"
t.string "description"
t.index ["user_id"], name: "index_administrators_on_user_id", using: :btree
end

View File

@@ -3,7 +3,7 @@ require "rails_helper"
describe "Admin administrators" do
let!(:admin) { create(:administrator) }
let!(:user) { create(:user, username: "Jose Luis Balbin") }
let!(:user_administrator) { create(:administrator) }
let!(:user_administrator) { create(:administrator, description: "admin_alias") }
before do
login_as(admin.user)
@@ -14,6 +14,7 @@ describe "Admin administrators" do
expect(page).to have_content user_administrator.id
expect(page).to have_content user_administrator.name
expect(page).to have_content user_administrator.email
expect(page).to have_content user_administrator.description
expect(page).not_to have_content user.name
end
@@ -100,4 +101,19 @@ describe "Admin administrators" do
end
end
context "Edit" do
let!(:administrator1) { create(:administrator, user: create(:user,
username: "Bernard Sumner",
email: "bernard@sumner.com")) }
scenario "admin can edit administrator1" do
visit(edit_admin_administrator_path(administrator1))
fill_in "administrator_description", with: "Admin Alias"
click_button "Update Administrator"
expect(page).to have_content("Administrator updated successfully")
expect(page).to have_content("Admin Alias")
end
end
end

View File

@@ -149,8 +149,9 @@ describe "Admin budget investments" do
scenario "Filtering by admin", :js do
user = create(:user, username: "Admin 1")
user2 = create(:user, username: "Admin 2")
administrator = create(:administrator, user: user)
create(:administrator, user: user2, description: "Alias")
create(:budget_investment, title: "Realocate visitors", budget: budget,
administrator: administrator)
create(:budget_investment, title: "Destroy the city", budget: budget)
@@ -166,6 +167,13 @@ describe "Admin budget investments" do
expect(page).not_to have_link("Destroy the city")
expect(page).to have_link("Realocate visitors")
select "Alias", from: "administrator_id"
click_button "Filter"
expect(page).to have_content("There are no investment projects")
expect(page).not_to have_link("Destroy the city")
expect(page).not_to have_link("Realocate visitors")
select "All administrators", from: "administrator_id"
click_button "Filter"
@@ -1066,12 +1074,12 @@ describe "Admin budget investments" do
scenario "Add administrator" do
budget_investment = create(:budget_investment)
user = create(:user, username: "Marta", email: "marta@admins.org")
create(:administrator, user: user)
create(:administrator, user: user, description: "Marta desc")
visit admin_budget_budget_investment_path(budget_investment.budget, budget_investment)
click_link "Edit classification"
select "Marta (marta@admins.org)", from: "budget_investment[administrator_id]"
select "Marta desc (marta@admins.org)", from: "budget_investment[administrator_id]"
click_button "Update"
expect(page).to have_content "Investment project updated succesfully."

View File

@@ -382,49 +382,109 @@ describe "Commenting Budget::Investments" do
end
describe "Administrators" do
scenario "can create comment as an administrator", :js do
admin = create(:administrator)
context "comment as administrator" do
scenario "can create comment", :js do
admin = create(:administrator)
login_as(admin.user)
visit budget_investment_path(investment.budget, investment)
login_as(admin.user)
visit budget_investment_path(investment.budget, investment)
fill_in "comment-body-budget_investment_#{investment.id}", with: "I am your Admin!"
check "comment-as-administrator-budget_investment_#{investment.id}"
click_button "Publish comment"
fill_in "comment-body-budget_investment_#{investment.id}", with: "I am your Admin!"
check "comment-as-administrator-budget_investment_#{investment.id}"
click_button "Publish comment"
within "#comments" do
expect(page).to have_content "I am your Admin!"
expect(page).to have_content "Administrator ##{admin.id}"
within "#comments" do
expect(page).to have_content "I am your Admin!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "div.is-admin"
expect(page).to have_css "img.admin-avatar"
end
end
scenario "display administrator description on admin views", :js do
admin = create(:administrator, description: "user description")
login_as(admin.user)
visit admin_budget_budget_investment_path(investment.budget, investment)
fill_in "comment-body-budget_investment_#{investment.id}", with: "I am your Admin!"
check "comment-as-administrator-budget_investment_#{investment.id}"
click_button "Publish comment"
visit admin_budget_budget_investment_path(investment.budget, investment)
within "#comments" do
expect(page).to have_content "I am your Admin!"
expect(page).to have_content "Administrator user description"
expect(page).to have_css "div.is-admin"
expect(page).to have_css "img.admin-avatar"
end
end
scenario "display administrator id on public views", :js do
admin = create(:administrator, description: "user description")
login_as(admin.user)
visit admin_budget_budget_investment_path(investment.budget, investment)
fill_in "comment-body-budget_investment_#{investment.id}", with: "I am your Admin!"
check "comment-as-administrator-budget_investment_#{investment.id}"
click_button "Publish comment"
within "#comments" do
expect(page).to have_content "I am your Admin!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "div.is-admin"
expect(page).to have_css "img.admin-avatar"
end
end
scenario "can create reply as an administrator", :js do
citizen = create(:user, username: "Ana")
manuela = create(:user, username: "Manuela")
admin = create(:administrator, user: manuela)
comment = create(:comment, commentable: investment, user: citizen)
login_as(manuela)
visit budget_investment_path(investment.budget, investment)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment-body-comment_#{comment.id}", with: "Top of the world!"
check "comment-as-administrator-comment_#{comment.id}"
click_button "Publish reply"
end
within "#comment_#{comment.id}" do
expect(page).to have_content "Top of the world!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "img.admin-avatar"
end
expect(page).not_to have_selector("#js-comment-form-comment_#{comment.id}", visible: true)
expect(page).to have_css "div.is-admin"
expect(page).to have_css "img.admin-avatar"
end
end
scenario "can create reply as an administrator", :js do
citizen = create(:user, username: "Ana")
manuela = create(:user, username: "Manuela")
admin = create(:administrator, user: manuela)
comment = create(:comment, commentable: investment, user: citizen)
login_as(manuela)
visit budget_investment_path(investment.budget, investment)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment-body-comment_#{comment.id}", with: "Top of the world!"
check "comment-as-administrator-comment_#{comment.id}"
click_button "Publish reply"
end
within "#comment_#{comment.id}" do
expect(page).to have_content "Top of the world!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "div.is-admin"
expect(page).to have_css "img.admin-avatar"
scenario "public users not see admin description", :js do
manuela = create(:user, username: "Manuela")
admin = create(:administrator, user: manuela)
comment = create(:comment,
commentable: investment,
user: manuela,
administrator_id: admin.id)
visit budget_investment_path(investment.budget, investment)
within "#comment_#{comment.id}" do
expect(page).to have_content comment.body
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "img.admin-avatar"
expect(page).to have_css "div.is-admin"
end
end
expect(page).not_to have_selector("#js-comment-form-comment_#{comment.id}", visible: true)
end
scenario "can not comment as a moderator" do

View File

@@ -0,0 +1,45 @@
require "rails_helper"
describe Administrator do
describe "#description_or_name" do
let!(:user) { create(:user, username: "Billy Wilder" )}
it "returns description if present" do
administrator = create(:administrator, user: user, description: "John Doe")
expect(administrator.description_or_name).to eq("John Doe")
end
it "returns name if description is nil" do
administrator = create(:administrator, user: user)
expect(administrator.description_or_name).to eq("Billy Wilder")
end
it "returns name if description is blank" do
administrator = create(:administrator, description: "")
expect(administrator.description_or_name).to eq(administrator.name)
end
end
describe "#description_or_name_and_email" do
let!(:user) { create(:user, username: "Billy Wilder", email: "test@test.com")}
it "returns description and email if decription present" do
administrator = create(:administrator,
description: "John Doe",
user: user)
expect(administrator.description_or_name_and_email).to eq("John Doe (test@test.com)")
end
it "returns name and email if description is not present" do
administrator = create(:administrator, user: user)
expect(administrator.description_or_name_and_email).to eq("Billy Wilder (test@test.com)")
end
end
end