Skip to content

Commit

Permalink
Merge pull request #24542 from kamipo/add_quoted_time
Browse files Browse the repository at this point in the history
Add `quoted_time` for truncating the date part of a time column value
  • Loading branch information
jeremy committed Apr 14, 2016
2 parents 128923c + 28ec8c4 commit 53ab1ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Expand Up @@ -146,6 +146,10 @@ def quoted_date(value)
end
end

def quoted_time(value) # :nodoc:
quoted_date(value).sub(/\A2000-01-01 /, '')
end

def prepare_binds_for_database(binds) # :nodoc:
binds.map(&:value_for_database)
end
Expand All @@ -166,6 +170,7 @@ def _quote(value)
# BigDecimals need to be put in a non-normalized form and quoted.
when BigDecimal then value.to_s('F')
when Numeric, ActiveSupport::Duration then value.to_s
when Type::Time::Value then "'#{quoted_time(value)}'"
when Date, Time then "'#{quoted_date(value)}'"
when Symbol then "'#{quote_string(value.to_s)}'"
when Class then "'#{value}'"
Expand All @@ -181,6 +186,7 @@ def _type_cast(value)
when false then unquoted_false
# BigDecimals need to be put in a non-normalized form and quoted.
when BigDecimal then value.to_s('F')
when Type::Time::Value then quoted_time(value)
when Date, Time then quoted_date(value)
when *types_which_need_no_typecasting
value
Expand Down
12 changes: 12 additions & 0 deletions activerecord/lib/active_record/type/time.rb
Expand Up @@ -2,6 +2,18 @@ module ActiveRecord
module Type
class Time < ActiveModel::Type::Time
include Internal::Timezone

class Value < DelegateClass(::Time) # :nodoc:
end

def serialize(value)
case value = super
when ::Time
Value.new(value)
else
value
end
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion activerecord/test/cases/time_precision_test.rb
Expand Up @@ -44,7 +44,6 @@ def test_invalid_time_precision_raises_error
end

def test_formatting_time_according_to_precision
skip("TIME column on MariaDB doesn't ignore the date part of the string when it coerces to time") if current_adapter?(:Mysql2Adapter) && ActiveRecord::Base.connection.mariadb?
@connection.create_table(:foos, force: true) do |t|
t.time :start, precision: 0
t.time :finish, precision: 4
Expand Down

0 comments on commit 53ab1ee

Please sign in to comment.