From fb9d3097cc1c6daa29b22b8a6ab9ff3f976d105b Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 27 Aug 2015 19:05:27 +0200 Subject: [PATCH] Finishes admin interface for debates --- app/controllers/admin/debates_controller.rb | 31 +++++++-- app/models/debate.rb | 2 +- app/views/admin/debates/index.html.erb | 31 +++++---- app/views/admin/debates/show.html.erb | 12 ---- app/views/admin/users/show.html.erb | 2 +- config/locales/admin.en.yml | 12 ++-- config/locales/admin.es.yml | 12 ++-- config/routes.rb | 17 +++-- lib/acts_as_paranoid_aliases.rb | 21 ++++-- spec/factories.rb | 12 ++++ spec/features/admin/debates_spec.rb | 71 +++++++++++++++++---- spec/lib/acts_as_paranoid_aliases_spec.rb | 14 +++- 12 files changed, 168 insertions(+), 69 deletions(-) delete mode 100644 app/views/admin/debates/show.html.erb diff --git a/app/controllers/admin/debates_controller.rb b/app/controllers/admin/debates_controller.rb index 9f2f21f62..899ce13d6 100644 --- a/app/controllers/admin/debates_controller.rb +++ b/app/controllers/admin/debates_controller.rb @@ -1,16 +1,35 @@ 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 - @debates = Debate.only_hidden.page(params[:page]) + @debates = Debate.only_hidden.send(@filter).page(params[:page]) end - def show - @debate = Debate.with_hidden.find(params[:id]) + def confirm_hide + @debate.confirm_hide + redirect_to request.query_parameters.merge(action: :index) end def restore - @debate = Debate.with_hidden.find(params[:id]) @debate.restore - redirect_to admin_debates_path, notice: t('admin.debates.restore.success') + redirect_to request.query_parameters.merge(action: :index) end -end \ No newline at end of file + + 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 diff --git a/app/models/debate.rb b/app/models/debate.rb index eed3fb7b5..14c229827 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,6 +1,5 @@ require 'numeric' class Debate < ActiveRecord::Base - include ActsAsParanoidAliases default_scope { order(created_at: :desc) } apply_simple_captcha @@ -10,6 +9,7 @@ class Debate < ActiveRecord::Base acts_as_commentable acts_as_taggable acts_as_paranoid column: :hidden_at + include ActsAsParanoidAliases belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' has_many :flags, :as => :flaggable diff --git a/app/views/admin/debates/index.html.erb b/app/views/admin/debates/index.html.erb index dbd85a46f..cdeb7b016 100644 --- a/app/views/admin/debates/index.html.erb +++ b/app/views/admin/debates/index.html.erb @@ -1,30 +1,37 @@

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

- -

<%= page_entries_info @debates %>

-<%= paginate @debates %> \ No newline at end of file +<%= paginate @debates %> diff --git a/app/views/admin/debates/show.html.erb b/app/views/admin/debates/show.html.erb deleted file mode 100644 index e740baf47..000000000 --- a/app/views/admin/debates/show.html.erb +++ /dev/null @@ -1,12 +0,0 @@ -

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

- -

<%= @debate.title %>

- -
<%= @debate.description %>
- -<%= 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" %> diff --git a/app/views/admin/users/show.html.erb b/app/views/admin/users/show.html.erb index 375d9110e..7d735ca89 100644 --- a/app/views/admin/users/show.html.erb +++ b/app/views/admin/users/show.html.erb @@ -19,7 +19,7 @@ diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 971699c0b..3190eedb7 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -36,6 +36,7 @@ en: hide_author: Ban author restore: Restore confirm: 'Are you sure?' + confirm_hide: Confirm archive: Archive tags: index: @@ -60,14 +61,9 @@ en: index: title: Hidden debates filter: Filter - show: - back: Back - restore: - success: The debate has been restored - filters: - all: All - pending: Pending - archived: Archived + filters: + all: All + with_confirmed_hide: Confirmed users: index: title: Banned users diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 3c264447f..18dfc06b0 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -36,6 +36,7 @@ es: hide_author: Bloquear al autor restore: Volver a mostrar confirm: '¿Estás seguro?' + confirm_hide: Confirmar archive: Archivar tags: index: @@ -60,14 +61,9 @@ es: index: title: Debates ocultos filter: Filtro - show: - back: Volver - restore: - success: El debate ha sido permitido - filters: - all: Todos - pending: Pendientes - archived: Archivados + filters: + all: Todos + with_confirmed_hide: Confirmados users: index: title: Usuarios bloqueados diff --git a/config/routes.rb b/config/routes.rb index 6d2170ffd..eb3953ea9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,15 +58,24 @@ Rails.application.routes.draw do end resources :users, only: [:index, :show] do - member { put :restore } + member do + put :restore + put :confirm_hide + end end - resources :debates, only: [:index, :show] do - member { put :restore } + resources :debates, only: :index do + member do + put :restore + put :confirm_hide + end end resources :comments, only: :index do - member { put :restore } + member do + put :restore + put :confirm_hide + end end resources :tags, only: [:index, :create, :update, :destroy] diff --git a/lib/acts_as_paranoid_aliases.rb b/lib/acts_as_paranoid_aliases.rb index b4faa19b9..1d7dbf6d9 100644 --- a/lib/acts_as_paranoid_aliases.rb +++ b/lib/acts_as_paranoid_aliases.rb @@ -14,9 +14,26 @@ module ActsAsParanoidAliases def after_hide 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 module ClassMethods + def with_confirmed_hide + where("confirmed_hide_at IS NOT NULL") + end + def with_hidden with_deleted end @@ -35,9 +52,5 @@ module ActsAsParanoidAliases only_hidden.where(id: ids).update_all(hidden_at: nil) end end - end -module ActsAsParanoid - include ActsAsParanoidAliases -end \ No newline at end of file diff --git a/spec/factories.rb b/spec/factories.rb index 9734b3364..3e355499a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -8,6 +8,10 @@ FactoryGirl.define do trait :hidden do hidden_at Time.now end + + trait :with_confirmed_hide do + confirmed_hide_at Time.now + end end factory :identity do @@ -30,6 +34,10 @@ FactoryGirl.define do ignored_flag_at Time.now end + trait :with_confirmed_hide do + confirmed_hide_at Time.now + end + trait :flagged do after :create do |debate| Flag.flag!(FactoryGirl.create(:user), debate) @@ -59,6 +67,10 @@ FactoryGirl.define do ignored_flag_at Time.now end + trait :with_confirmed_hide do + confirmed_hide_at Time.now + end + trait :flagged do after :create do |debate| Flag.flag!(FactoryGirl.create(:user), debate) diff --git a/spec/features/admin/debates_spec.rb b/spec/features/admin/debates_spec.rb index 50859816e..9d956582e 100644 --- a/spec/features/admin/debates_spec.rb +++ b/spec/features/admin/debates_spec.rb @@ -2,22 +2,71 @@ require 'rails_helper' feature 'Admin debates' do - scenario 'Restore', :js do - citizen = create(:user) + background do admin = create(:administrator) - - debate = create(:debate, :hidden) - login_as(admin.user) - visit admin_debate_path(debate) + end + + scenario 'Restore' do + debate = create(:debate, :hidden) + visit admin_debates_path click_link 'Restore' - expect(page).to have_content 'The debate has been restored' + expect(page).to_not have_content(debate.title) - login_as(citizen) - visit debates_path - - expect(page).to have_css('.debate', count: 1) + expect(debate.reload).to_not be_hidden end + + 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 + + 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 diff --git a/spec/lib/acts_as_paranoid_aliases_spec.rb b/spec/lib/acts_as_paranoid_aliases_spec.rb index 1c99884f3..b47be8cea 100644 --- a/spec/lib/acts_as_paranoid_aliases_spec.rb +++ b/spec/lib/acts_as_paranoid_aliases_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' describe 'Paranoid methods' do - describe '#hide_all' do + describe '.hide_all' do it 'hides all instances in the id list' do debate1 = create(:debate) debate2 = create(:debate) @@ -17,7 +17,7 @@ describe 'Paranoid methods' do end end - describe '#restore_all' do + describe '.restore_all' do it 'restores all instances in the id list' do debate1 = create(:debate) debate2 = create(:debate) @@ -34,4 +34,14 @@ describe 'Paranoid methods' do 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