Skip to content

Commit

Permalink
Merge branch 'fix/include_exceptions_for_reverse'
Browse files Browse the repository at this point in the history
  • Loading branch information
learnjin committed Dec 14, 2015
2 parents e6e4c9a + acc7b7e commit 4120ae3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
== 0.16.1

* include exception list for reverse translations

== 0.16.0

* Makes rewritten work with fullpath. Example: Matching on '/some/url?q=1' will work now
Expand Down
14 changes: 11 additions & 3 deletions lib/rack/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Url
def initialize(app, &block)
@app = app
@translate_backwards = false
@translate_backwards_exceptions = []
@downcase_before_lookup = false
@translate_partial = false
@base_url = ''
Expand Down Expand Up @@ -36,7 +37,7 @@ def call(env)
r = Rack::Response.new
r.redirect(target_url, 301)
r.finish
elsif ::Rewritten.includes?(req.fullpath) || ::Rewritten.includes?(path.chomp('/')) || backwards = (translate_backwards? && ::Rewritten.exist_translation_for?(path))
elsif ::Rewritten.includes?(req.fullpath) || ::Rewritten.includes?(path.chomp('/')) || backwards = (translate_backwards?(path) && ::Rewritten.exist_translation_for?(path))

to = ::Rewritten.includes?(req.fullpath)
to ||= ::Rewritten.includes?(path.chomp('/')) || path
Expand Down Expand Up @@ -80,11 +81,18 @@ def split_to_path_params(path_and_query)

private

def translate_backwards?
@translate_backwards
def translate_backwards?(path)
return false unless @translate_backwards

@translate_backwards_exceptions.each do |exception|
return false if path.index(exception) == 0
end

true
end

attr_writer :translate_backwards
attr_accessor :translate_backwards_exceptions

def downcase_before_lookup?
@downcase_before_lookup
Expand Down
2 changes: 1 addition & 1 deletion lib/rewritten/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Rewritten
VERSION = '0.16.0'
VERSION = '0.16.1'
end
13 changes: 13 additions & 0 deletions test/rack/rewritten_url_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ def request_url(url, params = {})
ret[0].must_equal 301
ret[1]['Location'].must_equal 'http://www.example.org/foo/baz'
end

it 'must not redirect from resource url to nice url if enabled but in exceptions' do
@rack = Rack::Rewritten::Url.new(@app) do
self.translate_backwards = true
self.translate_backwards_exceptions = ['/products']
end

@app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
ret = @rack.call request_url('/products/1')
@app.verify
ret[0].must_equal 200
end

end

describe 'flag behavior' do
Expand Down

0 comments on commit 4120ae3

Please sign in to comment.