Refactored admin´s moderator controller

This commit is contained in:
Jose Manuel
2015-08-29 01:39:46 +01:00
parent 3baef4ae79
commit e21336aeff
9 changed files with 45 additions and 38 deletions

View File

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

View File

@@ -75,6 +75,8 @@ class Ability
can :restore, Debate
can :restore, User
can :comment_as_administrator, [Debate, Comment]
can :manage, Moderator
end
end
end

View File

@@ -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)

View File

@@ -1,18 +1,13 @@
<% if moderator %>
<span><%= moderator.name %></span>
<span class="bullet">&nbsp;&bullet;&nbsp;</span>
<span><%= moderator.email %></span>
<% 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%>
<span><%= t('admin.moderators.search.user_not_found') %></span>
<span><%= moderator.name %></span>
<span class="bullet">&nbsp;&bullet;&nbsp;</span>
<span><%= moderator.email %></span>
<% 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 %>

View File

@@ -11,8 +11,7 @@
<% end %>
</div>
</div>
<div id="search-result" class="small">
</div>
<div id="search-result" class="row small"></div>
<h3><%= page_entries_info @moderators %></h3>
<ul class="admin-list">
<% @moderators.each do |moderator| %>

View File

@@ -1 +1 @@
$("#search-result").html("<div class=\"panel\"><%= j render 'moderator', moderator: @user %></div>");
$("#search-result").html("<div class=\"panel column\"><%= j render 'moderator', moderator: @moderator %></div>");

View File

@@ -0,0 +1 @@
$("#search-result").html("<div class=\"panel column\"><%= j t('admin.moderators.search.user_not_found') %></div>");

View File

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

View File

@@ -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
end