Permalink
Browse files

* make FTP path validation more user friendly.

  • Loading branch information...
kou committed Mar 15, 2009
1 parent ba06fdb commit 21956cb63e6d86111a992fad72b2f06aee0896ea
Showing with 51 additions and 15 deletions.
  1. +38 −14 app/models/site.rb
  2. +13 −1 test/unit/site_test.rb
View
@@ -10,8 +10,7 @@ def default
end
end
- validate :validate_ftp_host
- validates_format_of :ftp_path, :with => URI::ABS_PATH, :allow_blank => true
+ validate :validate_ftp_host, :validate_ftp_path
before_validation :repair_ftp_host, :repair_ftp_path
@@ -24,10 +23,8 @@ def have_ftp_configuration?
end
private
- def repair_ftp_host
- return if ftp_host.blank?
- normalized_ftp_host = ftp_host.gsub(/ /u, ' ')
- normalized_ftp_host = ftp_host.gsub(/[A-Za-z。.ー]/u) do |zenkaku|
+ 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")
@@ -37,32 +34,59 @@ def repair_ftp_host
"."
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 = 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?("/")
+ normalized_ftp_path = zenkaku_to_hankaku(ftp_path)
+ normalized_ftp_path = normalized_ftp_path.strip
+ unless normalized_ftp_path.starts_with?("/")
+ normalized_ftp_path = "/#{normalized_ftp_path}"
+ end
+ self.ftp_path = normalized_ftp_path
end
def validate_ftp_host
return if ftp_host.blank?
return if URI::HOST =~ ftp_host
- case ftp_host
+ add_invalid_ascii_value_error(: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(:ftp_host, :have_space, :value => ftp_host)
- when /[?:;'"!@\#$%^&*()\-_+=\\|~`\[\]{}]/
- errors.add(:ftp_host, :have_symbol, :value => ftp_host)
+ errors.add(key, :have_space, :value => value)
+ when /[?:;'"!@\#$%^&*()\-_+=|~`\[\]{}]/
+ errors.add(key, :have_symbol, :value => value)
when /[^a-zA-Z0-9]/
- errors.add(:ftp_host, :have_japanese, :value => ftp_host)
+ errors.add(key, :have_japanese, :value => value)
else
- errors.add(:ftp_host, :invalid, :value => ftp_host)
+ if /\\/ =~ value and !options[:accept_slash]
+ errors.add(key, :have_symbol, :value => value)
+ else
+ errors.add(key, :invalid, :value => value)
+ end
end
end
end
View
@@ -44,9 +44,21 @@ def test_ftp_path_validation
site = assert_valid_attribute(:ftp_path, "relative-path")
assert_equal("/relative-path", site.ftp_path)
- assert_not_valid_attribute(["FTPパス は不正な値です。"],
+ site = assert_valid_attribute(:ftp_path, " /surrounded-by-spaces ")
+ assert_equal("/surrounded-by-spaces", site.ftp_path)
+
+ site = assert_valid_attribute(:ftp_path, "/zenkaku")
+ assert_equal("/zenkaku", site.ftp_path)
+
+ assert_not_valid_attribute(["FTPパス にスペースが入っています。"],
:ftp_path,
"/have space")
+ assert_not_valid_attribute(["FTPパス に記号が入っています。"],
+ :ftp_path,
+ "/path?")
+ assert_not_valid_attribute(["FTPパス に日本語が入っています。"],
+ :ftp_path,
+ "/パス")
end
private

0 comments on commit 21956cb

Please sign in to comment.