Skip to content

Commit

Permalink
:separator option for custom unique suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
ismasan committed Oct 2, 2014
1 parent 10aa7ca commit 9e4c6fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/sluggable_finder/orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class << self
:allow_integer_ids => true,
:upcase => false,
:slug_modifier => slug_modifier || nil,
:separator => '-',
:ignore_sti => false # if true, Uniqueness won't check sibling classes.
}.merge( options ))
class_inheritable_reader :sluggable_finder_options
Expand Down Expand Up @@ -108,7 +109,7 @@ def create_sluggable_slug
existing = slugable_class.find(:first, :conditions => [conds_sql, proposed_slug + suffix])
while existing != nil or sluggable_finder_options[:reserved_slugs].include?(proposed_slug + suffix)
if suffix.empty?
suffix = "-2"
suffix = "#{sluggable_finder_options[:separator]}2"
else
suffix.succ!
end
Expand Down
15 changes: 15 additions & 0 deletions spec/sluggable_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class ScopedItem < Item
sluggable_finder :title, :scope => :category_id
end

class DifferentSeparatorItem < Item
sluggable_finder :title, :separator => ':'
end

describe "random slugs" do
it "should generate random slugs" do
SluggableFinder.random_slug_for(String).should_not be_nil
Expand Down Expand Up @@ -148,6 +152,17 @@ class ScopedItem < Item
end
end

describe 'with custom suffix separator' do
before(:each) do
DifferentSeparatorItem.create!(:title => 'A title')
@i2 = DifferentSeparatorItem.create!(:title => 'A title')
end

it 'creates unique slug with custom suffix separator' do
@i2.slug.should == 'a-title:2'
end
end

describe 'with modifier block' do

it 'should pass slug through block before saving' do
Expand Down

0 comments on commit 9e4c6fc

Please sign in to comment.