Commit 388e86a

mo khan <mo@mokhan.ca>
2016-05-17 04:07:12
extract authenticatable module.
1 parent cd0503b
app/controllers/concerns/authenticatable.rb
@@ -0,0 +1,24 @@
+module Authenticatable
+  extend ActiveSupport::Concern
+  included do
+    before_action :authenticate!
+    helper_method :current_user, :current_session
+  end
+
+  protected
+
+  def current_session(session_id = session[:user_id])
+    @current_session ||= UserSession.authenticate(session_id)
+  end
+
+  def current_user
+    @current_user ||= current_session.try(:user)
+  end
+
+  def authenticate!
+    return if current_user.present?
+    redirect_to new_session_path
+  rescue
+    redirect_to new_session_path
+  end
+end
app/controllers/concerns/internationalizationable.rb
@@ -6,6 +6,12 @@ module Internationalizationable
     around_action :with_locale
   end
 
+  protected
+
+  def translate(key)
+    I18n.translate("#{params[:controller]}.#{params[:action]}#{key}")
+  end
+
   private
 
   def with_time_zone
app/controllers/application_controller.rb
@@ -1,39 +1,18 @@
 class ApplicationController < ActionController::Base
+  include Authenticatable
   include Internationalizationable
   include Pageable
   # Prevent CSRF attacks by raising an exception.
   # For APIs, you may want to use :null_session instead.
   protect_from_forgery with: :exception
-  before_action :authenticate!
   rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
-  helper_method :current_user, :current_session, :feature_available?
-
-  protected
-
-  def current_session(session_id = session[:user_id])
-    @current_session ||= UserSession.authenticate(session_id)
-  end
-
-  def current_user
-    @current_user ||= current_session.try(:user)
-  end
+  helper_method :feature_available?
 
   def feature_available?(feature)
     return true if Rails.env.test?
     $flipper[feature.to_sym].enabled?(current_user)
   end
 
-  def translate(key)
-    I18n.translate("#{params[:controller]}.#{params[:action]}#{key}")
-  end
-
-  def authenticate!
-    return if current_user.present?
-    redirect_to new_session_path
-  rescue
-    redirect_to new_session_path
-  end
-
   def record_not_found
     render text: "404 Not Found", status: 404
   end