From 1b6f749e1d2fc298a06a1bae6166110b24339eb6 Mon Sep 17 00:00:00 2001 From: Jared White Date: Sun, 12 Feb 2023 15:44:25 -0800 Subject: [PATCH 1/2] Add support for Sequel with shared DB connection --- Gemfile | 4 ++-- lib/bridgetown-activerecord/initializer.rb | 7 ++++++- test/fixtures/config/initializers.rb | 2 +- test/test_bridgetown_activerecord.rb | 1 + 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 638ceac..29e116d 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,7 @@ gem "bridgetown", ENV["BRIDGETOWN_VERSION"] if ENV["BRIDGETOWN_VERSION"] group :test do gem "minitest" gem "minitest-reporters" + gem "pg", "~> 1.3" + gem "sequel-activerecord_connection", "~> 1.2" gem "shoulda" end - -gem "pg", "~> 1.3", group: :test diff --git a/lib/bridgetown-activerecord/initializer.rb b/lib/bridgetown-activerecord/initializer.rb index 16ec26d..e1bf42b 100644 --- a/lib/bridgetown-activerecord/initializer.rb +++ b/lib/bridgetown-activerecord/initializer.rb @@ -20,9 +20,14 @@ def self.log_writer end end -Bridgetown.initializer :"bridgetown-activerecord" do |config| +Bridgetown.initializer :"bridgetown-activerecord" do |config, sequel_support: nil| ActiveRecord::Base.establish_connection( BridgetownActiveRecord.db_configuration(config)[Bridgetown.environment] ) ActiveRecord::Base.logger = BridgetownActiveRecord.log_writer + + next unless sequel_support + + require "sequel" + DB = Sequel.send(sequel_support, extensions: :activerecord_connection) end diff --git a/test/fixtures/config/initializers.rb b/test/fixtures/config/initializers.rb index f6c79c5..99a0081 100644 --- a/test/fixtures/config/initializers.rb +++ b/test/fixtures/config/initializers.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true Bridgetown.configure do - init :"bridgetown-activerecord" + init :"bridgetown-activerecord", sequel_support: :postgres end diff --git a/test/test_bridgetown_activerecord.rb b/test/test_bridgetown_activerecord.rb index 69c6b87..05545b7 100644 --- a/test/test_bridgetown_activerecord.rb +++ b/test/test_bridgetown_activerecord.rb @@ -28,6 +28,7 @@ def setup should "connect to the database" do assert_includes @contents, "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter" + assert_equal "#:postgres, :extensions=>:activerecord_connection}>", DB.inspect end end end From d26271ef7eff177c8f699cb769be27cb15d68c5f Mon Sep 17 00:00:00 2001 From: Jared White Date: Sun, 12 Feb 2023 15:56:57 -0800 Subject: [PATCH 2/2] Add Readme documentation for Sequel --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f172706..84278d7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Bridgetown Active Record plugin -This plugin adds Active Record support to Bridgetown sites (v1.2 or higher). You can pull data from a database (currently PostgreSQL is officially supported) during a static build or during server requests (or both!) and use many of the features you know and love from Active Record in Rails—including migrations! +This plugin adds [Active Record](https://guides.rubyonrails.org/active_record_basics.html) support to Bridgetown sites (v1.2 or higher). You can pull data from a database (currently PostgreSQL is officially supported) during a static build or during server requests (or both!) and use many of the features you know and love from Active Record in Rails—including migrations! + +In addition, if you also like using the [Sequel gem](https://github.com/jeremyevans/sequel) for your database access, this plugin can support instantiating Sequel with a shared DB connection via the [sequel-activerecord_connection](https://github.com/janko/sequel-activerecord_connection) extension ([see below](#using-with-sequel)). ## Installation @@ -109,6 +111,28 @@ BridgetownActiveRecord.load_tasks(models_dir: "app/models") (Don't forget to update your autoload path in `config/initializers.rb` accordingly.) +## Using with Sequel + +To set up [Sequel](https://github.com/jeremyevans/sequel) using the same database connection as Active Record, follow these steps: + +First, install the sequel-activerecord_connection gem: + +```sh +bundle add sequel-activerecord_connection +``` + +Next, update your configuration in `config/initializers.rb` as follows: + +```ruby +init :"bridgetown-activerecord", sequel_support: :postgres # or mysql2 or sqlite3 +``` + +Now you should be able to call `DB` to access the Sequel API anywhere in your Bridgetown or Roda code: + +```ruby +DB.tables # => [:ar_internal_metadata, :schema_migrations, etc.] +``` + ## Testing * Run `bundle exec rake test` to run the test suite