Skip to content

Commit

Permalink
Fix permalink issue as described in spree#1254.
Browse files Browse the repository at this point in the history
  • Loading branch information
BGuimberteau authored and radar committed Mar 20, 2012
1 parent 105db90 commit 5379fef
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def variant_images
attr_accessor :option_values_hash
accepts_nested_attributes_for :product_properties, :allow_destroy => true, :reject_if => lambda { |pp| pp[:property_name].blank? }

make_permalink
make_permalink :order => :name

alias :options :product_option_types

Expand Down
29 changes: 20 additions & 9 deletions core/lib/spree/core/permalinks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,37 @@ def permalink_field
permalink_options[:field]
end

def permalink_order
order = permalink_options[:order]

if order
"#{order} ASC,"
end
end

end

def save_permalink
permalink_value = self.to_param
field = self.class.permalink_field
order = self.class.permalink_order
# Do other links exist with this permalink?
other = self.class.first(
:conditions => "#{field} LIKE '#{permalink_value}%'",
:order => "LENGTH(#{field}) DESC, #{field} DESC"
:order => "#{order} LENGTH(#{field}) DESC, #{field} DESC"
)
if other
# Find the number of that permalink and add one.
if /-(\d+)$/.match(other.send(field))
number = $1.to_i + 1
# Otherwise default to suffixing it with a 1.
else
number = 1
end
# Find existence of that permalink or the number of that permalink and add one.
if ( permalink_value == other.send(field) || /-(\d+)$/.match(other.send(field)) )
if $1
number = $1.to_i + 1
# Otherwise default to suffixing it with a 1.
else
number = 1
end

permalink_value += "-#{number.to_s}"
permalink_value += "-#{number.to_s}"
end
end
write_attribute(field, permalink_value)
end
Expand Down
26 changes: 26 additions & 0 deletions core/spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@
end
end

context "permalink should be incremented until the value is not taken for similar names" do
before do
@other_product = Factory(:product, :name => 'foo bar')
@product1 = Factory(:product, :name => 'foo')
@product2 = Factory(:product, :name => 'foo')
@product3 = Factory(:product, :name => 'foo')
end
it "should have valid permalink" do
@product1.permalink.should == 'foo'
@product2.permalink.should == 'foo-1'
@product3.permalink.should == 'foo-2'
end
end

context "permalink should be incremented until the value is not taken for similar names when there are more than 10 products" do
before do
@other_product = Factory(:product, :name => 'foo a')
@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 5379fef

Please sign in to comment.