Skip to content

Commit

Permalink
use polymorphic_url instead of url_for
Browse files Browse the repository at this point in the history
  • Loading branch information
learnjin committed Nov 19, 2013
1 parent 5dc4c17 commit f8e7c97
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
17 changes: 10 additions & 7 deletions lib/rewritten/document.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
module Rewritten
module Document

def rewritten_url=(new_translation)
Rewritten.add_translation(new_translation, url_for(self))
end

def rewritten_url
return "" unless persisted?
Rewritten.get_current_translation(url_for(self))
Rewritten.get_current_translation(polymorphic_url(self, :only_path => true))
end

def rewritten_url=(new_url)
if !new_url.nil? && new_url != "" && new_url != rewritten_url
Rewritten.add_translation(new_url, polymorphic_url(self, :only_path => true))
end
end

def rewritten_urls
return [] unless persisted?
Rewritten.get_all_translations(url_for(self))
Rewritten.get_all_translations(polymorphic_url(self, :only_path => true))
end

def has_rewritten_url?
Rewritten.exist_translation_for?(url_for(self))
Rewritten.exist_translation_for?(polymorphic_url(self, :only_path => true))
end

def remove_rewritten_urls
Rewritten.remove_all_translations(url_for(self))
Rewritten.remove_all_translations(polymorphic_url(self, :only_path => true))
end

end
Expand Down
14 changes: 12 additions & 2 deletions test/rewritten/document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Product
include Rewritten::Document
end
@instance = Product.new
def @instance.url_for(object); '/products/123'; end
def @instance.polymorphic_url(object, options={}); '/products/123'; end
def @instance.persisted?; true; end
end

Expand Down Expand Up @@ -35,7 +35,6 @@ def @instance.persisted?; false; end
Rewritten.add_translation('/foo/baz', '/products/123')

@instance.rewritten_urls.must_equal ['/foo/bar', '/foo/baz']

end

it 'must add a new translation' do
Expand All @@ -52,6 +51,17 @@ def @instance.persisted?; false; end
@instance.rewritten_urls.must_equal []
end

it 'must won\'t add blank and similar translations' do
@instance.rewritten_url = '/foo/bar'
@instance.rewritten_urls.must_equal ['/foo/bar']
@instance.rewritten_url = nil
@instance.rewritten_urls.must_equal ['/foo/bar']
@instance.rewritten_url = ""
@instance.rewritten_urls.must_equal ['/foo/bar']
@instance.rewritten_url = "/foo/bar"
@instance.rewritten_urls.must_equal ['/foo/bar']
end

end


Expand Down

0 comments on commit f8e7c97

Please sign in to comment.