-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIGRATION] adds Author#clean_url for slugs and fixed the sanitizatio…
…n with specs
- Loading branch information
Showing
14 changed files
with
122 additions
and
44 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
class AuthorsController < ApplicationController | ||
def show | ||
@author = Author.find_by_name(params[:author_slug]) | ||
@author = Author.find_by_clean_url(params[:author_slug]) | ||
raise ActiveRecord::RecordNotFound if @author.nil? | ||
end | ||
end |
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddCleanUrlToAuthor < ActiveRecord::Migration | ||
def self.up | ||
add_column :authors, :clean_url, :string | ||
add_index :authors, :clean_url | ||
end | ||
|
||
def self.down | ||
remove_index :authors, :clean_url | ||
end | ||
end |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
describe Author do | ||
describe 'clean_url' do | ||
it 'should sanitize and autoset the clean_url from name if clean_url is blank' do | ||
Author.create(:name => 'A. Author', :email => '[email protected]').clean_url.should == "A_Author" | ||
end | ||
|
||
it 'should sanitize crazy clean_urls' do | ||
Author.create(:name => 'A *Author1234_____..!', :email => '[email protected]').clean_url.should == "A_Author1234_" | ||
end | ||
|
||
it 'should not autoset the clean_url if clean_url is NOT blank' do | ||
a = Author.create(:name => 'A. Author', :email => '[email protected]', :clean_url => 'aauthor') | ||
a.clean_url.should == 'aauthor' | ||
end | ||
|
||
it 'should autoincrement if the clean_url already exists' do | ||
Author.create(:email => '[email protected]', :clean_url => 'an_author').clean_url.should == "an_author" | ||
Author.create(:email => '[email protected]', :clean_url => 'an_author').clean_url.should == "an_author2" | ||
end | ||
end | ||
|
||
describe 'name' do | ||
it 'should sanitize the name in case it is an email' do | ||
Author.create(:name => '[email protected]').name.should == 'a' | ||
Author.create(:email => '[email protected]').name.should == 'a' | ||
end | ||
|
||
it 'should be set to first part of email if nil' do | ||
a = Author.create(:email => '[email protected]') | ||
a.name.should == 'a' | ||
end | ||
|
||
it 'should autoincrement if the name already exists' do | ||
Author.create(:email => '[email protected]', :name => 'An Author').name.should == "An Author" | ||
Author.create(:email => '[email protected]', :name => 'An Author').name.should == "An Author (2)" | ||
end | ||
end | ||
|
||
describe 'validation' do | ||
it 'should not allow an author to be created without an email' do | ||
lambda do | ||
a = Author.create(:email => '', :name => 'ccc') | ||
a.should_not be_valid | ||
a.errors.on(:email).should include("can't be blank") | ||
end.should_not change(Author, :count) | ||
end | ||
end | ||
end | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -449,26 +449,6 @@ | |
end | ||
end | ||
|
||
|
||
describe Podcast, "finding or creating owner" do | ||
before do | ||
@podcast = Factory.build(:podcast, :title => "FOoooooobar", :author_email => "[email protected]") | ||
@save_podcast = lambda { @podcast.save } | ||
end | ||
|
||
it "should set and create the author if the author doesn't exist" do | ||
@save_podcast.should change { Author.all.size }.by(1) | ||
@podcast.author.should == Author.last | ||
@podcast.author.name.should == "some_owner" | ||
end | ||
|
||
it "should find and set the owner if owner exists" do | ||
author = Factory.create(:author, :email => @podcast.author_email) | ||
@save_podcast.should_not change { User.all.size } | ||
@podcast.author.should == author | ||
end | ||
end | ||
|
||
describe Podcast do | ||
before do | ||
@podcast = Factory.create(:podcast) | ||
|
@@ -488,3 +468,32 @@ | |
@podcast.been_reviewed_by?(nil).should be_false | ||
end | ||
end | ||
|
||
describe Podcast do | ||
before do | ||
@podcast = Factory.create(:podcast, :author_email => '[email protected]', :author_name => 'aaa') | ||
@podcast2 = Factory.create(:podcast, :author_email => '[email protected]', :author_name => 'bbb') | ||
end | ||
|
||
describe 'autocreating an author' do | ||
it 'should find or create the author based on email' do | ||
lambda { @podcast.author }.should change(Author, :count).by(1) | ||
lambda { @podcast.author }.should_not change(Author, :count) | ||
end | ||
|
||
it 'should autocreate the author with the same name and email' do | ||
@podcast.author.email.should == "[email protected]" | ||
@podcast.author.name.should == "aaa" | ||
end | ||
|
||
it 'should not create another author if it already exists' do | ||
@podcast.author | ||
lambda { @podcast2.author }.should_not change(Author, :count) | ||
end | ||
|
||
it 'should not overwrite the author name if it was already set' do | ||
@podcast.author | ||
@podcast2.author.name.should == 'aaa' | ||
end | ||
end | ||
end |