From ad52258f63cfa28f3b262be8095729bb44517795 Mon Sep 17 00:00:00 2001 From: Luu Nguyen Date: Sun, 3 Nov 2019 14:50:56 +0700 Subject: [PATCH] Support rails 5.x to 6.x --- lib/cursor_pagination/active_record_extension.rb | 3 ++- spec/fake_app/active_record/config.rb | 3 --- spec/fake_app/active_record/database.yml | 10 ++++++++++ spec/fake_app/active_record/models.rb | 4 +++- spec/fake_app/rails.rb | 10 +++++++++- spec/helpers/cursor_pagination_helper_spec.rb | 2 +- spec/models/entity_spec.rb | 4 ++-- spec/spec_helper.rb | 4 ++++ 8 files changed, 31 insertions(+), 9 deletions(-) delete mode 100644 spec/fake_app/active_record/config.rb create mode 100644 spec/fake_app/active_record/database.yml diff --git a/lib/cursor_pagination/active_record_extension.rb b/lib/cursor_pagination/active_record_extension.rb index 1e0f2fd..3b85d18 100644 --- a/lib/cursor_pagination/active_record_extension.rb +++ b/lib/cursor_pagination/active_record_extension.rb @@ -12,7 +12,8 @@ def inherited_with_cursor_pagination(kls) #:nodoc: inherited_without_cursor_pagination kls kls.send(:include, CursorPagination::ActiveRecordModelExtension) if kls.superclass == ActiveRecord::Base end - alias_method_chain :inherited, :cursor_pagination + alias_method :inherited_without_cursor_pagination, :inherited + alias_method :inherited, :inherited_with_cursor_pagination end # Existing subclasses pick up the model extension as well diff --git a/spec/fake_app/active_record/config.rb b/spec/fake_app/active_record/config.rb deleted file mode 100644 index cf0ec88..0000000 --- a/spec/fake_app/active_record/config.rb +++ /dev/null @@ -1,3 +0,0 @@ -ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}} -ActiveRecord::Base.default_timezone = :utc -ActiveRecord::Base.establish_connection('test') diff --git a/spec/fake_app/active_record/database.yml b/spec/fake_app/active_record/database.yml new file mode 100644 index 0000000..1bc1088 --- /dev/null +++ b/spec/fake_app/active_record/database.yml @@ -0,0 +1,10 @@ +default: &default + adapter: sqlite3 + +development: + <<: *default + database: memory_development + +test: + <<: *default + database: memory_test diff --git a/spec/fake_app/active_record/models.rb b/spec/fake_app/active_record/models.rb index 77fefe7..9ef947b 100644 --- a/spec/fake_app/active_record/models.rb +++ b/spec/fake_app/active_record/models.rb @@ -4,8 +4,10 @@ class Entity < ActiveRecord::Base end #migrations -class CreateAllTables < ActiveRecord::Migration +class CreateAllTables < ActiveRecord::Migration[6.0] def self.up + return if ActiveRecord::Base.connection.table_exists? 'entities' + create_table :entities do |t| t.integer :custom t.datetime :custom_time diff --git a/spec/fake_app/rails.rb b/spec/fake_app/rails.rb index 5e1ef3d..3b18537 100644 --- a/spec/fake_app/rails.rb +++ b/spec/fake_app/rails.rb @@ -2,7 +2,15 @@ require 'action_controller/railtie' require 'action_view/railtie' -require 'fake_app/active_record/config' if defined? ActiveRecord +if defined? ActiveRecord + ActiveRecord::Base.establish_connection( + YAML.safe_load( + ERB.new( + File.read('spec/fake_app/active_record/database.yml') + ).result, [], [], true + )['test'] + ) +end # config app = Class.new(Rails::Application) diff --git a/spec/helpers/cursor_pagination_helper_spec.rb b/spec/helpers/cursor_pagination_helper_spec.rb index 2fab927..bbdfa86 100644 --- a/spec/helpers/cursor_pagination_helper_spec.rb +++ b/spec/helpers/cursor_pagination_helper_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe CursorPagination::ActionViewHelper do +describe CursorPagination::ActionViewHelper, type: :helper do include_context "entities" diff --git a/spec/models/entity_spec.rb b/spec/models/entity_spec.rb index a1ecb84..c2cf207 100644 --- a/spec/models/entity_spec.rb +++ b/spec/models/entity_spec.rb @@ -159,7 +159,7 @@ context "with direct sql" do specify do - target_sql = "(\"entities\".\"id\" < 1 OR \"entities\".\"id\" = 1 AND (\"entities\".\"custom\" > 2 OR \"entities\".\"custom\" = 2 AND \"entities\".\"custom_time\" > '2013-09-02 14:32:42.000000'))" + target_sql = "(\"entities\".\"id\" < 1 OR \"entities\".\"id\" = 1 AND (\"entities\".\"custom\" > 2 OR \"entities\".\"custom\" = 2 AND \"entities\".\"custom_time\" > '2013-09-02 14:32:42'))" where = Entity.send(:_cursor_to_where, columns, cursor_value) where.to_sql.should eq target_sql end @@ -167,7 +167,7 @@ context "with reversed sql" do specify do - target_sql = "(\"entities\".\"id\" > 1 OR \"entities\".\"id\" = 1 AND (\"entities\".\"custom\" < 2 OR \"entities\".\"custom\" = 2 AND \"entities\".\"custom_time\" <= '2013-09-02 14:32:42.000000'))" + target_sql = "(\"entities\".\"id\" > 1 OR \"entities\".\"id\" = 1 AND (\"entities\".\"custom\" < 2 OR \"entities\".\"custom\" = 2 AND \"entities\".\"custom_time\" <= '2013-09-02 14:32:42'))" where = Entity.send(:_cursor_to_where, columns, cursor_value, true) where.to_sql.should eq target_sql end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0f52a11..5eda167 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,6 +15,10 @@ # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| + config.include Rails.application.routes.url_helpers + + config.include Capybara::DSL + config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus