From fe61994631391b07836dbb823bf843dee85d05d5 Mon Sep 17 00:00:00 2001 From: Uwe Kubosch Date: Sun, 9 Jun 2024 18:05:13 +0200 Subject: [PATCH] Handle ActiveRecord 7.1 --- .github/workflows/test.yml | 9 ++++++-- lib/active_record/type/time.rb | 37 ++++++++++++++++++++++++++++++ lib/activerecord-time/extension.rb | 21 ++--------------- lib/activerecord-time/version.rb | 2 +- lib/time_of_day/core_ext.rb | 2 ++ matrix_test.rb | 3 ++- 6 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 lib/active_record/type/time.rb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index acbec40..b7c46a3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 }} diff --git a/lib/active_record/type/time.rb b/lib/active_record/type/time.rb new file mode 100644 index 0000000..924abf8 --- /dev/null +++ b/lib/active_record/type/time.rb @@ -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 diff --git a/lib/activerecord-time/extension.rb b/lib/activerecord-time/extension.rb index 02eda70..4668daf 100644 --- a/lib/activerecord-time/extension.rb +++ b/lib/activerecord-time/extension.rb @@ -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 diff --git a/lib/activerecord-time/version.rb b/lib/activerecord-time/version.rb index fff9394..562f870 100644 --- a/lib/activerecord-time/version.rb +++ b/lib/activerecord-time/version.rb @@ -2,6 +2,6 @@ module Activerecord module Time - VERSION = '0.22.0' + VERSION = '1.0.0' end end diff --git a/lib/time_of_day/core_ext.rb b/lib/time_of_day/core_ext.rb index d1f28f1..3b20468 100644 --- a/lib/time_of_day/core_ext.rb +++ b/lib/time_of_day/core_ext.rb @@ -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) diff --git a/matrix_test.rb b/matrix_test.rb index af0492e..a4f041d 100755 --- a/matrix_test.rb +++ b/matrix_test.rb @@ -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) @@ -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