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 %>