diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d472aec..0feb302 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,8 +30,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - gemfile: ['6.1_stable', '7.0_stable'] - ruby-version: ['3.0', '3.1'] + gemfile: ['6.1_stable', '7.0_stable', '7.1_stable'] + ruby-version: ['3.0', '3.1', '3.2'] services: mysql: image: mysql:5.7 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b7cb72b..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -sudo: false -language: ruby -services: - - mysql -rvm: - - 3.0.6 - - 3.1.4 - - 3.2.2 -before_install: gem install bundler -gemfile: - - gemfiles/6.1_stable.gemfile - - gemfiles/7.0_stable.gemfile -script: - - bundle exec rspec - - bundle exec rubocop -script: bundle exec rspec diff --git a/Appraisals b/Appraisals index cfb9567..bd9efd6 100644 --- a/Appraisals +++ b/Appraisals @@ -7,3 +7,7 @@ end appraise '7.0-stable' do gem 'activerecord', '~> 7.0.0' end + +appraise '7.1-stable' do + gem 'activerecord', '~> 7.1.0' +end diff --git a/gemfiles/7.1_stable.gemfile b/gemfiles/7.1_stable.gemfile new file mode 100644 index 0000000..69bc38a --- /dev/null +++ b/gemfiles/7.1_stable.gemfile @@ -0,0 +1,7 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activerecord", "~> 7.1.0" + +gemspec path: "../" diff --git a/lib/active_record_encryption/quoter.rb b/lib/active_record_encryption/quoter.rb index 365b15e..a62486f 100644 --- a/lib/active_record_encryption/quoter.rb +++ b/lib/active_record_encryption/quoter.rb @@ -26,6 +26,11 @@ def type_cast(value) end end + # @return [Symbol] + def default_timezone + ActiveRecord::Base.connection.default_timezone + end + private if ActiveRecord::VERSION::MAJOR < 7 diff --git a/lib/active_record_encryption/type/binary.rb b/lib/active_record_encryption/type/binary.rb index 98794d4..ac6ff5c 100644 --- a/lib/active_record_encryption/type/binary.rb +++ b/lib/active_record_encryption/type/binary.rb @@ -1,6 +1,19 @@ module ActiveRecordEncryption class Type class Binary < ActiveModel::Type::Binary + def cast(value) + return super if ActiveRecord::VERSION::STRING < '7.0.0' + + if value.is_a?(Data) + value.to_s + else + value = cast_value(value) unless value.nil? + # NOTE: Don't convert string to binary for backward compatibility. + # value = value.b if ::String === value && value.encoding != Encoding::BINARY + value + end + end + def serialize(value) return if value.nil? Data.new(value) diff --git a/spec/active_record_encryption/quoter_spec.rb b/spec/active_record_encryption/quoter_spec.rb index da67789..7ad1c0e 100644 --- a/spec/active_record_encryption/quoter_spec.rb +++ b/spec/active_record_encryption/quoter_spec.rb @@ -4,6 +4,14 @@ describe '#type_cast' do subject { described_class.instance.type_cast(value) } + def to_s(value, *args) + if ActiveRecord::VERSION::MAJOR >= 7 + value.to_fs(*args) + else + value.to_s(*args) + end + end + context 'given string' do let(:value) { 'hello world' } it { is_expected.to eq(value) } @@ -31,17 +39,17 @@ context 'given ActiveRecord::Type::Time::Value' do let(:value) { ActiveRecord::Type.lookup(:time).serialize(Time.now).change(usec: 0) } - it { is_expected.to eq(value.to_s(:db)) } + it { is_expected.to eq(to_s(value, :db)) } end context 'given Date' do let(:value) { Date.today } - it { is_expected.to eq value.to_s(:db) } + it { is_expected.to eq to_s(value, :db) } end context 'given Time' do let(:value) { Time.now.change(usec: 0) } - it { is_expected.to eq value.utc.to_s(:db) } + it { is_expected.to eq to_s(value.utc, :db) } end context 'given Integer' do