Skip to content

Commit

Permalink
tweaks for @bhamman to allow uniqueness validation to be turned off
Browse files Browse the repository at this point in the history
  • Loading branch information
bkoski committed Apr 29, 2010
1 parent 4d28d2b commit 346218f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/slug/slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ module ClassMethods
#
# Options:
# * <tt>:column</tt> - the column the slug will be saved to (defaults to <tt>:slug</tt>)
# * <tt>:validates_uniquness_if</tt> - proc to determine whether uniqueness validation runs, same format as validates_uniquness_of :if
#
# Slug will take care of validating presence and uniqueness of slug. It will generate the slug before create;
# subsequent changes to the source column will have no effect on the slug. If you'd like to update the slug
# later on, call <tt>@model.set_slug</tt>
# Slug will take care of validating presence and uniqueness of slug.

# Before create, Slug will generate and assign the slug if it wasn't explicitly set.
# Note that subsequent changes to the source column will have no effect on the slug.
# If you'd like to update the slug later on, call <tt>@model.set_slug</tt>
def slug source, opts={}
class_inheritable_accessor :slug_source, :slug_column
include InstanceMethods

self.slug_source = source

self.slug_column = opts.has_key?(:column) ? opts[:column] : :slug

uniqueness_opts = {}
uniqueness_opts.merge!(:if => opts[:validates_uniqueness_if]) if opts[:validates_uniqueness_if].present?

validates_presence_of self.slug_column, :message => "cannot be blank. Is #{self.slug_source} sluggable?"
validates_uniqueness_of self.slug_column
validates_uniqueness_of self.slug_column, uniqueness_opts
validates_format_of self.slug_column, :with => /^[a-z0-9-]+$/, :message => "contains invalid characters. Only downcase letters, numbers, and '-' are allowed."
before_validation_on_create :set_slug
end
Expand All @@ -32,7 +38,7 @@ module InstanceMethods
# Sets the slug. Called before create.
def set_slug
validate_slug_columns
self[self.slug_column] = self.send(self.slug_source)
self[self.slug_column] = self.send(self.slug_source) if self[self.slug_column].blank?

strip_diacritics_from_slug
normalize_slug
Expand Down
4 changes: 4 additions & 0 deletions test/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Company < ActiveRecord::Base
slug :name
end

class Post < ActiveRecord::Base
slug :headline, :validates_uniqueness_if => Proc.new { false }
end

# Used to test slugs based on methods rather than database attributes
class Event < ActiveRecord::Base
slug :title_for_slug
Expand Down
5 changes: 5 additions & 0 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
t.column "slug", "string"
end

create_table "posts", :force => true do |t|
t.column "headline", "string"
t.column "slug", "string"
end

create_table "events", :force => true do |t|
t.column "title", "string"
t.column "location", "string"
Expand Down
23 changes: 23 additions & 0 deletions test/test_slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,30 @@ def setup
assert !article.valid?
assert article.errors[:slug].present?
end

should "validate uniqueness of slug by default" do
article1 = Article.create!(:headline => 'Test Headline')
article2 = Article.create!(:headline => 'Test Headline')
article2.slug = 'test-headline'

assert !article2.valid?
assert article2.errors[:slug].present?
end

should "use validate_uniquness_if proc to decide whether uniqueness validation applies" do
article1 = Post.create!(:headline => 'Test Headline')
article2 = Post.new
article2.slug = 'test-headline'

assert article2.valid?
end

should "not overwrite slug value on create if it was already specified" do
a = Article.create!(:headline => 'Test Headline', :slug => 'slug1')
assert_equal 'slug1', a.slug
end


context "slug normalization" do
setup do
@article = Article.new
Expand Down

0 comments on commit 346218f

Please sign in to comment.