Created
January 7, 2013 16:10
-
-
Save lukebergen/4476132 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe Mongoid::Sluggable do | |
[:article, :brand, :author, :product].each do |subject| | |
describe subject.to_s.camelcase do | |
before(:each) do | |
@klass = subject.to_s.camelcase.constantize | |
@subject = build(subject) | |
@subject.stub!(:slug_base).and_return("This is My Slug Base!") | |
end | |
describe 'validations' do | |
it "parameterizes slug components, not full slug" do | |
subject1 = create(subject, name: "My Big Article!", slug: "neato-article/page1") | |
subject1.slug.should eql("neato-article/page1") | |
subject1.slug = "neato-article/pageone" | |
subject1.save | |
subject1.slug.should eql("neato-article/pageone") | |
end | |
describe "slug_uniqueness" do | |
it "checks uniqueness" do | |
author1 = create(:author, name: "My Big Article!", slug: "my-big-article") | |
author2 = build(:author, name: author1.name, slug: author1.slug) | |
author2.save.should eql(false) | |
end | |
end | |
describe "associations" do | |
it 'has_many histories' do | |
@subject.should respond_to(:slug_histories) | |
end | |
end | |
end | |
describe '#generate_slug' do | |
it "generates a new slug" do | |
@subject.slug.should be_blank | |
@subject.generate_slug | |
@subject.slug.should eql("this-is-my-slug-base") | |
end | |
it "generates a unique slug" do | |
subject1 = create(subject, name: "My Big Article!", slug: "my-big-article") | |
subject2 = build(subject, name: subject1.name) | |
subject2.stub!(:slug_base).and_return(subject1.slug) | |
subject2.slug.should be_blank | |
subject2.generate_slug | |
subject2.slug.should eql("#{subject1.slug}-2") | |
end | |
it "considers full string of slug for uniqueness check" do | |
subject1 = create(subject, name: "abcdefghij", slug: "abcdefghij") | |
subject2 = build(subject, name: "abcde") | |
subject2.slug.should be_blank | |
subject2.generate_slug | |
subject2.slug.should eql("abcde") | |
end | |
end | |
describe '.by_slug_or_id' do | |
before(:each) do | |
@subject.save! | |
@subject = @klass.find(@subject.id) | |
end | |
it "returns an object by it's id" do | |
@klass.by_slug_or_id(@subject.id).first.should eql(@subject) | |
end | |
it "returns an object by it's slug" do | |
@klass.by_slug_or_id(@subject.slug).first.should eql(@subject) | |
end | |
end | |
end | |
end | |
describe "callbacks" do | |
describe "add_slug_history" do | |
before :each do | |
@author = create(:author, name: "My Big Article!", slug: "my-big-article") | |
end | |
context "slug has changed" do | |
it "creates a slug_history" do | |
@author.slug = "my-new-slug" | |
expect { | |
@author.save | |
}.to change(SlugHistory.all, :count).by(1) | |
h = SlugHistory.last | |
h.source.should eql("my-big-article") | |
h.destination.should eql("my-new-slug") | |
h.sluggable_id.to_s.should eql(@author.id.to_s) | |
end | |
end | |
context "slug has not changed" do | |
it "does not create a slug_history" do | |
expect { | |
@author.save | |
}.to_not change(SlugHistory.all, :count) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment