Module: ControllerExtensions
- Defined in:
- lib/controller_extensions.rb
Overview
for inclusion into ApplicationController
Class Method Summary collapse
Instance Method Summary collapse
- #access_denied(e) ⇒ Object
- #activation_required(e) ⇒ Object
- #current_reader ⇒ Object
- #current_reader=(reader) ⇒ Object
- #current_reader_session ⇒ Object
- #current_reader_session=(reader_session) ⇒ Object
-
#login_required(e) ⇒ Object
reader-permission exception handling.
- #redirect_back ⇒ Object
- #redirect_back_or_to(default) ⇒ Object
- #redirect_back_with_format(format = 'html') ⇒ Object
- #set_reader ⇒ Object
- #set_reader_for_user ⇒ Object
- #store_location!(location = request.request_uri) ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/controller_extensions.rb', line 3 def self.included(base) base.class_eval do rescue_from ReaderError::AccessDenied, :with => :access_denied rescue_from ReaderError::LoginRequired, :with => :login_required rescue_from ReaderError::ActivationRequired, :with => :activation_required before_filter :set_reader_for_user before_filter :set_reader helper_method :current_reader_session, :current_reader, :current_reader= end protected def current_reader_session return @current_reader_session if @current_reader_session.is_a?(ReaderSession) @current_reader_session = ReaderSession.find Reader.current = @current_reader_session.record if @current_reader_session @current_reader_session end def current_reader_session=(reader_session) @current_reader_session = reader_session end def current_reader current_reader_session.record if current_reader_session end def current_reader=(reader) if reader && reader.is_a?(Reader) current_reader_session = ReaderSession.create!(reader) else current_reader_session.destroy end end def set_reader_for_user if current_user current_reader_session = ReaderSession.create!(Reader.for_user(current_user)) end end def set_reader Reader.current = current_reader end def store_location!(location = request.request_uri) session[:return_to] = location end def redirect_back if session[:return_to] redirect_to session[:return_to] session[:return_to] = nil true else false end end def redirect_back_or_to(default) redirect_back or redirect_to(default) end def redirect_back_with_format(format = 'html') address = session[:return_to] previous_format = File.extname(address) raise StandardError, "Can't add format to an already formatted url: #{address}" unless previous_format.blank? || previous_format == format redirect_to address + ".#{format}" # nasty! but necessary for inline login. end # reader-permission exception handling def login_required(e) @message = e. store_location! respond_to do |format| format.html { flash[:explanation] = t('reader_extension.reader_required') flash[:notice] = e. redirect_to reader_login_url } format.js { @inline = true render :partial => 'reader_sessions/login_form' } end end def activation_required(e) @message = e. respond_to do |format| format.html { flash[:explanation] = t('reader_extension.activation_required') redirect_to reader_activation_url } format.js { @inline = true render :partial => 'reader_activations/activation_required' } end end def access_denied(e) @message = e. respond_to do |format| format.html { flash[:explanation] = t('reader_extension.access_denied') flash[:notice] = e. render :template => 'shared/not_allowed' } format.js { render :text => @message, :status => 403 } end end end |
Instance Method Details
#access_denied(e) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/controller_extensions.rb', line 106 def access_denied(e) @message = e. respond_to do |format| format.html { flash[:explanation] = t('reader_extension.access_denied') flash[:notice] = e. render :template => 'shared/not_allowed' } format.js { render :text => @message, :status => 403 } end end |
#activation_required(e) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/controller_extensions.rb', line 92 def activation_required(e) @message = e. respond_to do |format| format.html { flash[:explanation] = t('reader_extension.activation_required') redirect_to reader_activation_url } format.js { @inline = true render :partial => 'reader_activations/activation_required' } end end |
#current_reader ⇒ Object
27 28 29 |
# File 'lib/controller_extensions.rb', line 27 def current_reader current_reader_session.record if current_reader_session end |
#current_reader=(reader) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/controller_extensions.rb', line 31 def current_reader=(reader) if reader && reader.is_a?(Reader) current_reader_session = ReaderSession.create!(reader) else current_reader_session.destroy end end |
#current_reader_session ⇒ Object
16 17 18 19 20 21 |
# File 'lib/controller_extensions.rb', line 16 def current_reader_session return @current_reader_session if @current_reader_session.is_a?(ReaderSession) @current_reader_session = ReaderSession.find Reader.current = @current_reader_session.record if @current_reader_session @current_reader_session end |
#current_reader_session=(reader_session) ⇒ Object
23 24 25 |
# File 'lib/controller_extensions.rb', line 23 def current_reader_session=(reader_session) @current_reader_session = reader_session end |
#login_required(e) ⇒ Object
reader-permission exception handling
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/controller_extensions.rb', line 76 def login_required(e) @message = e. store_location! respond_to do |format| format.html { flash[:explanation] = t('reader_extension.reader_required') flash[:notice] = e. redirect_to reader_login_url } format.js { @inline = true render :partial => 'reader_sessions/login_form' } end end |
#redirect_back ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/controller_extensions.rb', line 53 def redirect_back if session[:return_to] redirect_to session[:return_to] session[:return_to] = nil true else false end end |
#redirect_back_or_to(default) ⇒ Object
63 64 65 |
# File 'lib/controller_extensions.rb', line 63 def redirect_back_or_to(default) redirect_back or redirect_to(default) end |
#redirect_back_with_format(format = 'html') ⇒ Object
67 68 69 70 71 72 |
# File 'lib/controller_extensions.rb', line 67 def redirect_back_with_format(format = 'html') address = session[:return_to] previous_format = File.extname(address) raise StandardError, "Can't add format to an already formatted url: #{address}" unless previous_format.blank? || previous_format == format redirect_to address + ".#{format}" # nasty! but necessary for inline login. end |
#set_reader ⇒ Object
45 46 47 |
# File 'lib/controller_extensions.rb', line 45 def set_reader Reader.current = current_reader end |
#set_reader_for_user ⇒ Object
39 40 41 42 43 |
# File 'lib/controller_extensions.rb', line 39 def set_reader_for_user if current_user current_reader_session = ReaderSession.create!(Reader.for_user(current_user)) end end |
#store_location!(location = request.request_uri) ⇒ Object
49 50 51 |
# File 'lib/controller_extensions.rb', line 49 def store_location!(location = request.request_uri) session[:return_to] = location end |