Skip to content
Browse files

Optional slug modifier block, bumped to 2.3.3

  • Loading branch information...
1 parent 5789de6 commit 7bee20d72d385e0c0243f6620ad391c5901389dc @ismasan committed
Showing with 29 additions and 2 deletions.
  1. +8 −0 README.markdown
  2. +3 −1 lib/sluggable_finder/orm.rb
  3. +1 −1 lib/sluggable_finder/version.rb
  4. +17 −0 spec/sluggable_finder_spec.rb
View
8 README.markdown
@@ -81,6 +81,14 @@ Slug uniqueness will be checked accross all classes in STI models. If you want t
sluggable_finder :title, :ignore_sti => true
end
+You can pass an optional block to pre-process the slug before saving
+
+ class Comment < SomeParentClass
+ sluggable_finder :title do |slug_candidate|
+ "comment-#{slug_candidate}"
+ end
+ end
+
### Controllers
You can do Model.find(slug) just how you would with a single numerical ID. It will also raise a RecordNotFound exception so you can handle that in your application controller.
View
4 lib/sluggable_finder/orm.rb
@@ -3,7 +3,7 @@ module Orm
module ClassMethods
- def sluggable_finder(field = :title, options = {})
+ def sluggable_finder(field = :title, options = {}, &slug_modifier)
return if self.included_modules.include?(SluggableFinder::Orm::InstanceMethods)
extend SluggableFinder::Finder
extend SluggableFinder::BaseFinder
@@ -21,6 +21,7 @@ class << self
:reserved_slugs => [],
:allow_integer_ids => true,
:upcase => false,
+ :slug_modifier => slug_modifier || nil,
:ignore_sti => false # if true, Uniqueness won't check sibling classes.
}.merge( options ))
class_inheritable_reader :sluggable_finder_options
@@ -76,6 +77,7 @@ def set_slug
def get_value_or_generate_random(column_name)
v = self.send(column_name)
+ v = sluggable_finder_options[:slug_modifier].call(v) if sluggable_finder_options[:slug_modifier]
v || SluggableFinder.random_slug_for(self.class)
end
View
2 lib/sluggable_finder/version.rb
@@ -1,3 +1,3 @@
module SluggableFinder
- VERSION = '2.3.2'
+ VERSION = '2.3.3'
end
View
17 spec/sluggable_finder_spec.rb
@@ -39,6 +39,12 @@ class SimpleItem < Item
sluggable_finder :title, :reserved_slugs => ['admin','settings'] # defaults :to => :slug
end
+class SimpleItemWithBlock < Item
+ sluggable_finder :title do |slug_candidate|
+ slug_candidate =~ /\A\d+\Z/ ? "p#{slug_candidate}" : slug_candidate
+ end
+end
+
class SimpleUpcaseItem < Item
sluggable_finder :title, :upcase => true
end
@@ -142,6 +148,17 @@ class ScopedItem < Item
end
end
+ describe 'with modifier block' do
+
+ it 'should pass slug through block before saving' do
+ item1 = SimpleItemWithBlock.create!(:title => 'Hello World')
+ item1.to_param.should == 'hello-world'
+
+ item2 = SimpleItemWithBlock.create!(:title => '1234')
+ item2.to_param.should == 'p1234'
+ end
+ end
+
describe SimpleUpcaseItem, 'uppercase slugs' do
it 'should uppercase slugs' do

0 comments on commit 7bee20d

Please sign in to comment.
Something went wrong with that request. Please try again.