2010-01-11 09:09:50 +00:00
|
|
|
class RerouteMiddleware
|
|
|
|
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
|
|
|
|
#Rails.logger.debug env.to_yaml
|
|
|
|
return @app.call(env) if env['REQUEST_URI'] =~ /^\/admin/
|
|
|
|
|
2010-02-22 07:27:11 +00:00
|
|
|
env['REQUEST_URI'] =~ /^\/([0-9a-zA-Z\-_\/]*)/
|
2010-01-11 09:09:50 +00:00
|
|
|
parsed_entry_name = $1
|
|
|
|
|
2010-02-11 08:46:20 +00:00
|
|
|
entry = Item.find_by_full_name( parsed_entry_name )
|
2010-01-28 09:24:47 +00:00
|
|
|
|
|
|
|
case entry.type.to_s
|
|
|
|
when 'Component' :
|
2010-01-18 08:23:33 +00:00
|
|
|
env['REQUEST_URI'] = env['REQUEST_URI'].sub!(parsed_entry_name, entry.engine_name)
|
2010-01-11 09:09:50 +00:00
|
|
|
env['QUERY_STRING'] = (env['QUERY_STRING'].blank?)? "entry_name=#{parsed_entry_name}" : "entry_name=#{parsed_entry_name}&#{env['QUERY_STRING']}"
|
2010-01-28 09:24:47 +00:00
|
|
|
when 'Link' :
|
|
|
|
return [301, {'Location' => entry.url }, ['See Ya!']]
|
2010-01-11 09:09:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|