test/test_slug.rb in slug-0.5.0 vs test/test_slug.rb in slug-0.5.1
- old
+ new
@@ -10,10 +10,15 @@
should "base slug on specified source column" do
article = Article.create!(:headline => 'Test Headline')
assert_equal 'test-headline', article.slug
end
+ should "base slug on specified source column, even if it is defined as a method rather than database attribute" do
+ article = Event.create!(:title => 'Test Event', :location => 'Portland')
+ assert_equal 'test-event-portland', article.slug
+ end
+
context "slug column" do
should "save slug to 'slug' column by default" do
article = Article.create!(:headline => 'Test Headline')
assert_equal 'test-headline', article.slug
end
@@ -22,33 +27,35 @@
person = Person.create!(:name => 'Test Person')
assert_equal 'test-person', person.web_slug
end
end
- context "setup validations" do
- teardown do
- Person.slug(:name, :column => :web_slug) # Reset Person slug column to valid config.
- end
-
+ context "column validations" do
should "raise ArgumentError if an invalid source column is passed" do
- assert_raises(ArgumentError) { Person.slug(:invalid_source_column) }
+ Company.slug(:invalid_source_column)
+ assert_raises(ArgumentError) { Company.create! }
end
should "raise an ArgumentError if an invalid slug column is passed" do
- assert_raises(ArgumentError) { Person.slug(:name, :column => :bad_slug_column)}
+ Company.slug(:name, :column => :bad_slug_column)
+ assert_raises(ArgumentError) { Company.create! }
end
end
should "set validation error if source column is empty" do
article = Article.create
assert !article.valid?
+ require 'ruby-debug'
+ debugger if article.errors.count > 1
+ assert_equal 1, article.errors.count
assert article.errors.on(:slug)
end
should "set validation error if normalization makes source value empty" do
article = Article.create(:headline => '---')
assert !article.valid?
+ assert_equal 1, article.errors.count
assert article.errors.on(:slug)
end
should "not update the slug even if the source column changes" do
article = Article.create!(:headline => 'Test Headline')
@@ -105,9 +112,15 @@
should "should strip leading dashes" do
@article.headline = '-ab'
@article.save!
assert_match 'ab', @article.slug
+ end
+
+ should "remove double-dashes" do
+ @article.headline = 'a--b--c'
+ @article.save!
+ assert_match 'a-b-c', @article.slug
end
should "should not modify valid slug strings" do
@article.headline = 'a-b-c-d'
@article.save!
\ No newline at end of file