Skip to content

Commit

Permalink
Merge pull request #2 from bridgetownrb/support-sequel
Browse files Browse the repository at this point in the history
Support sharing DB connection with Sequel
  • Loading branch information
jaredcwhite authored Mar 13, 2023
2 parents d812a51 + d26271e commit 7f8992b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/bridgetown-activerecord/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/fixtures/config/initializers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

Bridgetown.configure do
init :"bridgetown-activerecord"
init :"bridgetown-activerecord", sequel_support: :postgres
end
1 change: 1 addition & 0 deletions test/test_bridgetown_activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setup

should "connect to the database" do
assert_includes @contents, "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
assert_equal "#<Sequel::Postgres::Database: {:adapter=>:postgres, :extensions=>:activerecord_connection}>", DB.inspect
end
end
end

0 comments on commit 7f8992b

Please sign in to comment.