Sha256: ec206f0240e0a74e72e561e11ff1ccf3b41cb287b1e3073e7495553f4d99c944

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module Crystal
  module ControllerUrlHelper
    # 
    # redirect_to
    # 
    def redirect_to *args
      params, response = workspace.params, workspace.response
      params.format.must_be.in 'html', 'js'                
      
      url = special_url(*args) || url_for(*args)
      
      if params.format == 'js'
        response.body << "window.location = '#{url}';"
      else
        response.status = 301
        response.headers['Location'] = url        
        response.body = %(<html><body>You are being <a href="#{h(url)}">redirected</a>.</body></html>)
      end
      
      # Flash need to know if we using redirect
      force_keeping_mode_for_flash!
      
      nil
    end    
    
    def reload_page
      workspace.params.format.must == 'js'
      
      force_keeping_mode_for_flash!
      workspace.response.body << "window.location.reload();"
    end
    
    def special_url *args
      return nil unless args.last.empty?
              
      case args.first.to_s
      when 'back'
        workspace.request.env["HTTP_REFERER"] || 'javascript:history.back()'
      when '#' then
        '#'
      else
        nil
      end
    end
    
    protected            
      
      def force_keeping_mode_for_flash!
        crystal[:flash].force_keeping_mode! if crystal.include? :flash
      end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
crystal-0.0.13 lib/crystal/html/controller_helpers/controller_url_helper.rb
crystal-0.0.12 lib/crystal/html/controller_helpers/controller_url_helper.rb
crystal_ext-0.0.11 lib/crystal/html/controller_helpers/controller_url_helper.rb