From 982dc180cc2d952837202f76748386764ed1d4ec Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Mon, 24 Aug 2015 01:50:01 +0100 Subject: [PATCH 1/4] Adds moderators config to admin console --- Gemfile.lock | 3 --- .../admin/moderators_controller.rb | 18 +++++++++++++ app/models/user.rb | 4 +++ app/views/admin/_menu.html.erb | 7 ++++++ .../admin/moderators/_moderator.html.erb | 18 +++++++++++++ app/views/admin/moderators/index.html.erb | 25 +++++++++++++++++++ app/views/admin/moderators/search.js.erb | 1 + config/locales/admin.en.yml | 8 ++++++ config/locales/admin.es.yml | 8 ++++++ config/routes.rb | 4 +++ spec/features/admin/moderators_spec.rb | 15 +++++++++++ 11 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/moderators_controller.rb create mode 100644 app/views/admin/moderators/_moderator.html.erb create mode 100644 app/views/admin/moderators/index.html.erb create mode 100644 app/views/admin/moderators/search.js.erb create mode 100644 spec/features/admin/moderators_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 2753c16ba..4bc94dbf2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -378,6 +378,3 @@ DEPENDENCIES uglifier (>= 1.3.0) unicorn web-console (~> 2.0) - -BUNDLED WITH - 1.10.6 diff --git a/app/controllers/admin/moderators_controller.rb b/app/controllers/admin/moderators_controller.rb new file mode 100644 index 000000000..350b4949c --- /dev/null +++ b/app/controllers/admin/moderators_controller.rb @@ -0,0 +1,18 @@ +class Admin::ModeratorsController < Admin::BaseController + def index + @moderators = User.joins(:moderator).page(params[:page]) + end + def search + @email = params[:email] + @user = User.find_by(email: @email) + + respond_to do |format| + format.js + end + end + def toggle + @user = User.find(params[:id]) + @user.toggle_moderator + redirect_to admin_moderators_path + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 52287d9f4..3ecf0d1f7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -70,6 +70,10 @@ class User < ActiveRecord::Base e.present? ? where(email: e) : none end + def toggle_moderator + moderator? ? self.moderator.destroy : create_moderator + end + private def use_first_name? !organization? && !use_nickname? diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index bc2d459e2..252769752 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -46,6 +46,13 @@ <% end %> +
  • > + <%= link_to admin_moderators_path do %> + + <%= t('admin.menu.moderators') %> + <% end %> +
  • +
  • > <%= link_to admin_settings_path do %> diff --git a/app/views/admin/moderators/_moderator.html.erb b/app/views/admin/moderators/_moderator.html.erb new file mode 100644 index 000000000..de351c160 --- /dev/null +++ b/app/views/admin/moderators/_moderator.html.erb @@ -0,0 +1,18 @@ +<% if moderator %> + <%= moderator.name %> +  •  + <%= moderator.email %> + <% if moderator.moderator? %> + <%= link_to t('admin.moderators.moderator.delete'), + toggle_admin_moderator_path(moderator), + method: :post, + class: "button tiny radius alert right" %> + <% else %> + <%= link_to t('admin.moderators.moderator.add'), + toggle_admin_moderator_path(moderator), + method: :post, + class: "button tiny radius success right" %> + <% end %> +<% else%> + <%= t('admin.moderators.search.user_not_found') %> +<% end %> diff --git a/app/views/admin/moderators/index.html.erb b/app/views/admin/moderators/index.html.erb new file mode 100644 index 000000000..8a1f8e40d --- /dev/null +++ b/app/views/admin/moderators/index.html.erb @@ -0,0 +1,25 @@ +

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

    +
    +
    + <%= form_tag search_admin_moderators_path, method: :get, remote: true do %> +
    + <%= text_field_tag :email, '', placeholder: t('admin.moderators.search.email_placeholder') %> +
    +
    + <%= submit_tag t('admin.moderators.search.search'), class: 'button radius' %> +
    + <% end %> +
    +
    +
    +
    +

    <%= page_entries_info @moderators %>

    + + +<%= paginate @moderators %> diff --git a/app/views/admin/moderators/search.js.erb b/app/views/admin/moderators/search.js.erb new file mode 100644 index 000000000..30d9f8a5d --- /dev/null +++ b/app/views/admin/moderators/search.js.erb @@ -0,0 +1 @@ +$("#search-result").html("
    <%= j render 'moderator', moderator: @user %>
    "); diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index cdf6774db..2c7565d62 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -16,6 +16,7 @@ en: hidden_users: Hidden users organizations: Organizations officials: Officials + moderators: Moderators stats: Statistics organizations: index: @@ -91,3 +92,10 @@ en: flash: official_updated: 'Official position saved!' official_destroyed: 'User is not an official anymore' + moderators: + index: + title: Moderators + search: + email_placeholder: 'Search user by email' + search: Search + user_not_found: 'User not found' diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 02f0ef65a..dc3213cbd 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -16,6 +16,7 @@ es: hidden_users: Usuarios ocultos organizations: Organizaciones officials: Cargos públicos + moderators: Moderadores stats: Estadísticas organizations: index: @@ -91,3 +92,10 @@ es: flash: official_updated: 'Datos del cargo público guardados' official_destroyed: 'Datos guardados: el usuario ya no es cargo público' + moderators: + index: + title: Moderadores + search: + email_placeholder: 'Buscar usuario por email' + search: Buscar + user_not_found: 'Usuario no encontrado' diff --git a/config/routes.rb b/config/routes.rb index 7dbe31613..0aff6bfd3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,10 @@ Rails.application.routes.draw do end resources :settings, only: [:index, :update] + resources :moderators, only: [:index] do + member { post :toggle } + collection { get :search } + end end namespace :moderation do diff --git a/spec/features/admin/moderators_spec.rb b/spec/features/admin/moderators_spec.rb new file mode 100644 index 000000000..f218779b0 --- /dev/null +++ b/spec/features/admin/moderators_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +feature 'Admin moderators' do + background do + @user = create(:user) + @moderator = create(:moderator) + @admin = create(:administrator) + login_as(@admin) + end + + scenario 'Index' do + visit admin_moderators_path + end +end + From 84e0e8f9909786ae807f1469e1fae461df79d6a4 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Mon, 24 Aug 2015 23:04:10 +0100 Subject: [PATCH 2/4] =?UTF-8?q?Adds=20testing=20for=20moderator=C2=B4s=20a?= =?UTF-8?q?dmin=20consol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/locales/admin.en.yml | 4 ++++ config/locales/admin.es.yml | 3 +++ spec/features/admin/moderators_spec.rb | 25 +++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 2c7565d62..1783ff4f6 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -99,3 +99,7 @@ en: email_placeholder: 'Search user by email' search: Search user_not_found: 'User not found' + moderator: + delete: Delete + add: Add + diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index dc3213cbd..ae7c4a23b 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -99,3 +99,6 @@ es: email_placeholder: 'Buscar usuario por email' search: Buscar user_not_found: 'Usuario no encontrado' + moderator: + delete: Borrar + add: Añadir diff --git a/spec/features/admin/moderators_spec.rb b/spec/features/admin/moderators_spec.rb index f218779b0..c94373e93 100644 --- a/spec/features/admin/moderators_spec.rb +++ b/spec/features/admin/moderators_spec.rb @@ -2,14 +2,35 @@ require 'rails_helper' feature 'Admin moderators' do background do - @user = create(:user) + @user = create(:user, first_name: 'Jose Luis', last_name: 'Balbin' ) @moderator = create(:moderator) @admin = create(:administrator) - login_as(@admin) + login_as(@admin.user) end scenario 'Index' do visit admin_moderators_path + expect(page).to have_content @moderator.name + expect(page).to have_content @moderator.email + expect(page).to_not have_content @user.name + end + + scenario 'Create Moderator', :js do + visit admin_moderators_path + fill_in 'email', with: @user.email + click_button 'Search' + + expect(page).to have_content @user.name + click_link 'Add' + + expect(page).to have_content @user.name + end + + scenario 'Delete Moderator' do + visit admin_moderators_path + click_link 'Delete' + + expect(page).to_not have_content @moderator.name end end From 4c71577da61190b12c7d0ebbba711f4909c25ab6 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Tue, 25 Aug 2015 01:14:55 +0100 Subject: [PATCH 3/4] Fixes moderators tests after merge --- db/schema.rb | 2 +- spec/features/admin/moderators_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 12e457a1c..6f4d26c5b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -158,8 +158,8 @@ ActiveRecord::Schema.define(version: 20150824113326) do t.boolean "email_on_comment_reply", default: false t.string "official_position" t.integer "official_level", default: 0 - t.datetime "hidden_at" t.string "phone_number", limit: 30 + t.datetime "hidden_at" t.string "username" end diff --git a/spec/features/admin/moderators_spec.rb b/spec/features/admin/moderators_spec.rb index c94373e93..bdbd23b05 100644 --- a/spec/features/admin/moderators_spec.rb +++ b/spec/features/admin/moderators_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' feature 'Admin moderators' do background do - @user = create(:user, first_name: 'Jose Luis', last_name: 'Balbin' ) + @user = create(:user, username: 'Jose Luis Balbin') @moderator = create(:moderator) @admin = create(:administrator) login_as(@admin.user) From e21336aeff90cdd3a81ca3adde50d7e39bf50de3 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Sat, 29 Aug 2015 01:39:46 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Refactored=20admin=C2=B4s=20=20moderator=20?= =?UTF-8?q?controller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/moderators_controller.rb | 29 ++++++++++++++----- app/models/ability.rb | 2 ++ app/models/user.rb | 4 --- .../admin/moderators/_moderator.html.erb | 29 ++++++++----------- app/views/admin/moderators/index.html.erb | 3 +- app/views/admin/moderators/search.js.erb | 2 +- .../admin/moderators/user_not_found.js.erb | 1 + config/routes.rb | 3 +- db/schema.rb | 10 +++---- 9 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 app/views/admin/moderators/user_not_found.js.erb diff --git a/app/controllers/admin/moderators_controller.rb b/app/controllers/admin/moderators_controller.rb index 350b4949c..f2ef6f1c3 100644 --- a/app/controllers/admin/moderators_controller.rb +++ b/app/controllers/admin/moderators_controller.rb @@ -1,18 +1,33 @@ class Admin::ModeratorsController < Admin::BaseController + + load_and_authorize_resource + def index - @moderators = User.joins(:moderator).page(params[:page]) + @moderators = @moderators.page(params[:page]) end + def search - @email = params[:email] - @user = User.find_by(email: @email) + @user = User.find_by(email: params[:email]) respond_to do |format| - format.js + if @user + @moderator = Moderator.find_or_initialize_by(user: @user) + format.js + else + format.js { render "user_not_found" } + end end end - def toggle - @user = User.find(params[:id]) - @user.toggle_moderator + + def destroy + @moderator.destroy + redirect_to admin_moderators_path + end + + def create + @moderator.user_id = params[:user_id] + @moderator.save + redirect_to admin_moderators_path end end diff --git a/app/models/ability.rb b/app/models/ability.rb index c8ed63d22..b4c8295fd 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -75,6 +75,8 @@ class Ability can :restore, Debate can :restore, User can :comment_as_administrator, [Debate, Comment] + + can :manage, Moderator end end end diff --git a/app/models/user.rb b/app/models/user.rb index 355a1c326..d7b77a7e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -110,10 +110,6 @@ class User < ActiveRecord::Base e.present? ? where(email: e) : none end - def toggle_moderator - moderator? ? self.moderator.destroy : create_moderator - end - def email_provided? !!(email && email !~ OMNIAUTH_EMAIL_REGEX) || !!(unconfirmed_email && unconfirmed_email !~ OMNIAUTH_EMAIL_REGEX) diff --git a/app/views/admin/moderators/_moderator.html.erb b/app/views/admin/moderators/_moderator.html.erb index de351c160..00fd9c602 100644 --- a/app/views/admin/moderators/_moderator.html.erb +++ b/app/views/admin/moderators/_moderator.html.erb @@ -1,18 +1,13 @@ -<% if moderator %> - <%= moderator.name %> -  •  - <%= moderator.email %> - <% if moderator.moderator? %> - <%= link_to t('admin.moderators.moderator.delete'), - toggle_admin_moderator_path(moderator), - method: :post, - class: "button tiny radius alert right" %> - <% else %> - <%= link_to t('admin.moderators.moderator.add'), - toggle_admin_moderator_path(moderator), - method: :post, - class: "button tiny radius success right" %> - <% end %> -<% else%> - <%= t('admin.moderators.search.user_not_found') %> +<%= moderator.name %> + •  +<%= moderator.email %> +<% if moderator.persisted? %> + <%= link_to t('admin.moderators.moderator.delete'), + admin_moderator_path(moderator), + method: :delete, + class: "button tiny radius alert right" + %> +<% else %> + <%= link_to t('admin.moderators.moderator.add'),{ controller: "admin/moderators", action: :create, user_id: moderator.user_id }, + method: :post, class: "button tiny radius success right" %> <% end %> diff --git a/app/views/admin/moderators/index.html.erb b/app/views/admin/moderators/index.html.erb index 8a1f8e40d..cf3855d05 100644 --- a/app/views/admin/moderators/index.html.erb +++ b/app/views/admin/moderators/index.html.erb @@ -11,8 +11,7 @@ <% end %> -
    -
    +

    <%= page_entries_info @moderators %>

      <% @moderators.each do |moderator| %> diff --git a/app/views/admin/moderators/search.js.erb b/app/views/admin/moderators/search.js.erb index 30d9f8a5d..5b8a61207 100644 --- a/app/views/admin/moderators/search.js.erb +++ b/app/views/admin/moderators/search.js.erb @@ -1 +1 @@ -$("#search-result").html("
      <%= j render 'moderator', moderator: @user %>
      "); +$("#search-result").html("
      <%= j render 'moderator', moderator: @moderator %>
      "); diff --git a/app/views/admin/moderators/user_not_found.js.erb b/app/views/admin/moderators/user_not_found.js.erb new file mode 100644 index 000000000..a3f86936c --- /dev/null +++ b/app/views/admin/moderators/user_not_found.js.erb @@ -0,0 +1 @@ +$("#search-result").html("
      <%= j t('admin.moderators.search.user_not_found') %>
      "); diff --git a/config/routes.rb b/config/routes.rb index 67f3734e9..1a9568d1b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -75,8 +75,7 @@ Rails.application.routes.draw do end resources :settings, only: [:index, :update] - resources :moderators, only: [:index] do - member { post :toggle } + resources :moderators, only: [:index, :create, :destroy] do collection { get :search } end end diff --git a/db/schema.rb b/db/schema.rb index 1d7972417..6f71e0f4a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -41,7 +41,7 @@ ActiveRecord::Schema.define(version: 20150826112500) do t.string "title" t.text "body" t.string "subject" - t.integer "user_id", null: false + t.integer "user_id", null: false t.integer "parent_id" t.integer "lft" t.integer "rgt" @@ -70,16 +70,16 @@ ActiveRecord::Schema.define(version: 20150826112500) do t.string "title", limit: 80 t.text "description" t.integer "author_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.datetime "hidden_at" t.string "visit_id" t.datetime "flagged_as_inappropiate_at" t.integer "inappropiate_flags_count", default: 0 + t.datetime "archived_at" t.integer "cached_votes_total", default: 0 t.integer "cached_votes_up", default: 0 t.integer "cached_votes_down", default: 0 - t.datetime "archived_at" t.integer "comments_count", default: 0 end @@ -241,4 +241,4 @@ ActiveRecord::Schema.define(version: 20150826112500) do add_foreign_key "inappropiate_flags", "users" add_foreign_key "moderators", "users" add_foreign_key "organizations", "users" -end \ No newline at end of file +end