Skip to content

Commit

Permalink
be like a duck. Let's not rely on explicit classes, so we can pass pr…
Browse files Browse the repository at this point in the history
…oxy objects around and have them interpreted correctly by ActiveRecord's serialization routines

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5953 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jamis committed Jan 15, 2007
1 parent 5314960 commit 521ca37
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
Expand Up @@ -24,9 +24,14 @@ def quote(value, column = nil)
when Float, Fixnum, Bignum then value.to_s
# BigDecimals need to be output in a non-normalized form and quoted.
when BigDecimal then value.to_s('F')
when Date then "'#{value.to_s}'"
when Time, DateTime then "'#{quoted_date(value)}'"
else "'#{quote_string(value.to_yaml)}'"
else
if value.acts_like?(:date)
"'#{value.to_s}'"
elsif value.acts_like?(:time)
"'#{quoted_date(value)}'"
else
"'#{quote_string(value.to_yaml)}'"
end
end
end

Expand Down
Expand Up @@ -375,31 +375,33 @@ def quote(value, column = nil)
(column && column.type == :integer ? '0' : quoted_false)
when Float, Fixnum, Bignum, BigDecimal
value.to_s
when Time, Date, DateTime
if column
case column.type
when :date
else
if value.acts_like?(:time) || value.acts_like?(:date)
if column
case column.type
when :date
"DATE '#{value.strftime("%Y-%m-%d")}'"
when :time
"TIME '#{value.strftime("%H:%M:%S")}'"
when :timestamp
"TIMESTAMP '#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
else
raise NotImplementedError, "Unknown column type!"
end # case
else # Column wasn't passed in, so try to guess the right type
if value.acts_like?(:date)
"DATE '#{value.strftime("%Y-%m-%d")}'"
when :time
"TIME '#{value.strftime("%H:%M:%S")}'"
when :timestamp
"TIMESTAMP '#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
else
raise NotImplementedError, "Unknown column type!"
end # case
else # Column wasn't passed in, so try to guess the right type
if value.kind_of? Date
"DATE '#{value.strftime("%Y-%m-%d")}'"
else
if [:hour, :min, :sec].all? {|part| value.send(:part).zero? }
"TIME '#{value.strftime("%H:%M:%S")}'"
else
"TIMESTAMP '#{quoted_date(value)}'"
end
end
end #if column
else
"'#{quote_string(value.to_yaml)}'"
if [:hour, :min, :sec].all? {|part| value.send(:part).zero? }
"TIME '#{value.strftime("%H:%M:%S")}'"
else
"TIMESTAMP '#{quoted_date(value)}'"
end
end
end #if column
else
"'#{quote_string(value.to_yaml)}'"
end
end #case
end
else
Expand Down
Expand Up @@ -347,9 +347,14 @@ def quote(value, column = nil)
case value
when TrueClass then '1'
when FalseClass then '0'
when Time, DateTime then "'#{value.strftime("%Y%m%d %H:%M:%S")}'"
when Date then "'#{value.strftime("%Y%m%d")}'"
else super
else
if value.acts_like?(:time)
"'#{value.strftime("%Y%m%d %H:%M:%S")}'"
elsif value.acts_like?(:date)
"'#{value.strftime("%Y%m%d")}'"
else
super
end
end
end

Expand Down
Expand Up @@ -292,8 +292,12 @@ def quote(value, column = nil)
when TrueClass then '1'
when FalseClass then '0'
when Float, Fixnum, Bignum then force_numeric?(column) ? value.to_s : "'#{value.to_s}'"
when Time, DateTime then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
else super
else
if value.acts_like?(:time)
"'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
else
super
end
end
end

Expand Down

0 comments on commit 521ca37

Please sign in to comment.