diff --git a/app/controllers/admin/administrators_controller.rb b/app/controllers/admin/administrators_controller.rb
index 3467ee0c0..5e423be81 100644
--- a/app/controllers/admin/administrators_controller.rb
+++ b/app/controllers/admin/administrators_controller.rb
@@ -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
diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb
index e0dc3834f..bad9af389 100644
--- a/app/helpers/admin_helper.rb
+++ b/app/helpers/admin_helper.rb
@@ -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)
diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb
index c37e06f65..c0e723fff 100644
--- a/app/models/abilities/administrator.rb
+++ b/app/models/abilities/administrator.rb
@@ -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
diff --git a/app/models/administrator.rb b/app/models/administrator.rb
index b8b6cc1c0..1c5cb027a 100644
--- a/app/models/administrator.rb
+++ b/app/models/administrator.rb
@@ -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
diff --git a/app/views/admin/administrators/edit.html.erb b/app/views/admin/administrators/edit.html.erb
new file mode 100644
index 000000000..e9374ee78
--- /dev/null
+++ b/app/views/admin/administrators/edit.html.erb
@@ -0,0 +1,15 @@
+<%= back_link_to admin_administrators_path %>
+
+
<%= t("admin.administrators.form.edit_title") %>
+
+
+ <%= @administrator.name %>
+ <%= @administrator.email %>
+
+
+
+ <%= form_for [:admin, @administrator] do |f| %>
+ <%= f.text_field :description %>
+ <%= f.submit class: "button success" %>
+ <% end %>
+
diff --git a/app/views/admin/administrators/index.html.erb b/app/views/admin/administrators/index.html.erb
index c727114bf..469905279 100644
--- a/app/views/admin/administrators/index.html.erb
+++ b/app/views/admin/administrators/index.html.erb
@@ -11,6 +11,7 @@
<%= t("admin.administrators.index.id") %> |
<%= t("admin.administrators.index.name") %> |
<%= t("admin.administrators.index.email") %> |
+ <%= t("admin.administrators.index.description") %> |
<%= t("admin.shared.actions") %> |
<% @administrators.each do |administrator| %>
@@ -24,13 +25,18 @@
<%= administrator.email %>
|
+
+ <%= administrator.description %>
+ |
<% 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,
diff --git a/app/views/admin/budget_investments/_select_investment.html.erb b/app/views/admin/budget_investments/_select_investment.html.erb
index 788d9824b..2da7a3817 100644
--- a/app/views/admin/budget_investments/_select_investment.html.erb
+++ b/app/views/admin/budget_investments/_select_investment.html.erb
@@ -13,9 +13,9 @@
|
<% if investment.administrator.present? %>
- ">
- <%= investment.administrator.name %>
-
+ ">
+ <%= investment.administrator.description_or_name %>
+
<% else %>
<%= t("admin.budget_investments.index.no_admin_assigned") %>
<% end %>
diff --git a/app/views/admin/budget_investments/edit.html.erb b/app/views/admin/budget_investments/edit.html.erb
index 20e3c5e56..8d2ff842f 100644
--- a/app/views/admin/budget_investments/edit.html.erb
+++ b/app/views/admin/budget_investments/edit.html.erb
@@ -46,7 +46,7 @@
<%= 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") }) %>
diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb
index 9fc0f875b..8e72d6c53 100644
--- a/app/views/comments/_comment.html.erb
+++ b/app/views/comments/_comment.html.erb
@@ -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 %>
|
<%= t("valuation.budget_investments.valuation_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, - 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 %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index e3c5c597b..0ad7312e9 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -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 diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index e3bd118f7..4e5b8acd8 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -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" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 34fc7510b..291051a9d 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -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 diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 39ed9b44b..148138d61 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -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 diff --git a/db/migrate/20190314150724_add_description_to_administrator.rb b/db/migrate/20190314150724_add_description_to_administrator.rb new file mode 100644 index 000000000..d2058bb7f --- /dev/null +++ b/db/migrate/20190314150724_add_description_to_administrator.rb @@ -0,0 +1,5 @@ +class AddDescriptionToAdministrator < ActiveRecord::Migration + def change + add_column :administrators, :description, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 5178cec3c..8f83dfffb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/features/admin/administrators_spec.rb b/spec/features/admin/administrators_spec.rb index a782e39d3..3ba76eaac 100644 --- a/spec/features/admin/administrators_spec.rb +++ b/spec/features/admin/administrators_spec.rb @@ -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 diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index 8dd9743d3..ed96e7140 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -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." diff --git a/spec/features/comments/budget_investments_spec.rb b/spec/features/comments/budget_investments_spec.rb index b5f064d93..5aeb77f7f 100644 --- a/spec/features/comments/budget_investments_spec.rb +++ b/spec/features/comments/budget_investments_spec.rb @@ -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 diff --git a/spec/models/administrator_spec.rb b/spec/models/administrator_spec.rb new file mode 100644 index 000000000..39b740980 --- /dev/null +++ b/spec/models/administrator_spec.rb @@ -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