diff --git a/app/controllers/admin/officials_controller.rb b/app/controllers/admin/officials_controller.rb new file mode 100644 index 000000000..023f0f7fc --- /dev/null +++ b/app/controllers/admin/officials_controller.rb @@ -0,0 +1,32 @@ +class Admin::OfficialsController < Admin::BaseController + + def index + @officials = User.officials.page(params[:page]) + end + + def search + @users = User.with_email(params[:email]).page(params[:page]) + end + + def edit + @user = User.find(params[:id]) + end + + def update + @user = User.find(params[:id]) + @user.update(user_params) + redirect_to admin_officials_path, notice: t("admin.officials.flash.official_updated") + end + + def destroy + @official = User.officials.find(params[:id]) + @official.remove_official_position! + redirect_to admin_officials_path, notice: t("admin.officials.flash.official_destroyed") + end + + private + def user_params + params.require(:user).permit(:official_position, :official_level) + end + +end \ No newline at end of file diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index a7fef3aa2..7038ec389 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -4,6 +4,10 @@ module AdminHelper render "/#{namespace}/menu" end + def official_level_options + 1..5 + end + private def namespace diff --git a/app/models/user.rb b/app/models/user.rb index 857373d39..16d1de154 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,4 +41,8 @@ class User < ActiveRecord::Base def remove_official_position! update official_position: nil, official_level: 0 end + + def self.with_email(e) + e.present? ? where(email: e) : none + end end diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 8ea2b27af..8459fcab3 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -2,4 +2,5 @@
  • <%= link_to t('admin.menu.debate_topics'), admin_tags_path %>
  • <%= link_to t('admin.menu.hidden_debates'), admin_debates_path %>
  • <%= link_to t('admin.menu.hidden_comments'), admin_comments_path %>
  • +
  • <%= link_to t('admin.officials.index.title'), admin_officials_path %>
  • diff --git a/app/views/admin/officials/edit.html.erb b/app/views/admin/officials/edit.html.erb new file mode 100644 index 000000000..b986df2b5 --- /dev/null +++ b/app/views/admin/officials/edit.html.erb @@ -0,0 +1,14 @@ +

    <%= t("admin.officials.edit.title") %>

    + +<%= @user.name %> (<%= @user.email %>) +<%= form_for @user, url: admin_official_path(@user) do |f| %> + <%= f.text_field :official_position %> + <%= f.select :official_level, official_level_options %> + <%= f.submit %> + + <% if @user.official? %> + <%= link_to t("admin.officials.edit.destroy"), admin_official_path(@user), method: :delete, class: 'button tiny alert' %> + <% else %> + <%= link_to t("admin.officials.edit.cancel"), admin_officials_path, class: 'button tiny alert' %> + <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/admin/officials/index.html.erb b/app/views/admin/officials/index.html.erb new file mode 100644 index 000000000..779fce36b --- /dev/null +++ b/app/views/admin/officials/index.html.erb @@ -0,0 +1,25 @@ +

    <%= t("admin.officials.index.title") %>

    + +
    +<%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %> + <%= text_field_tag :email, "", label: false, placeholder: t("admin.officials.index.search_email_placeholder") %> + <%= f.submit t("admin.officials.index.search") %> +<% end %> +
    + +
    +<%= page_entries_info @officials %> +
    + +
    +<% @officials.each do |official| %> + <%= link_to official.name, edit_admin_official_path(official) %> + <%= official.official_position %> + <%= official.official_level %> +

    +<% end %> +
    + +
    +<%= paginate @officials %> +
    \ No newline at end of file diff --git a/app/views/admin/officials/search.html.erb b/app/views/admin/officials/search.html.erb new file mode 100644 index 000000000..8f7f9e670 --- /dev/null +++ b/app/views/admin/officials/search.html.erb @@ -0,0 +1,21 @@ +

    <%= t("admin.officials.search.title") %>

    + +
    +<%= form_for(User.new, url: search_admin_officials_path, as: :user, method: :get) do |f| %> + <%= text_field_tag :email, "", label: false, placeholder: t("admin.officials.index.search_email_placeholder") %> + <%= f.submit t("admin.officials.search.search") %> +<% end %> +
    + +
    +<%= page_entries_info @users %> +
    + +
    +<% @users.each do |user| %> + <%= link_to user.name, edit_admin_official_path(user) %> + <%= user.official_position %> + <%= user.official_level %> + <%= link_to user.official? ? t("admin.officials.search.edit_official") : t("admin.officials.search.make_official"), edit_admin_official_path(user) %> +<% end %> +
    \ No newline at end of file diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 42c6d375c..660c7015b 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -29,4 +29,20 @@ en: title: Hidden debates restore: success: The debate has been restored - + officials: + index: + title: Officials + search_email_placeholder: 'Search user by email' + search: Search + search: + title: 'Officials: Search users' + edit_official: Edit official + make_official: Make this user an official + search: Search + edit: + title: 'Officials: edit user' + destroy: "Remove 'Official' condition" + cancel: "Cancel" + flash: + official_updated: 'Official position saved!' + official_destroyed: 'User is not an official anymore' diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 71062c25b..ee36b04a8 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -29,3 +29,20 @@ es: title: Debates ocultos restore: success: El debate ha sido permitido + officials: + index: + title: Cargos Públicos + search_email_placeholder: 'Buscar usuario por email' + search: Buscar + search: + title: 'Cargos Públicos: Búsqueda de usuarios' + edit_official: Editar cargo público + make_official: Convertir en cargo público + search: Buscar + edit: + title: 'Cargos Públicos: Editar usuario' + destroy: "Eliminar condición de 'Cargo Público'" + cancel: "Cancelar" + flash: + official_updated: 'Datos del cargo público guardados' + official_destroyed: 'Datos guardados: el usuario ya no es cargo público' diff --git a/config/routes.rb b/config/routes.rb index 27f558e69..d7ccc5cb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,6 +29,9 @@ Rails.application.routes.draw do end resources :tags, only: [:index, :create, :update, :destroy] + resources :officials, only: [:index, :edit, :update, :destroy] do + collection { get :search} + end end namespace :moderation do diff --git a/spec/features/admin/officials_spec.rb b/spec/features/admin/officials_spec.rb new file mode 100644 index 000000000..6824ccf62 --- /dev/null +++ b/spec/features/admin/officials_spec.rb @@ -0,0 +1,78 @@ +require 'rails_helper' + +feature 'Admin officials' do + + background do + @citizen = create(:user, first_name: "Citizen", last_name: "Kane") + @official = create(:user, official_position: "Mayor", official_level: 5) + @admin = create(:administrator) + login_as(@admin.user) + end + + scenario 'Index' do + visit admin_officials_path + + expect(page).to have_content @official.name + expect(page).to_not have_content @citizen.name + expect(page).to have_content @official.official_position + expect(page).to have_content @official.official_level + end + + scenario 'Edit an official' do + visit admin_officials_path + click_link @official.name + + expect(current_path).to eq(edit_admin_official_path(@official)) + + expect(page).to_not have_content @citizen.name + expect(page).to have_content @official.name + expect(page).to have_content @official.email + + fill_in 'user_official_position', with: 'School Teacher' + select '3', from: 'user_official_level' + click_button 'Update User' + + expect(page).to have_content 'Official position saved!' + + visit admin_officials_path + + expect(page).to have_content @official.name + expect(page).to have_content 'School Teacher' + expect(page).to have_content '3' + end + + scenario 'Create an official' do + visit admin_officials_path + fill_in 'email', with: @citizen.email + click_button 'Search' + + expect(current_path).to eq(search_admin_officials_path) + expect(page).to_not have_content @official.name + + click_link @citizen.name + + fill_in 'user_official_position', with: 'Hospital manager' + select '4', from: 'user_official_level' + click_button 'Update User' + + expect(page).to have_content 'Official position saved!' + + visit admin_officials_path + + expect(page).to have_content @official.name + expect(page).to have_content @citizen.name + expect(page).to have_content 'Hospital manager' + expect(page).to have_content '4' + end + + scenario 'Destroy' do + visit edit_admin_official_path(@official) + + click_link "Remove 'Official' condition" + + expect(page).to have_content 'User is not an official anymore' + expect(current_path).to eq(admin_officials_path) + expect(page).to_not have_content @citizen.name + expect(page).to_not have_content @official.name + end +end \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 29f84e406..2d8d83562 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -169,4 +169,18 @@ describe User do end end + describe "self.with_email" do + it "find users by email" do + user1 = create(:user, email: "larry@madrid.es") + user2 = create(:user, email: "bird@madrid.es") + search = User.with_email("larry@madrid.es") + expect(search.size).to eq(1) + expect(search.first).to eq(user1) + end + + it "returns no results if no email provided" do + expect(User.with_email(" ").size).to eq(0) + end + end + end