Skip to content

Commit 53ab1ee

Browse files
committed
Merge pull request rails#24542 from kamipo/add_quoted_time
Add `quoted_time` for truncating the date part of a time column value
2 parents 128923c + 28ec8c4 commit 53ab1ee

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

activerecord/lib/active_record/connection_adapters/abstract/quoting.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ def quoted_date(value)
146146
end
147147
end
148148

149+
def quoted_time(value) # :nodoc:
150+
quoted_date(value).sub(/\A2000-01-01 /, '')
151+
end
152+
149153
def prepare_binds_for_database(binds) # :nodoc:
150154
binds.map(&:value_for_database)
151155
end
@@ -166,6 +170,7 @@ def _quote(value)
166170
# BigDecimals need to be put in a non-normalized form and quoted.
167171
when BigDecimal then value.to_s('F')
168172
when Numeric, ActiveSupport::Duration then value.to_s
173+
when Type::Time::Value then "'#{quoted_time(value)}'"
169174
when Date, Time then "'#{quoted_date(value)}'"
170175
when Symbol then "'#{quote_string(value.to_s)}'"
171176
when Class then "'#{value}'"
@@ -181,6 +186,7 @@ def _type_cast(value)
181186
when false then unquoted_false
182187
# BigDecimals need to be put in a non-normalized form and quoted.
183188
when BigDecimal then value.to_s('F')
189+
when Type::Time::Value then quoted_time(value)
184190
when Date, Time then quoted_date(value)
185191
when *types_which_need_no_typecasting
186192
value

activerecord/lib/active_record/type/time.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ module ActiveRecord
22
module Type
33
class Time < ActiveModel::Type::Time
44
include Internal::Timezone
5+
6+
class Value < DelegateClass(::Time) # :nodoc:
7+
end
8+
9+
def serialize(value)
10+
case value = super
11+
when ::Time
12+
Value.new(value)
13+
else
14+
value
15+
end
16+
end
517
end
618
end
719
end

activerecord/test/cases/time_precision_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def test_invalid_time_precision_raises_error
4444
end
4545

4646
def test_formatting_time_according_to_precision
47-
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?
4847
@connection.create_table(:foos, force: true) do |t|
4948
t.time :start, precision: 0
5049
t.time :finish, precision: 4

0 commit comments

Comments
 (0)