Skip to content

Commit

Permalink
Handle ActiveRecord 7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
donv committed Jun 9, 2024
1 parent 315fe22 commit fe61994
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ jobs:
strategy:
fail-fast: false
matrix:
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby: [ 3.3, 3.2, 3.1, jruby, truffleruby ]
gemfile: [ ar71, ar70 ]
adapter: [ postgresql, sqlite3 ]
exclude:
- ruby: jruby
gemfile: ar71 # Not released, yet.
- ruby: jruby
gemfile: ar70 # Fails, please investigate.
- ruby: truffleruby
gemfile: ar71 # Fails, please investigate.
- ruby: truffleruby
gemfile: ar70 # Fails, please investigate.
env:
ADAPTER: ${{ matrix.adapter }}
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/gems_${{ matrix.gemfile }}.rb
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
Expand Down
37 changes: 37 additions & 0 deletions lib/active_record/type/time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module ActiveRecord
module Type
class Time < ActiveModel::Type::Time
include Internal::Timezone

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

def map(value)
value
end

def serialize(value)
case value = super
when ::TimeOfDay
Value.new(value)
else
value
end
end

def serialize_cast_value(value)
Value.new(value) if value
end

private def cast_value(value)
return value.time_of_day if value.is_a?(::DateTime) || value.is_a?(::Time)
return value unless value.is_a?(::String)
return if value.empty?

TimeOfDay._parse(value)
end
end
end
end
21 changes: 2 additions & 19 deletions lib/activerecord-time/extension.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
# frozen_string_literal: true

require 'active_record/connection_adapters/abstract/quoting'

module Activerecord
module Time
module Quoting
def quote(value)
return "'#{value}'" if value.is_a?(TimeOfDay)

super(value)
super
end

def type_cast(value)
return value.to_s if value.is_a?(TimeOfDay)

super(value)
super
end
end
end
end

ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend Activerecord::Time::Quoting

module Activerecord
module Time
module Casting
private def cast_value(value)
return value.time_of_day if value.is_a?(::DateTime) || value.is_a?(::Time)
return value unless value.is_a?(::String)
return if value.empty?

TimeOfDay._parse(value)
end
end
end
end
ActiveRecord::Type::Time.prepend Activerecord::Time::Casting
2 changes: 1 addition & 1 deletion lib/activerecord-time/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Activerecord
module Time
VERSION = '0.22.0'
VERSION = '1.0.0'
end
end
2 changes: 2 additions & 0 deletions lib/time_of_day/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def time_of_day

class Date
def at(time_of_day)
return if time_of_day.nil?

time_of_day = TimeOfDay.parse(time_of_day) if time_of_day.is_a?(String)
zone = Time.zone || Time
zone.local(year, month, day, time_of_day.hour, time_of_day.minute, time_of_day.second)
Expand Down
3 changes: 2 additions & 1 deletion matrix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
update_gemfiles = ARGV.delete('--update')

require 'yaml'
require 'bundler'
actions = YAML.safe_load_file('.github/workflows/test.yml')

def run_script(ruby, env, gemfile)
Expand Down Expand Up @@ -36,7 +37,7 @@ def use_gemfile(ruby, gemfile, update_gemfiles)
end

def bad_variant?(bad_variants, ruby, gemfile = nil, adapter = nil)
bad_variants.find do |f|
bad_variants&.find do |f|
!(ruby.nil? ^ f['ruby'].nil?) && f['ruby'] == ruby &&
!(gemfile.nil? ^ f['gemfile'].nil?) && f['gemfile'] == gemfile &&
!(adapter.nil? ^ f['adapter'].nil?) && f['adapter'] == adapter
Expand Down

0 comments on commit fe61994

Please sign in to comment.