Skip to content

Commit

Permalink
* repair FTP host and path if available.
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Mar 15, 2009
1 parent fd096b5 commit 971f1ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
14 changes: 14 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ def default
validates_format_of :ftp_host, :with => URI::HOST, :allow_blank => true
validates_format_of :ftp_path, :with => URI::ABS_PATH, :allow_blank => true

before_validation :repair_ftp_host, :repair_ftp_path

def blank_configuration?
title.blank? and !have_ftp_configuration?
end

def have_ftp_configuration?
!ftp_host.blank? and !ftp_path.blank?
end

private
def repair_ftp_host
return if ftp_host.blank?
self.ftp_host = ftp_host.strip
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
end
9 changes: 6 additions & 3 deletions test/unit/site_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def test_ftp_host_validation
assert_valid_attribute(:ftp_host, "ftp.example.com")
assert_valid_attribute(:ftp_host, "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")
Expand All @@ -28,12 +31,12 @@ def test_ftp_path_validation
assert_valid_attribute(:ftp_path, "/")
assert_valid_attribute(:ftp_path, "/directory/")

site = assert_valid_attribute(:ftp_path, "relative-path")
assert_equal("/relative-path", site.ftp_path)

assert_not_valid_attribute(["FTPパス は不正な値です。"],
:ftp_path,
"/have space")
assert_not_valid_attribute(["FTPパス は不正な値です。"],
:ftp_path,
"relative-path")
end

private
Expand Down

0 comments on commit 971f1ab

Please sign in to comment.