Permalink
Browse files

* add page name validation.

  • Loading branch information...
1 parent d3f5348 commit 1100e533bbbd30c4310f7d5b59219f288cc0af59 @kou committed Mar 15, 2009
Showing with 20 additions and 0 deletions.
  1. +3 −0 app/models/page.rb
  2. +1 −0 config/locales/ja.yml
  3. +16 −0 test/unit/page_test.rb
View
3 app/models/page.rb
@@ -10,6 +10,9 @@ def find_or_create_by_language(language)
end
named_scope :sorted, :order => "name"
+ validates_presence_of :name
+ validates_uniqueness_of :name
+
def sorted_contents
AVAILABLE_LANGUAGES.collect do |language|
contents.find_or_create_by_language(language)
View
1 config/locales/ja.yml
@@ -102,6 +102,7 @@ ja:
have_space: "にスペースが入っています。"
have_symbol: "に記号が入っています。"
have_japanese: "に日本語が入っています。"
+ taken: "が重複しています。"
template:
header:
one: "{{model}}に問題があります。"
View
16 test/unit/page_test.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require 'test_helper'
class PageTest < ActiveSupport::TestCase
@@ -31,4 +32,19 @@ def test_find_or_create_by_language(language)
content = page.find_or_create_by_language("ja")
assert_equal(content.title, "new-page")
end
+
+ def test_name_validation_blank
+ page = pages(:index)
+ page.name = ""
+ assert_not_valid(["名前 を入力してください。"], page)
+ end
+
+ def test_name_validation_duplicated
+ page = Page.new(:name => "new-page")
+ assert_valid(page)
+ page.save!
+
+ duplicated_name_page = Page.new(:name => "new-page")
+ assert_not_valid(["名前 が重複しています。"], duplicated_name_page)
+ end
end

0 comments on commit 1100e53

Please sign in to comment.