Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ActionController::Routing::RouteSet::Mapper.class_eval do
protected
def map_unnamed_routes(map, path_without_format, options)
map.connect("#{path_without_format}.:format", options)
# map.connect("#{path_without_format}.:format", options)
end
def map_named_routes(map, name, path_without_format, options)
map.named_route(name, "#{path_without_format}.:format", options)
# map.named_route("formatted_#{name}", "#{path_without_format}.:format", options)
end
end
module ActionController
module Routing
class OptionalFormatSegment < DynamicSegment
def initialize(value = nil, options = {})
super(:format, {:optional => true, :regex => /\.(.*)?/}.merge(options))
end
# When building the url with url_for, put the dot back in.
def interpolation_chunk
"." + super
end
def regexp_chunk
'([^/?]+)' #intentionally exclude .
end
def to_s
'(.:format)?'
end
def match_extraction(next_capture)
# All non code-related keys (such as :id, :slug) are URI-unescaped as
# path parameters.
default_value = default ? default.inspect : nil
%[
value = if (m = match[#{next_capture}])
URI.unescape(m)
else
#{default_value}
end
params[:#{key}] = value.from(1) if value
]
end
end
end
end
ActionController::Routing::RouteBuilder.class_eval do
def segment_for_with_optional_format(string)
if string =~ /\A\.(.*)?\//
[::ActionController::Routing::OptionalFormatSegment.new($1),$~.post_match]
else
segment_for_without_optional_format(string)
end
end
alias_method_chain :segment_for, :optional_format
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment