Skip to content
Browse files

* make page name validation more user friendly.

  • Loading branch information...
1 parent 1100e53 commit fbbc5f5fc9a82e3e8a58002af7bef74e7703da85 @kou committed Mar 15, 2009
Showing with 79 additions and 51 deletions.
  1. +21 −0 app/models/page.rb
  2. +6 −42 app/models/site.rb
  3. +25 −0 lib/string_converter.rb
  4. +27 −9 test/unit/page_test.rb
View
21 app/models/page.rb
@@ -1,4 +1,6 @@
class Page < ActiveRecord::Base
+ include AsciiValueValidator
+
has_many :contents, :dependent => :destroy do
def find_or_create_by_language(language)
content = method_missing(:find_or_create_by_language, language)
@@ -13,6 +15,9 @@ def find_or_create_by_language(language)
validates_presence_of :name
validates_uniqueness_of :name
+ validate :validate_name
+ before_validation :repair_name
+
def sorted_contents
AVAILABLE_LANGUAGES.collect do |language|
contents.find_or_create_by_language(language)
@@ -30,4 +35,20 @@ def sorted_available_contents
def html_path(directory=nil)
File.join(*[directory, "#{name}.html"].compact)
end
+
+ private
+ def repair_name
+ return if name.blank?
+ normalized_name = StringConverter.zenkaku_to_hankaku(name)
+ normalized_name = normalized_name.strip
+ self.name = normalized_name
+ end
+
+ def validate_name
+ return if name.blank?
+ validate_ascii_value(:name, name,
+ :accept_hyphen => true,
+ :accept_underscore => true,
+ :default_is_valid => true)
+ end
end
View
48 app/models/site.rb
@@ -3,6 +3,8 @@
require 'uri'
class Site < ActiveRecord::Base
+ include AsciiValueValidator
+
DEFAULT_NAME = "default"
class << self
def default
@@ -23,37 +25,16 @@ def have_ftp_configuration?
end
private
- def zenkaku_to_hankaku(string)
- string.gsub(/[A-Za-z。.ー/ ]/u) do |zenkaku|
- case zenkaku
- when /[A-Z]/
- [zenkaku.unpack("U")[0] - "".unpack("U")[0] + "A".unpack("U")[0]].pack("U")
- when /[a-z]/
- [zenkaku.unpack("U")[0] - "".unpack("U")[0] + "a".unpack("U")[0]].pack("U")
- when "", ""
- "."
- when ""
- "-"
- when " "
- " "
- when ""
- "/"
- else
- zenkaku
- end
- end
- end
-
def repair_ftp_host
return if ftp_host.blank?
- normalized_ftp_host = zenkaku_to_hankaku(ftp_host)
+ normalized_ftp_host = StringConverter.zenkaku_to_hankaku(ftp_host)
normalized_ftp_host = normalized_ftp_host.strip
self.ftp_host = normalized_ftp_host
end
def repair_ftp_path
return if ftp_path.blank?
- normalized_ftp_path = zenkaku_to_hankaku(ftp_path)
+ normalized_ftp_path = StringConverter.zenkaku_to_hankaku(ftp_path)
normalized_ftp_path = normalized_ftp_path.strip
unless normalized_ftp_path.starts_with?("/")
normalized_ftp_path = "/#{normalized_ftp_path}"
@@ -64,29 +45,12 @@ def repair_ftp_path
def validate_ftp_host
return if ftp_host.blank?
return if URI::HOST =~ ftp_host
- add_invalid_ascii_value_error(:ftp_host, ftp_host)
+ validate_ascii_value(:ftp_host, ftp_host)
end
def validate_ftp_path
return if ftp_path.blank?
return if URI::ABS_PATH =~ ftp_path
- add_invalid_ascii_value_error(:ftp_path, ftp_path, :accept_slash => true)
- end
-
- def add_invalid_ascii_value_error(key, value, options={})
- case value
- when /\s/
- errors.add(key, :have_space, :value => value)
- when /[?:;'"!@\#$%^&*()\-_+=|~`\[\]{}]/
- errors.add(key, :have_symbol, :value => value)
- when /[^a-zA-Z0-9]/
- errors.add(key, :have_japanese, :value => value)
- else
- if /\\/ =~ value and !options[:accept_slash]
- errors.add(key, :have_symbol, :value => value)
- else
- errors.add(key, :invalid, :value => value)
- end
- end
+ validate_ascii_value(:ftp_path, ftp_path, :accept_slash => true)
end
end
View
25 lib/string_converter.rb
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+
+module StringConverter
+ module_function
+ def zenkaku_to_hankaku(string)
+ string.gsub(/[A-Za-z。.ー/ ]/u) do |zenkaku|
+ case zenkaku
+ when /[A-Z]/
+ [zenkaku.unpack("U")[0] - "".unpack("U")[0] + "A".unpack("U")[0]].pack("U")
+ when /[a-z]/
+ [zenkaku.unpack("U")[0] - "".unpack("U")[0] + "a".unpack("U")[0]].pack("U")
+ when "", ""
+ "."
+ when ""
+ "-"
+ when " "
+ " "
+ when ""
+ "/"
+ else
+ zenkaku
+ end
+ end
+ end
+end
View
36 test/unit/page_test.rb
@@ -33,18 +33,36 @@ def test_find_or_create_by_language(language)
assert_equal(content.title, "new-page")
end
- def test_name_validation_blank
- page = pages(:index)
- page.name = ""
- assert_not_valid(["名前 を入力してください。"], page)
+ def test_name_validation
+ assert_valid_name("new-page")
+ assert_valid_name("page1")
+
+ page = assert_valid_name("welcome")
+ assert_equal("welcome", page.name)
+
+ page = assert_valid_name(" new_page ")
+ assert_equal("new_page", page.name)
+
+ assert_not_valid_name(["名前 を入力してください。"], "")
+ assert_not_valid_name(["名前 が重複しています。"], pages(:introduction).name)
+ assert_not_valid_name(["名前 にスペースが入っています。"], "new page")
+ assert_not_valid_name(["名前 に記号が入っています。"], "page?")
+ assert_not_valid_name(["名前 に記号が入っています。"], "/")
+ assert_not_valid_name(["名前 に日本語が入っています。"], "ページ")
end
- def test_name_validation_duplicated
- page = Page.new(:name => "new-page")
+ private
+ def assert_valid_name(value)
+ page = Page.find(pages(:index))
+ page.name = value
assert_valid(page)
- page.save!
+ page
+ end
- duplicated_name_page = Page.new(:name => "new-page")
- assert_not_valid(["名前 が重複しています。"], duplicated_name_page)
+ def assert_not_valid_name(expected_errors, value)
+ page = Page.find(pages(:index))
+ page.name = value
+ assert_not_valid(expected_errors, page)
+ page
end
end

0 comments on commit fbbc5f5

Please sign in to comment.
Something went wrong with that request. Please try again.