Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on remove_week if days is nil #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/torque/postgresql/adapter/oid/interval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ module PostgreSQL
module Adapter
module OID
class Interval < ActiveModel::Type::Value

CAST_PARTS = [:years, :months, :days, :hours, :minutes, :seconds]
CAST_PARTS = %i[years months days hours minutes seconds]

def type
:interval
Expand All @@ -26,6 +25,7 @@ def type
# produces: 12 minutes, and 0 seconds
def cast(value)
return if value.blank?

case value
when ::String then deserialize(value)
when ::ActiveSupport::Duration then value
Expand All @@ -51,13 +51,15 @@ def cast(value)
# The value must be Integer when no precision is given
def deserialize(value)
return if value.blank?

ActiveSupport::Duration.parse(value)
end

# Uses the ActiveSupport::Duration::ISO8601Serializer
# See ActiveSupport::Duration#iso8601
def serialize(value)
return if value.blank?

value = cast(value) unless value.is_a?(ActiveSupport::Duration)
value = remove_weeks(value) if value.parts.to_h.key?(:weeks)
value.iso8601(precision: @scale)
Expand Down Expand Up @@ -96,7 +98,8 @@ def parts_to_duration(parts)
# https://github.com/crashtech/torque-postgresql/issues/26
# https://github.com/rails/rails/issues/34655
def remove_weeks(value)
parts = value.parts.dup
parts = value.parts.to_h.dup
parts[:days] ||= 0
parts[:days] += parts.delete(:weeks) * 7
ActiveSupport::Duration.new(value.seconds.to_i, parts)
end
Expand Down