Skip to content

Commit

Permalink
Merge pull request #90 from ifad/feature/rails6
Browse files Browse the repository at this point in the history
Add support for Rails 6.0
  • Loading branch information
vjt authored Oct 2, 2019
2 parents cb3d9ed + 954f540 commit 20c9e40
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 27 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@ rvm:
- 2.3
- 2.4
- 2.5
- 2.6

gemfile:
- gemfiles/rails_5.0.gemfile
- gemfiles/rails_5.1.gemfile
- gemfiles/rails_5.2.gemfile
- gemfiles/rails_6.0.gemfile

matrix:
exclude:
- rvm: 2.3
gemfile: gemfiles/rails_6.0.gemfile
- rvm: 2.4
gemfile: gemfiles/rails_6.0.gemfile
- rvm: 2.6
gemfile: gemfiles/rails_5.0.gemfile
- rvm: 2.6
gemfile: gemfiles/rails_5.1.gemfile

addons:
code_climate:
Expand Down
2 changes: 1 addition & 1 deletion chrono_model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
gem.version = ChronoModel::VERSION

gem.add_dependency 'activerecord', '>= 5.0.0'
gem.add_dependency "pg"
gem.add_dependency "pg", '> 1.1.0'
gem.add_dependency "multi_json"

gem.add_development_dependency 'rails'
Expand Down
6 changes: 6 additions & 0 deletions gemfiles/rails_6.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

gem "activerecord", "~> 6.0.0"
gem "rails", "~> 6.0.0"

gemspec :path => "../"
15 changes: 9 additions & 6 deletions lib/active_record/connection_adapters/chronomodel_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def chronomodel_connection(config) # :nodoc:
valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
conn_params.slice!(*valid_conn_param_keys)

# The postgres drivers don't allow the creation of an unconnected PG::Connection object,
# so just pass a nil connection object for the time being.
adapter = ChronoModel::Adapter.new(nil, logger, conn_params, config)
conn = PG.connect(conn_params) if ActiveRecord::VERSION::MAJOR >= 6

adapter = ChronoModel::Adapter.new(conn, logger, conn_params, config)

unless adapter.chrono_supported?
raise ChronoModel::Error, "Your database server is not supported by ChronoModel. "\
Expand All @@ -32,10 +32,13 @@ def chronomodel_connection(config) # :nodoc:
adapter.chrono_setup!

return adapter
end

module Connectionadapters
ChronoModelAdapter = ::ChronoModel::Adapter
rescue ::PG::Error => error
if error.message.include?(conn_params[:dbname])
raise ActiveRecord::NoDatabaseError
else
raise
end
end

end
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Conversions
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\.(\d+))?\z/

def string_to_utc_time(string)
return string if string.is_a?(Time)

if string =~ ISO_DATETIME
usec = $7.nil? ? '000000' : $7.ljust(6, '0') # .1 is .100000, not .000001
Time.utc $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, usec.to_i
Expand Down
3 changes: 3 additions & 0 deletions lib/chrono_model/patches/preloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def preload(records, associations, given_preload_scope = nil)
ChronoModel::Patches::AsOfTimeRelation.new(options[:model])

preload_scope.as_of_time!(options[:as_of_time])
elsif given_preload_scope.respond_to?(:as_of_time)
preload_scope = given_preload_scope

end

super records, associations, preload_scope
Expand Down
4 changes: 2 additions & 2 deletions spec/chrono_model/time_machine/default_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class ::Defoo < ActiveRecord::Base
end

active = ts_eval { Defoo.create! :name => 'active 1', :active => true }
ts_eval(active) { update_attributes! :name => 'active 2' }
ts_eval(active) { update! :name => 'active 2' }

hidden = ts_eval { Defoo.create! :name => 'hidden 1', :active => false }
ts_eval(hidden) { update_attributes! :name => 'hidden 2' }
ts_eval(hidden) { update! :name => 'hidden 2' }

describe 'it honors default_scopes' do
it { expect(Defoo.as_of(active.ts[0]).map(&:name)).to eq ['active 1'] }
Expand Down
2 changes: 1 addition & 1 deletion spec/chrono_model/time_machine/manipulations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
rec = nil
before(:all) do
rec = ts_eval { Foo.create!(:name => 'alive foo', :fooity => 42) }
ts_eval(rec) { update_attributes!(:name => 'dying foo') }
ts_eval(rec) { update!(:name => 'dying foo') }
end
after(:all) do
rec.history.delete_all
Expand Down
2 changes: 1 addition & 1 deletion spec/chrono_model/time_machine/sti_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ::Publication < Element
describe 'timeline' do
let(:publication) do
pub = ts_eval { Publication.create! :title => 'wrong title' }
ts_eval(pub) { update_attributes! :title => 'correct title' }
ts_eval(pub) { update! :title => 'correct title' }

pub
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/adapter/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.columns(&block)
end

def self.pk_type
@pk_type ||= if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1
@pk_type ||= if ActiveRecord::VERSION::STRING.to_f >= 5.1
'bigint'
else
'integer'
Expand Down
11 changes: 9 additions & 2 deletions spec/support/time_machine/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ def ts_eval(ctx = nil, &block)
define_method(:ts) { @_ts ||= [] }
end unless obj.methods.include?(:ts)

now = ChronoTest.connection.select_value('select now()::timestamp') + 'Z'
obj.ts.push(Time.parse(now))
now = ChronoTest.connection.select_value('select now()::timestamp')
case now
when Time # Already parsed, thanks AR
obj.ts.push(now)
when String # ISO8601 Timestamp
obj.ts.push(Time.parse(now+'Z'))
else
raise "Don't know how to deal with #{now.inspect}"
end
end
end
end
Expand Down
26 changes: 13 additions & 13 deletions spec/support/time_machine/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
# The models exercise different ActiveRecord features.
#
# They look candidate of unwinding by their respective specs, however this
# test suite aims also at testing within a "real" use case scenario, in which
# multiple models are defined and they interact - with their AR side effects,
# still ChronoModel should provide the expected results.
# test suite aims testing within a "real" use case scenario, with multiple
# interacting models are defined, with their AR side effects, testing that
# ChronoModel provides the expected results.
#
# The +$t+ global variable holds a timeline of events that have happened in
# the form of .create! and update_attributes, that aim to mimic the most of
# AR with the least of the effort. Full coverage exercises are most welcome.
# the form of .create! and update!, aiming to mimic the most of AR with the
# least of the effort. Full coverage exercises are most welcome.
#
module ChronoTest::TimeMachine
include ChronoTest::TimeMachine::Helpers
Expand Down Expand Up @@ -83,23 +83,23 @@ class ::Baz < ActiveRecord::Base
# Set up associated records, with intertwined updates
#
$t.foo = ts_eval { Foo.create! :name => 'foo', :fooity => 1 }
ts_eval($t.foo) { update_attributes! :name => 'foo bar' }
ts_eval($t.foo) { update! :name => 'foo bar' }

#
$t.bar = ts_eval { Bar.create! :name => 'bar', :foo => $t.foo }
ts_eval($t.bar) { update_attributes! :name => 'foo bar' }
ts_eval($t.bar) { update! :name => 'foo bar' }

#
$t.subbar = ts_eval { SubBar.create! :name => 'sub-bar', :bar => $t.bar }
ts_eval($t.subbar) { update_attributes! :name => 'bar sub-bar' }
ts_eval($t.subbar) { update! :name => 'bar sub-bar' }

ts_eval($t.foo) { update_attributes! :name => 'new foo' }
ts_eval($t.foo) { update! :name => 'new foo' }

ts_eval($t.bar) { update_attributes! :name => 'bar bar' }
ts_eval($t.bar) { update_attributes! :name => 'new bar' }
ts_eval($t.bar) { update! :name => 'bar bar' }
ts_eval($t.bar) { update! :name => 'new bar' }

ts_eval($t.subbar) { update_attributes! :name => 'sub-bar sub-bar' }
ts_eval($t.subbar) { update_attributes! :name => 'new sub-bar' }
ts_eval($t.subbar) { update! :name => 'sub-bar sub-bar' }
ts_eval($t.subbar) { update! :name => 'new sub-bar' }

#
$t.foos = Array.new(2) {|i| ts_eval { Foo.create! :name => "foo #{i}" } }
Expand Down

0 comments on commit 20c9e40

Please sign in to comment.