Finishes admin interface for debates

This commit is contained in:
kikito
2015-08-27 19:05:27 +02:00
parent ad3b689a22
commit fb9d3097cc
12 changed files with 168 additions and 69 deletions

View File

@@ -1,16 +1,35 @@
class Admin::DebatesController < Admin::BaseController class Admin::DebatesController < Admin::BaseController
before_filter :set_valid_filters, only: :index
before_filter :parse_filter, only: :index
before_filter :load_debate, only: [:confirm_hide, :restore]
def index def index
@debates = Debate.only_hidden.page(params[:page]) @debates = Debate.only_hidden.send(@filter).page(params[:page])
end end
def show def confirm_hide
@debate = Debate.with_hidden.find(params[:id]) @debate.confirm_hide
redirect_to request.query_parameters.merge(action: :index)
end end
def restore def restore
@debate = Debate.with_hidden.find(params[:id])
@debate.restore @debate.restore
redirect_to admin_debates_path, notice: t('admin.debates.restore.success') redirect_to request.query_parameters.merge(action: :index)
end end
private
def load_debate
@debate = Debate.with_hidden.find(params[:id])
end
def set_valid_filters
@valid_filters = %w{all with_confirmed_hide}
end
def parse_filter
@filter = params[:filter]
@filter = 'all' unless @valid_filters.include?(@filter)
end
end end

View File

@@ -1,6 +1,5 @@
require 'numeric' require 'numeric'
class Debate < ActiveRecord::Base class Debate < ActiveRecord::Base
include ActsAsParanoidAliases
default_scope { order(created_at: :desc) } default_scope { order(created_at: :desc) }
apply_simple_captcha apply_simple_captcha
@@ -10,6 +9,7 @@ class Debate < ActiveRecord::Base
acts_as_commentable acts_as_commentable
acts_as_taggable acts_as_taggable
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
has_many :flags, :as => :flaggable has_many :flags, :as => :flaggable

View File

@@ -1,28 +1,35 @@
<h2><%= t("admin.debates.index.title") %></h2> <h2><%= t("admin.debates.index.title") %></h2>
<!-- Filters for pending and archived debates (example on "/admin/organizations/index.html.erb")-->
<dl class="sub-nav"> <dl class="sub-nav">
<dt><%= t("admin.debates.index.filter") %>:</dt> <dt><%= t("admin.debates.index.filter") %>:</dt>
<dd class="active"><%= t("admin.debates.filters.all") %></dd>
<dd><%= t("admin.debates.filters.pending") %></dd> <% @valid_filters.each do |filter| %>
<dd><%= t("admin.debates.filters.archived") %></dd> <% if @filter == filter %>
<dd class="active"><%= t("admin.debates.index.filters.#{filter}") %></dd>
<% else %>
<dd><%= link_to t("admin.debates.index.filters.#{filter}"),
admin_debates_path(filter: filter) %></dd>
<% end %>
<% end %>
</dl> </dl>
<!-- Filters for pending and archived debates (example on "/admin/organizations/index.html.erb")-->
<h3><%= page_entries_info @debates %></h3> <h3><%= page_entries_info @debates %></h3>
<ul class="admin-list"> <ul class="admin-list">
<% @debates.each do |debate| %> <% @debates.each do |debate| %>
<li id="<%= dom_id(debate) %>"> <li id="<%= dom_id(debate) %>">
<%= link_to debate.title, admin_debate_path(debate) %> <%= link_to debate.title, debate_path(debate) %>
<%= link_to t("admin.actions.restore"), restore_admin_debate_path(debate), <%= link_to t("admin.actions.restore"),
method: :put, data: { confirm: t("admin.actions.confirm") }, restore_admin_debate_path(debate, request.query_parameters),
method: :put,
data: { confirm: t("admin.actions.confirm") },
class: "button radius tiny success right" %> class: "button radius tiny success right" %>
<!-- Link to archive this debate --> <%= link_to t("admin.actions.confirm_hide"),
<%= link_to t("admin.actions.archive"), "#", class: "button radius tiny warning right" %> confirm_hide_admin_debate_path(debate, request.query_parameters),
<!-- /. Link to archive this debate --> method: :put,
class: "button radius tiny warning right" %>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View File

@@ -1,12 +0,0 @@
<h2><%= t("admin.debates.index.title") %></h2>
<h3><%= @debate.title %></h3>
<div><%= @debate.description %></div>
<%= link_to t("admin.debates.show.back"), admin_debates_path,
class: "button radius small secondary" %>
<%= link_to t("admin.actions.restore"), restore_admin_debate_path(@debate),
method: :put, data: { confirm: t("admin.actions.confirm") },
class: "button radius small success" %>

View File

@@ -19,7 +19,7 @@
<ul class="admin-list"> <ul class="admin-list">
<% @debates.each do |debate| %> <% @debates.each do |debate| %>
<li> <li>
<%= link_to debate.title, admin_debate_path(debate) %> <%= link_to debate.title, debate_path(debate) %>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View File

@@ -36,6 +36,7 @@ en:
hide_author: Ban author hide_author: Ban author
restore: Restore restore: Restore
confirm: 'Are you sure?' confirm: 'Are you sure?'
confirm_hide: Confirm
archive: Archive archive: Archive
tags: tags:
index: index:
@@ -60,14 +61,9 @@ en:
index: index:
title: Hidden debates title: Hidden debates
filter: Filter filter: Filter
show:
back: Back
restore:
success: The debate has been restored
filters: filters:
all: All all: All
pending: Pending with_confirmed_hide: Confirmed
archived: Archived
users: users:
index: index:
title: Banned users title: Banned users

View File

@@ -36,6 +36,7 @@ es:
hide_author: Bloquear al autor hide_author: Bloquear al autor
restore: Volver a mostrar restore: Volver a mostrar
confirm: '¿Estás seguro?' confirm: '¿Estás seguro?'
confirm_hide: Confirmar
archive: Archivar archive: Archivar
tags: tags:
index: index:
@@ -60,14 +61,9 @@ es:
index: index:
title: Debates ocultos title: Debates ocultos
filter: Filtro filter: Filtro
show:
back: Volver
restore:
success: El debate ha sido permitido
filters: filters:
all: Todos all: Todos
pending: Pendientes with_confirmed_hide: Confirmados
archived: Archivados
users: users:
index: index:
title: Usuarios bloqueados title: Usuarios bloqueados

View File

@@ -58,15 +58,24 @@ Rails.application.routes.draw do
end end
resources :users, only: [:index, :show] do resources :users, only: [:index, :show] do
member { put :restore } member do
put :restore
put :confirm_hide
end
end end
resources :debates, only: [:index, :show] do resources :debates, only: :index do
member { put :restore } member do
put :restore
put :confirm_hide
end
end end
resources :comments, only: :index do resources :comments, only: :index do
member { put :restore } member do
put :restore
put :confirm_hide
end
end end
resources :tags, only: [:index, :create, :update, :destroy] resources :tags, only: [:index, :create, :update, :destroy]

View File

@@ -14,9 +14,26 @@ module ActsAsParanoidAliases
def after_hide def after_hide
end end
def confirmed_hide?
confirmed_hide_at.present?
end
def confirm_hide
update_attribute(:confirmed_hide_at, Time.now)
end
def restore(opts={})
super(opts)
update_attribute(:confirmed_hide_at, nil)
end
end end
module ClassMethods module ClassMethods
def with_confirmed_hide
where("confirmed_hide_at IS NOT NULL")
end
def with_hidden def with_hidden
with_deleted with_deleted
end end
@@ -35,9 +52,5 @@ module ActsAsParanoidAliases
only_hidden.where(id: ids).update_all(hidden_at: nil) only_hidden.where(id: ids).update_all(hidden_at: nil)
end end
end end
end end
module ActsAsParanoid
include ActsAsParanoidAliases
end

View File

@@ -8,6 +8,10 @@ FactoryGirl.define do
trait :hidden do trait :hidden do
hidden_at Time.now hidden_at Time.now
end end
trait :with_confirmed_hide do
confirmed_hide_at Time.now
end
end end
factory :identity do factory :identity do
@@ -30,6 +34,10 @@ FactoryGirl.define do
ignored_flag_at Time.now ignored_flag_at Time.now
end end
trait :with_confirmed_hide do
confirmed_hide_at Time.now
end
trait :flagged do trait :flagged do
after :create do |debate| after :create do |debate|
Flag.flag!(FactoryGirl.create(:user), debate) Flag.flag!(FactoryGirl.create(:user), debate)
@@ -59,6 +67,10 @@ FactoryGirl.define do
ignored_flag_at Time.now ignored_flag_at Time.now
end end
trait :with_confirmed_hide do
confirmed_hide_at Time.now
end
trait :flagged do trait :flagged do
after :create do |debate| after :create do |debate|
Flag.flag!(FactoryGirl.create(:user), debate) Flag.flag!(FactoryGirl.create(:user), debate)

View File

@@ -2,22 +2,71 @@ require 'rails_helper'
feature 'Admin debates' do feature 'Admin debates' do
scenario 'Restore', :js do background do
citizen = create(:user)
admin = create(:administrator) admin = create(:administrator)
debate = create(:debate, :hidden)
login_as(admin.user) login_as(admin.user)
visit admin_debate_path(debate) end
scenario 'Restore' do
debate = create(:debate, :hidden)
visit admin_debates_path
click_link 'Restore' click_link 'Restore'
expect(page).to have_content 'The debate has been restored' expect(page).to_not have_content(debate.title)
login_as(citizen) expect(debate.reload).to_not be_hidden
visit debates_path end
expect(page).to have_css('.debate', count: 1) scenario 'Confirm hide' do
debate = create(:debate, :hidden)
visit admin_debates_path
click_link 'Confirm'
expect(page).to have_content(debate.title)
expect(page).to have_content('Confirmed')
expect(debate.reload).to be_confirmed_hide
end end
scenario "Current filter is properly highlighted" do
visit admin_debates_path
expect(page).to_not have_link('All')
expect(page).to have_link('Confirmed')
visit admin_debates_path(filter: 'all')
expect(page).to_not have_link('All')
expect(page).to have_link('Confirmed')
visit admin_debates_path(filter: 'with_confirmed_hide')
expect(page).to have_link('All')
expect(page).to_not have_link('Confirmed')
end
scenario "Filtering debates" do
create(:debate, :hidden, title: "Unconfirmed debate")
create(:debate, :hidden, :with_confirmed_hide, title: "Confirmed debate")
visit admin_debates_path(filter: 'all')
expect(page).to have_content('Unconfirmed debate')
expect(page).to have_content('Confirmed debate')
visit admin_debates_path(filter: 'with_confirmed_hide')
expect(page).to_not have_content('Unconfirmed debate')
expect(page).to have_content('Confirmed debate')
end
scenario "Action links remember the pagination setting and the filter" do
per_page = Kaminari.config.default_per_page
(per_page + 2).times { create(:debate, :hidden, :with_confirmed_hide) }
visit admin_debates_path(filter: 'with_confirmed_hide', page: 2)
click_on('Restore', match: :first, exact: true)
expect(current_url).to include('filter=with_confirmed_hide')
expect(current_url).to include('page=2')
end
end end

View File

@@ -2,7 +2,7 @@ require 'rails_helper'
describe 'Paranoid methods' do describe 'Paranoid methods' do
describe '#hide_all' do describe '.hide_all' do
it 'hides all instances in the id list' do it 'hides all instances in the id list' do
debate1 = create(:debate) debate1 = create(:debate)
debate2 = create(:debate) debate2 = create(:debate)
@@ -17,7 +17,7 @@ describe 'Paranoid methods' do
end end
end end
describe '#restore_all' do describe '.restore_all' do
it 'restores all instances in the id list' do it 'restores all instances in the id list' do
debate1 = create(:debate) debate1 = create(:debate)
debate2 = create(:debate) debate2 = create(:debate)
@@ -34,4 +34,14 @@ describe 'Paranoid methods' do
end end
end end
describe '#restore' do
it 'resets the confirmed_hide_at attribute' do
debate = create(:debate, :hidden, :with_confirmed_hide)
debate.restore
expect(debate.reload.confirmed_hide?).to_not be
end
end
end end