Skip to content

Instantly share code, notes, and snippets.

@danmcclain
Forked from HeroicEric/gist:1317499
Created October 26, 2011 20:16
Show Gist options
  • Save danmcclain/1317682 to your computer and use it in GitHub Desktop.
Save danmcclain/1317682 to your computer and use it in GitHub Desktop.
# 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