TASK:
- constraint T&C policy routes according to its parameters.
EX:
- /en/us/legal/terms.f0a6f2aa-8e68-45ae-a875-84444ef1130f
constraints:
- languageCode – 2 alpha characters – EX: en/ fr/ sp
- country – 2 alpha characters – EX: us/ ca/ uk
- policy – 3 types – EX: Terms/ Privacy/ Electronic
- guid – standard – 32 hexadecimal digits – EX: 21EC2020-3AEA-1069-A2DD-08002B30309D
—————————————————————————————————————–
rspec:
note: the guid is located after the “.”, which works as a separator for formatted routes,
hence the :format=>guid instead of :guid => guid.
describe "GET policies" do it "should route to us privacy page" do { :get => '/en/us/legal/privacy.f0a6f2aa-8e68-45ae-a875-84444ef1130f'}.should route_to(:controller=>"pages", :action=>"privacy") end it "should route to ca terms page" do { :get => '/en/ca/legal/terms.f0a6f2aa-8e68-45ae-a875-84444ef1130f'}.should route_to(:controller=>"pages", :action=>"terms_ca") end it "should route to 404 page when given invalid policy parameter" do { :get => '/en/ca/legal/invalid.f0a6f2aa-8e68-45ae-a875-84444ef1130f'}.should route_to(:controller=> "errors", :action => "routing", :path=>"en/ca/legal/invalid", :format=> "f0a6f2aa-8e68-45ae-a875-84444ef1130f") end it "should route to 404 page when given invalid country parameter" do { :get => '/en/invalid/legal/terms.f0a6f2aa-8e68-45ae-a875-84444ef1130f'}.should route_to(:controller=> "errors", :action => "routing", :path=>"en/invalid/legal/terms", :format=> "f0a6f2aa-8e68-45ae-a875-84444ef1130f") end it "should route to 404 page when given invalid language parameter" do { :get => '/invalid/us/legal/privacy.f0a6f2aa-8e68-45ae-a875-84444ef1130f'}.should route_to(:controller=> "errors", :action => "routing", :path=>"invalid/ca/legal/privacy", :format=> "f0a6f2aa-8e68-45ae-a875-84444ef1130f") end it "should route to 404 page when given invalid guid" do { :get => '/en/us/legal/privacy.invalid'}.should route_to(:controller=> "errors", :action => "routing", :path=>"en/ca/legal/privacy", :format=> "invalid") end end
# routes.rb
constraints(PolicyConstraint) do get ':language/:country/legal/:policy.:guid' => "pages#policy_by_type", :constraints => {:language => /[a-zA-Z]{2}/, :country => /[a-zA-Z]{2}/, :guid=>/[\h]{8}-[\h]{4}-[\h]{4}-[\h]{4}-[\h]{12}/} end
An example of policy_by_type method that is used for filtering two countries: CA and US.
# pages_controller.rb
def policy_by_type if params[:country].upcase == "CA" case params[:policy] when "terms" redirect_to terms_ca_url when "privacy" redirect_to privacy_ca_url when "electronic_communication" redirect_to electronic_communication_ca_url else render_404 end else case params[:policy] when "terms" redirect_to terms_url when "privacy" redirect_to privacy_url when "electronic_communication" redirect_to electronic_communication_url else render_404 end end end
now for constraining policy segment:
# constraints/policy_constraint.rb
class PolicyConstraint def self.matches?(request) policy_types = ['terms', 'privacy', 'electronic'] policy_types.include?(request.parameters[:policy]) end end
and viola the route is now “constrained” (:
more info: http://guides.rubyonrails.org/routing.html