diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 40f9b31dc..0399ccd63 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -1,11 +1,11 @@ class Admin::CommentsController < Admin::BaseController - before_filter :set_valid_filters, only: :index - before_filter :parse_filter, only: :index + + has_filters %w{all with_confirmed_hide} before_filter :load_comment, only: [:confirm_hide, :restore] def index - @comments = Comment.only_hidden.send(@filter).page(params[:page]) + @comments = Comment.only_hidden.send(@current_filter).page(params[:page]) end def confirm_hide @@ -23,13 +23,4 @@ class Admin::CommentsController < Admin::BaseController @comment = Comment.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/controllers/admin/debates_controller.rb b/app/controllers/admin/debates_controller.rb index 4487fcf81..23edb01f5 100644 --- a/app/controllers/admin/debates_controller.rb +++ b/app/controllers/admin/debates_controller.rb @@ -1,11 +1,10 @@ class Admin::DebatesController < Admin::BaseController - before_filter :set_valid_filters, only: :index - before_filter :parse_filter, only: :index + has_filters %w{all with_confirmed_hide}, only: :index before_filter :load_debate, only: [:confirm_hide, :restore] def index - @debates = Debate.only_hidden.send(@filter).page(params[:page]) + @debates = Debate.only_hidden.send(@current_filter).page(params[:page]) end def confirm_hide @@ -24,13 +23,4 @@ class Admin::DebatesController < Admin::BaseController @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/controllers/admin/organizations_controller.rb b/app/controllers/admin/organizations_controller.rb index bb623702a..0e704e9a1 100644 --- a/app/controllers/admin/organizations_controller.rb +++ b/app/controllers/admin/organizations_controller.rb @@ -1,11 +1,11 @@ class Admin::OrganizationsController < Admin::BaseController - before_filter :set_valid_filters, only: :index - before_filter :parse_filter, only: :index + + has_filters %w{all pending verified rejected}, only: :index load_and_authorize_resource except: :search def index - @organizations = @organizations.send(@filter) + @organizations = @organizations.send(@current_filter) @organizations = @organizations.includes(:user).order(:name, 'users.email').page(params[:page]) end @@ -23,14 +23,4 @@ class Admin::OrganizationsController < Admin::BaseController redirect_to request.query_parameters.merge(action: :index) end - private - def set_valid_filters - @valid_filters = %w{all pending verified rejected} - end - - def parse_filter - @filter = params[:filter] - @filter = 'all' unless @valid_filters.include?(@filter) - end - end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 61c0f5650..ecf7f206a 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,11 +1,11 @@ class Admin::UsersController < Admin::BaseController - before_filter :set_valid_filters, only: :index - before_filter :parse_filter, only: :index + + has_filters %w{all with_confirmed_hide}, only: :index before_filter :load_user, only: [:confirm_hide, :restore] def index - @users = User.only_hidden.send(@filter).page(params[:page]) + @users = User.only_hidden.send(@current_filter).page(params[:page]) end def show @@ -30,13 +30,4 @@ class Admin::UsersController < Admin::BaseController @user = User.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/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5d207e463..86219f0ad 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,8 @@ require "application_responder" class ApplicationController < ActionController::Base + include HasFilters + before_filter :authenticate_http_basic before_filter :authenticate_user!, unless: :devise_controller?, if: :beta_site? diff --git a/app/controllers/concerns/has_filters.rb b/app/controllers/concerns/has_filters.rb new file mode 100644 index 000000000..3dd3cc295 --- /dev/null +++ b/app/controllers/concerns/has_filters.rb @@ -0,0 +1,13 @@ +module HasFilters + extend ActiveSupport::Concern + + class_methods do + def has_filters(valid_filters, *args) + before_filter(*args) do + @valid_filters = valid_filters + @current_filter = params[:filter] + @current_filter = @valid_filters.first unless @valid_filters.include?(@current_filter) + end + end + end +end diff --git a/app/controllers/moderation/comments_controller.rb b/app/controllers/moderation/comments_controller.rb index 3622d02ac..065516531 100644 --- a/app/controllers/moderation/comments_controller.rb +++ b/app/controllers/moderation/comments_controller.rb @@ -1,12 +1,12 @@ class Moderation::CommentsController < Moderation::BaseController - before_filter :set_valid_filters, only: :index - before_filter :parse_filter, only: :index + + has_filters %w{all pending_flag_review with_ignored_flag}, only: :index before_filter :load_comments, only: :index load_and_authorize_resource def index - @comments = @comments.send(@filter) + @comments = @comments.send(@current_filter) @comments = @comments.page(params[:page]) end @@ -30,13 +30,4 @@ class Moderation::CommentsController < Moderation::BaseController @comments = Comment.accessible_by(current_ability, :hide).flagged.sorted_for_moderation.includes(:commentable) end - def set_valid_filters - @valid_filters = %w{all pending_flag_review with_ignored_flag} - end - - def parse_filter - @filter = params[:filter] - @filter = 'all' unless @valid_filters.include?(@filter) - end - end diff --git a/app/controllers/moderation/debates_controller.rb b/app/controllers/moderation/debates_controller.rb index abb8e964b..5fdd19049 100644 --- a/app/controllers/moderation/debates_controller.rb +++ b/app/controllers/moderation/debates_controller.rb @@ -1,13 +1,13 @@ class Moderation::DebatesController < Moderation::BaseController - before_filter :set_valid_filters, only: :index - before_filter :parse_filter, only: :index + + has_filters %w{all pending_flag_review with_ignored_flag}, only: :index + before_filter :load_debates, only: :index load_and_authorize_resource def index - @debates = @debates.send(@filter) - @debates = @debates.page(params[:page]) + @debates = @debates.send(@current_filter).page(params[:page]) end def hide @@ -30,13 +30,4 @@ class Moderation::DebatesController < Moderation::BaseController @debates = Debate.accessible_by(current_ability, :hide).flagged.sorted_for_moderation end - def set_valid_filters - @valid_filters = %w{all pending_flag_review with_ignored_flag} - end - - def parse_filter - @filter = params[:filter] - @filter = 'all' unless @valid_filters.include?(@filter) - end - end diff --git a/app/views/admin/comments/index.html.erb b/app/views/admin/comments/index.html.erb index 87178c66b..f0639047f 100644 --- a/app/views/admin/comments/index.html.erb +++ b/app/views/admin/comments/index.html.erb @@ -1,17 +1,6 @@