Refactored admin´s moderator controller
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -75,6 +75,8 @@ class Ability
|
||||
can :restore, Debate
|
||||
can :restore, User
|
||||
can :comment_as_administrator, [Debate, Comment]
|
||||
|
||||
can :manage, Moderator
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
<% if moderator %>
|
||||
<span><%= moderator.name %></span>
|
||||
<span class="bullet"> • </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"> • </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 %>
|
||||
|
||||
@@ -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| %>
|
||||
|
||||
@@ -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>");
|
||||
|
||||
1
app/views/admin/moderators/user_not_found.js.erb
Normal file
1
app/views/admin/moderators/user_not_found.js.erb
Normal file
@@ -0,0 +1 @@
|
||||
$("#search-result").html("<div class=\"panel column\"><%= j t('admin.moderators.search.user_not_found') %></div>");
|
||||
@@ -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
|
||||
|
||||
10
db/schema.rb
10
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
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user