-
-
Save danmcclain/1317682 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# post.rb | |
class Post | |
include DataMapper::Resource | |
property :id, Serial | |
property :title, String, :required => true | |
property :body, Text, :required => true | |
property :slug, String | |
before :save, :make_slug | |
def make_slug | |
self.slug = title.downcase.gsub(/\W/,'-').squeeze('-').chomp('-') | |
end | |
end | |
# post_spec.rb | |
it "should make a slug from the title" do | |
post_with_generated_slug = FactoryGirl.Build(:post, title: "Star Wars") | |
post_with_generated_slug.save | |
post_with_generated_slug.slug.should == "star-wars" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment