Enrutamiento de bastidor de prueba con rSpec
Tengo una regla en mis rutas.rb:
constraints AssetRestrictor do
match '*seopath' => SeoDispatcher
end
Entonces en lib / seo_dispatcher.rb, tengo esto:
class SeoDispatcher
AD_KEY = "action_dispatch.request.path_parameters"
def self.call(env)
seopath = env[AD_KEY][:seopath]
if seopath
params = seopath.split('/') # get array of path components
env[AD_KEY][:id] = params.last # the real page name is the last element
env[AD_KEY][:category] = params.first if params.length > 1
end
Rails.logger.debug "routing to show #{env[AD_KEY]}"
PagesController.action(:show).call(env)
# TODO error handling for invalid paths
end
end
class AssetRestrictor
EXCEPTION_FILES = ['javascripts', 'stylesheets', 'autodiscover']
def self.matches?(request)
return false if request.method == 'POST' # no post requests are SEO-ed
EXCEPTION_FILES.each do |ex|
return false if request.url.include?(ex)
end
true
end
end
Y, básicamente, todo funciona. La idea es despegar el último componente de ruta y vincularlo con la babosa de una página. La gente de SEO me dice que esta es la mejor manera de engañar a los motores de búsqueda para que te ubiquen más alt
ejando de lado mis comentarios sarcásticos, tengo problemas para escribir una prueba en rSpec que ejercite este código. Mi primer intento fue:
describe SeoDispatcher do
it "routes /insurance/charters-and-guides/how-to-buy-charter-boat-insurance to pages#show :id => how-to-buy-charter-boat-insurance" do
{ :get => "/insurance/charters-and-guides/how-to-buy-charter-boat-insurance"}.should route_to(
:controller => 'pages',
:action => 'show',
:id => 'how-to-buy-charter-boat-insurance'
)
end
end
Pero esto simplemente no ejerce el código de envío de Rack. ¿Alguien sabe cómo se podría ejercer el código? Realmente quiero traer los parámetros desde Rack en lugar de simplemente hacerSeoDispatcher.call({"action_dispatch.request.path_parameters" => {:seopath => "/foo/bar"}})
.
¡Gracias