Permalink
Browse files

#174 Page slug format validation.

  • Loading branch information...
1 parent 0a3b5d9 commit d55822d84d801fb3e2ca4970952dd2c1855e70ce @huacnlee huacnlee committed Jan 9, 2012
Showing with 18 additions and 0 deletions.
  1. +1 −0 app/models/page.rb
  2. +17 −0 spec/models/page_spec.rb
View
@@ -32,6 +32,7 @@ class Page
# 当需要记录版本时,如果是更新,那么要求填写 :change_desc
validates_presence_of :user_id, :if => Proc.new { |p| p.version_enable == true }
validates_presence_of :change_desc, :if => Proc.new { |p| p.version_enable == true and !p.new_record? }
+ validates_format_of :slug, :with => /^[a-z0-9\-_]+$/
validates_uniqueness_of :slug
searchable do
View
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe Page do
+ it 'slug has check format' do
+ page = Page.create(:title => "Foo bar", :slug => "foo.bar")
+ page.should have(1).error_on(:slug)
+
+ page = Page.create(:title => "Foo bar", :slug => "foo-bar")
+ page.should_not have(1).error_on(:slug)
+
+ page = Page.create(:title => "Foo bar", :slug => "foo_bar")
+ page.should_not have(1).error_on(:slug)
+
+ page = Page.create(:title => "Foo bar", :slug => "foo-bar-1")
+ page.should_not have(1).error_on(:slug)
+ end
+end

0 comments on commit d55822d

Please sign in to comment.