Permalink
Browse files

* make FTP host validation more user friendly.

  • Loading branch information...
1 parent 971f1ab commit ba06fdb642c152a822cb2701b9b1e8f2c57c4c6b @kou committed Mar 15, 2009
Showing with 51 additions and 3 deletions.
  1. +35 −2 app/models/site.rb
  2. +5 −0 config/locales/ja.yml
  3. +11 −1 test/unit/site_test.rb
View
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
require 'uri'
class Site < ActiveRecord::Base
@@ -8,7 +10,7 @@ def default
end
end
- validates_format_of :ftp_host, :with => URI::HOST, :allow_blank => true
+ validate :validate_ftp_host
validates_format_of :ftp_path, :with => URI::ABS_PATH, :allow_blank => true
before_validation :repair_ftp_host, :repair_ftp_path
@@ -24,12 +26,43 @@ def have_ftp_configuration?
private
def repair_ftp_host
return if ftp_host.blank?
- self.ftp_host = ftp_host.strip
+ normalized_ftp_host = ftp_host.gsub(/ /u, ' ')
+ normalized_ftp_host = ftp_host.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 ""
+ "-"
+ else
+ zenkaku
+ end
+ end
+ normalized_ftp_host = normalized_ftp_host.strip
+ self.ftp_host = normalized_ftp_host
end
def repair_ftp_path
return if ftp_path.blank?
self.ftp_path = ftp_path.strip
self.ftp_path = "/#{ftp_path}" unless ftp_path.starts_with?("/")
end
+
+ def validate_ftp_host
+ return if ftp_host.blank?
+ return if URI::HOST =~ ftp_host
+ case ftp_host
+ when /\s/
+ errors.add(:ftp_host, :have_space, :value => ftp_host)
+ when /[?:;'"!@\#$%^&*()\-_+=\\|~`\[\]{}]/
+ errors.add(:ftp_host, :have_symbol, :value => ftp_host)
+ when /[^a-zA-Z0-9]/
+ errors.add(:ftp_host, :have_japanese, :value => ftp_host)
+ else
+ errors.add(:ftp_host, :invalid, :value => ftp_host)
+ end
+ end
end
View
@@ -94,6 +94,11 @@ ja:
site:
ftp_host: "FTPホスト"
ftp_path: "FTPパス"
+ errors:
+ messages:
+ have_space: "にスペースが入っています。"
+ have_symbol: "に記号が入っています。"
+ have_japanese: "に日本語が入っています。"
title:
controller:
View
@@ -20,9 +20,19 @@ def test_ftp_host_validation
site = assert_valid_attribute(:ftp_host, " ftp.example.com ")
assert_equal("ftp.example.com", site.ftp_host)
- assert_not_valid_attribute(["FTPホスト は不正な値です。"],
+ site = assert_valid_attribute(:ftp_host,
+ "ftp.example.com")
+ assert_equal("ftp.example.com", site.ftp_host)
+
+ assert_not_valid_attribute(["FTPホスト にスペースが入っています。"],
:ftp_host,
"example com")
+ assert_not_valid_attribute(["FTPホスト に記号が入っています。"],
+ :ftp_host,
+ "example.com?")
+ assert_not_valid_attribute(["FTPホスト に日本語が入っています。"],
+ :ftp_host,
+ "あいうえお")
end
def test_ftp_path_validation

0 comments on commit ba06fdb

Please sign in to comment.