Skip to content

Commit

Permalink
Fix permalink increments when number is more than one digit long.
Browse files Browse the repository at this point in the history
Merges spree#987
  • Loading branch information
ebeigarts authored and radar committed Jan 18, 2012
1 parent f6bc515 commit b4cab9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/lib/spree/core/permalinks.rb
Expand Up @@ -40,7 +40,10 @@ def save_permalink
permalink_value = self.to_param
field = self.class.permalink_field
# Do other links exist with this permalink?
other = self.class.first(:conditions => "#{field} LIKE '#{permalink_value}%'", :order => "#{field} DESC")
other = self.class.first(
:conditions => "#{field} LIKE '#{permalink_value}%'",
:order => "LENGTH(#{field}) DESC, #{field} DESC"
)
if other
# Find the number of that permalink and add one.
if /-(\d+)$/.match(other.send(field))
Expand Down
11 changes: 11 additions & 0 deletions core/spec/models/product_spec.rb
Expand Up @@ -60,6 +60,17 @@
end
end

context "permalink should be incremented until the value is not taken when there are more than 10 products" do
before do
@products = 0.upto(11).map do
Factory(:product, :name => 'foo')
end
end
it "should have valid permalink" do
@products[11].permalink.should == 'foo-11'
end
end

context "make_permalink should declare validates_uniqueness_of" do
before do
@product1 = Factory(:product, :name => 'foo')
Expand Down

0 comments on commit b4cab9e

Please sign in to comment.