Skip to content
Browse files

- Override to_param to refer to the slug column

- only include instance methods after calling slugify (so that to_param doesn't leak out before calling slugify)
  • Loading branch information...
1 parent 05bf6e4 commit 7a50b8136a3f4fb46e3370007f688e1c9a926ce0 @smtlaissezfaire committed Aug 10, 2010
Showing with 15 additions and 3 deletions.
  1. +6 −3 lib/slugify.rb
  2. +9 −0 spec/slugify/slug_spec.rb
View
9 lib/slugify.rb
@@ -6,9 +6,6 @@ module Slugify
def self.append_features(other)
other.extend ClassMethods
- other.class_eval do
- include InstanceMethods
- end
end
module ClassMethods
@@ -31,6 +28,8 @@ def slugify(source_slug_column, options_given={})
self.slug_column = options[:slug_column]
self.slug_scope = options[:scope]
self.slugify_when = options[:when]
+
+ include InstanceMethods
end
private
@@ -52,5 +51,9 @@ def generate_slug
def regenerate_slug
Slugify::SlugGenerator.regenerate(self)
end
+
+ def to_param
+ read_attribute(self.class.slug_column)
+ end
end
end
View
9 spec/slugify/slug_spec.rb
@@ -53,6 +53,15 @@ def create_user(attributes={})
end
end
+ describe "to_param" do
+ it "should return the slug" do
+ u = new_user(:name => "foo")
+ u.generate_slug
+
+ u.to_param.should == "foo"
+ end
+ end
+
describe "slug escaping" do
it "should replace uppercase with lowercase" do
u = new_user(:name => "Scott")

0 comments on commit 7a50b81

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