2010-03-20 02:26:30 +01:00
|
|
|
module RoutingFilter
|
2011-05-11 12:21:21 +02:00
|
|
|
class Foodcoop < Filter
|
2010-03-20 02:26:30 +01:00
|
|
|
def around_recognize(path, env, &block)
|
|
|
|
token = extract_token!(path) # remove the token from the beginning of the path
|
2011-05-11 00:58:53 +02:00
|
|
|
yield.tap do |params| # invoke the given block (calls more filters and finally routing)
|
2010-03-20 02:26:30 +01:00
|
|
|
params[:foodcoop] = token if token # set recognized token to the resulting params hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def around_generate(*args, &block)
|
|
|
|
token = args.extract_options!.delete(:foodcoop) # extract the passed :token option
|
|
|
|
token = Foodsoft.env if token.nil? # default to Foodsoft.env
|
|
|
|
|
2011-05-11 00:58:53 +02:00
|
|
|
yield.tap do |result|
|
2010-03-20 02:26:30 +01:00
|
|
|
if token
|
|
|
|
url = result.is_a?(Array) ? result.first : result
|
|
|
|
prepend_token!(url, token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def extract_token!(path)
|
|
|
|
foodcoop = nil
|
|
|
|
path.sub! %r(^/([a-zA-Z0-9]*)(?=/|$)) do foodcoop = $1; '' end
|
|
|
|
foodcoop
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepend_token!(url, token)
|
|
|
|
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{token}#{$2}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|