From c4db0ff9201cc34c0588027b32b6bd9f0998c72d Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Mon, 20 Jul 2015 00:35:53 +0300 Subject: [PATCH] Extract current_conference assignment in a concern --- app/controllers/application_controller.rb | 6 +---- .../concerns/current_conference_assigning.rb | 26 +++++++++++++++++++ .../management/management_controller.rb | 18 ------------- 3 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 app/controllers/concerns/current_conference_assigning.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4d04934..58dd170 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,9 +1,5 @@ class ApplicationController < ActionController::Base - def require_current_conference - if not current_conference? - redirect_to '/', alert: 'No conference selected' - end - end + include CurrentConferenceAssigning # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. diff --git a/app/controllers/concerns/current_conference_assigning.rb b/app/controllers/concerns/current_conference_assigning.rb new file mode 100644 index 0000000..c2a6f2a --- /dev/null +++ b/app/controllers/concerns/current_conference_assigning.rb @@ -0,0 +1,26 @@ +module CurrentConferenceAssigning + extend ActiveSupport::Concern + + included do + helper_method :current_conference? + helper_method :current_conference + end + + def current_conference? + current_conference.present? + end + + def current_conference + if not @current_conference and params[:conference_id].present? + @current_conference = Conference.find(params[:conference_id]) + end + + @current_conference + end + + def require_current_conference! + if not current_conference? + raise ActionController::RoutingError.new('Not Found') + end + end +end diff --git a/app/controllers/management/management_controller.rb b/app/controllers/management/management_controller.rb index cefbbaf..d50b412 100644 --- a/app/controllers/management/management_controller.rb +++ b/app/controllers/management/management_controller.rb @@ -6,24 +6,6 @@ module Management private - def current_conference? - current_conference.present? - end - helper_method :current_conference? - - def current_conference - if not @current_conference - if @conference - @current_conference = @conference - elsif params[:conference_id].present? - @current_conference = Conference.find(params[:conference_id]) - end - end - - @current_conference - end - helper_method :current_conference - def authorize_user! head :forbidden unless current_user.admin? end