Class: Rack::Adapter::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/adapter/rails.rb

Defined Under Namespace

Classes: CGIWrapper, CgiApp

Constant Summary collapse

FILE_METHODS =
%w(GET HEAD).freeze

Instance Method Summary collapse

Constructor Details

#initialize(helper) ⇒ Rails

Returns a new instance of Rails.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rack/adapter/rails.rb', line 67

def initialize(helper)
  options = helper.options        
  @rails_grizzly_helper = helper
  @root = options[:root] || Dir.pwd
  @env = options[:environment] || 'development'
  @prefix = options[:prefix]
  @public = options[:public]

  load_application

  @rails_app = if rack_based?
    ActionController::Dispatcher.new
  else
    CgiApp.new
  end

  @file_app = Rack::File.new(::File.join(@root, "public"))

end

Instance Method Details

#call(env) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rack/adapter/rails.rb', line 118

def call(env)
  path        = env['PATH_INFO'].chomp('/')
  method      = env['REQUEST_METHOD']
  cached_path = (path.empty? ? 'index' : path) + ActionController::Base.page_cache_extension

  if FILE_METHODS.include?(method)
    if file_exist?(path)              # Serve the file if it's there
      return @file_app.call(env)
    elsif file_exist?(cached_path)    # Serve the page cache if it's there
      env['PATH_INFO'] = cached_path
      return @file_app.call(env)
    end
  end

  # No static file, let Rails handle it
  @rails_app.call(env)
end

#file_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/rack/adapter/rails.rb', line 113

def file_exist?(path)
    full_path = ::File.join(@file_app.root, Utils.unescape(path))
  ::File.file?(full_path) && ::File.readable_real?(full_path)
end

#load_applicationObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rack/adapter/rails.rb', line 93

def load_application
  ENV['RAILS_ROOT'] = @root        
  ENV['RAILS_ENV'] = @env


  require "#{@root}/config/environment"        
  require 'dispatcher'

  setup_logger

  if !@prefix.nil? && @prefix != "/"
    if ActionController::Base.respond_to?('relative_url_root=') #for rails < 2.2
      ActionController::Base.relative_url_root = @prefix
    else
      ActionController::AbstractRequest.relative_url_root = @prefix
    end
  end
  
end

#rack_based?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/rack/adapter/rails.rb', line 87

def rack_based?
  ActionController.const_defined?(:Dispatcher) &&
    (ActionController::Dispatcher.instance_methods.include?(:call) ||
     ActionController::Dispatcher.instance_methods.include?("call"))
end