Skip to content

Instantly share code, notes, and snippets.

@HeroicEric
Created October 26, 2011 19:23
Show Gist options
  • Save HeroicEric/1317499 to your computer and use it in GitHub Desktop.
Save HeroicEric/1317499 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 = Factory(:post, title: "Star Wars")
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