bridgetown-activerecord 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccfd8d0b6032758ade7b72ece8ff373552c3257d84a35cab4a34e98c88789fb3
4
- data.tar.gz: 244806f70399573021e39162d2aca78e634a8e41279c3e23c85cf42f8d1fa176
3
+ metadata.gz: e1ec1ff457bbf992255599d52b2c8efa8708f80c97dd7c4e9e5b4497adc7edb9
4
+ data.tar.gz: b8dbab9b63c86433665feda57f3f316c6c01ca4418a17c0bc3bcb2768431d9b5
5
5
  SHA512:
6
- metadata.gz: db2527fb6c3ca7da3f67678bbb75d845a01d310ab762ec05661b688650a19b203c400bbfd8de4c1c86f2fdbd143e8383cda37142a751904c27368ea04612a17b
7
- data.tar.gz: fea781ad77051b5018687ba9199a40839bbfc00926810170942309cdb0c5ec5e772589214d31b04731778d57aa25e8430e9518b51c2f3766b05de652c7b78af6
6
+ metadata.gz: f1ecfaec16724b1f609e6ea43b645afd1d8b08bc4d8a1332254d4f7ff32d599d52b7c3f8cfdcd0519cef550ddf61acc68e693290c78b3f2abf251aa7ca78cc30
7
+ data.tar.gz: c96065139c88134350b93e71109a9090355f2e6954b3be5ea437f106be18ac7f7473e23fe634798b8139227a5ace8a09ae4398166626614c95de6c784f1be77b
data/CHANGELOG.md CHANGED
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ## [2.2.0] - 2023-03-13
11
+
12
+ - Add support for Sequel with shared DB connection
13
+
14
+ ## [2.1.0] - 2022-10-10
15
+
16
+ - Allow customizing of the models dir
17
+
10
18
  ## [2.0.0] - 2022-10-08
11
19
 
12
20
  - Upgrade to initializers system in Bridgetown 1.2
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ gem "bridgetown", ENV["BRIDGETOWN_VERSION"] if ENV["BRIDGETOWN_VERSION"]
8
8
  group :test do
9
9
  gem "minitest"
10
10
  gem "minitest-reporters"
11
+ gem "pg", "~> 1.3"
12
+ gem "sequel-activerecord_connection", "~> 1.2"
11
13
  gem "shoulda"
12
14
  end
13
-
14
- gem "pg", "~> 1.3", group: :test
data/README.md CHANGED
@@ -1,10 +1,14 @@
1
- # Bridgetown ActiveRecord plugin
1
+ # Bridgetown Active Record plugin
2
2
 
3
- **NOTE:** currently waiting on a bugfix in Bridgetown itself before getting the green light…coming very soon!
3
+ 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!
4
+
5
+ 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)).
4
6
 
5
7
  ## Installation
6
8
 
7
- It's recommended you run our automation script to set up your project to support ActiveRecord and DB models:
9
+ > **IMPORTANT NOTE:** there's currently a compatibilty issue between Rails 7 gems and Rack version 3. For now, please add `gem "rack", "~> 2.2"` to your Gemfile _before_ you install this plugin.
10
+
11
+ It's recommended you run our automation script to set up your project to support Active Record and DB models:
8
12
 
9
13
  ```shell
10
14
  $ bin/bridgetown apply https://github.com/bridgetownrb/bridgetown-activerecord
@@ -13,7 +17,7 @@ $ bin/bridgetown apply https://github.com/bridgetownrb/bridgetown-activerecord
13
17
  Or for a fully manual setup:
14
18
 
15
19
  ```shell
16
- $ bundle add bridgetown-activerecord -g bridgetown_plugins
20
+ $ bundle add bridgetown-activerecord
17
21
  ```
18
22
 
19
23
  then replicate the individual steps outlined in the [automation script](https://github.com/bridgetownrb/bridgetown-activerecord/blob/main/bridgetown.automation.rb).
@@ -24,6 +28,8 @@ You will need to decide on your database adapter of choice. For a typical Postgr
24
28
  $ bundle add pg
25
29
  ```
26
30
 
31
+ When deploying to production, the `DATABASE_URL` ENV var will need to be set with the appropriate connection string…many hosting services will do this for you automatically.
32
+
27
33
  ## Usage
28
34
 
29
35
  Let's create a simple Movie model to load and save our favorite movies. Add the file `models/movie.rb`:
@@ -92,7 +98,40 @@ $ bin/bridgetown console
92
98
  }
93
99
  ```
94
100
 
95
- You're ready to roll to take full advantage of ActiveRecord database models in your Bridgetown site!
101
+ You're ready to roll to take full advantage of Active Record database models in your Bridgetown site!
102
+
103
+ ## Changing the Models Directory
104
+
105
+ If you'd prefer to set up your models folder elsewhere other than `./models`, you can move the files to another path and then update your Rakefile to pass in that path. For example, you could use the more familiar `app/models`:
106
+
107
+ ```ruby
108
+ require "bridgetown-activerecord"
109
+ BridgetownActiveRecord.load_tasks(models_dir: "app/models")
110
+ ```
111
+
112
+ (Don't forget to update your autoload path in `config/initializers.rb` accordingly.)
113
+
114
+ ## Using with Sequel
115
+
116
+ To set up [Sequel](https://github.com/jeremyevans/sequel) using the same database connection as Active Record, follow these steps:
117
+
118
+ First, install the sequel-activerecord_connection gem:
119
+
120
+ ```sh
121
+ bundle add sequel-activerecord_connection
122
+ ```
123
+
124
+ Next, update your configuration in `config/initializers.rb` as follows:
125
+
126
+ ```ruby
127
+ init :"bridgetown-activerecord", sequel_support: :postgres # or mysql2 or sqlite3
128
+ ```
129
+
130
+ Now you should be able to call `DB` to access the Sequel API anywhere in your Bridgetown or Roda code:
131
+
132
+ ```ruby
133
+ DB.tables # => [:ar_internal_metadata, :schema_migrations, etc.]
134
+ ```
96
135
 
97
136
  ## Testing
98
137
 
@@ -1,5 +1,5 @@
1
- add_bridgetown_plugin "bridgetown-activerecord"
2
- run "bundle add annotate -g development", abort_on_failure: false
1
+ add_gem "bridgetown-activerecord"
2
+ add_gem "annotate", group: :development
3
3
 
4
4
  # TODO:
5
5
  #dboptions = ["postgresql", "mysql", "vanilla"]
@@ -50,21 +50,20 @@ create_file ".standalone_migrations" do
50
50
  YML
51
51
  end
52
52
 
53
- append_to_file "bridgetown.config.yml" do
54
- <<~YML
53
+ insert_into_file "Rakefile", "require \"bridgetown-activerecord\"\nBridgetownActiveRecord.load_tasks\n", :after => "Bridgetown.load_tasks\n"
55
54
 
56
- autoload_paths:
57
- - path: models
58
- eager: true
55
+ ruby_configure "Support for autoloading models" do
56
+ <<~RUBY
57
+ config.autoload_paths << {
58
+ path: "models",
59
+ eager: true
60
+ }
59
61
 
60
- autoloader_collapsed_paths:
61
- - models/concerns
62
- YML
62
+ config.autoloader_collapsed_paths << "models/concerns"
63
+ RUBY
63
64
  end
64
65
 
65
- insert_into_file "Rakefile", "require \"bridgetown-activerecord\"\nBridgetownActiveRecord.load_tasks\n", :after => "Bridgetown.load_tasks\n"
66
-
67
- # TODO: add to initializer !!!
66
+ add_initializer :"bridgetown-activerecord"
68
67
 
69
68
  say_status :active_record, "The plugin has been configured. For usage help visit:"
70
69
  say_status :active_record, "https://github.com/bridgetownrb/bridgetown-activerecord/blob/main/README.md"
@@ -6,7 +6,8 @@ require "active_record"
6
6
  require "active_support/configuration_file"
7
7
 
8
8
  module BridgetownActiveRecord
9
- def self.load_tasks
9
+ def self.load_tasks(models_dir: "models")
10
+ ENV["BRIDGETOWN_ACTIVERECORD_MODELS_DIR"] ||= models_dir
10
11
  load File.expand_path("../tasks/database.rake", __dir__)
11
12
  end
12
13
 
@@ -19,9 +20,14 @@ module BridgetownActiveRecord
19
20
  end
20
21
  end
21
22
 
22
- Bridgetown.initializer :"bridgetown-activerecord" do |config|
23
+ Bridgetown.initializer :"bridgetown-activerecord" do |config, sequel_support: nil|
23
24
  ActiveRecord::Base.establish_connection(
24
25
  BridgetownActiveRecord.db_configuration(config)[Bridgetown.environment]
25
26
  )
26
27
  ActiveRecord::Base.logger = BridgetownActiveRecord.log_writer
28
+
29
+ next unless sequel_support
30
+
31
+ require "sequel"
32
+ DB = Sequel.send(sequel_support, extensions: :activerecord_connection) # rubocop:disable Lint/ConstantDefinitionInBlock
27
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgetownActiveRecord
4
- VERSION = "2.0.0"
4
+ VERSION = "2.2.0"
5
5
  end
@@ -5,12 +5,12 @@ ENV["SCHEMA"] = File.join(Dir.pwd, "db", "schema.rb")
5
5
  require "standalone_migrations"
6
6
  StandaloneMigrations::Tasks.load_tasks
7
7
 
8
- require "./models/application_record"
8
+ require "./#{ENV.fetch("BRIDGETOWN_ACTIVERECORD_MODELS_DIR", "models")}/application_record"
9
9
 
10
10
  # Setup Annotate so it runs on db:migrate, etc.
11
11
  if Bridgetown.environment.development?
12
12
  require "annotate"
13
- ENV["model_dir"] = "models"
13
+ ENV["model_dir"] = ENV.fetch("BRIDGETOWN_ACTIVERECORD_MODELS_DIR", "models")
14
14
  Annotate::Helpers.class_eval do
15
15
  # We need to redefine this because our custom model_dir shortcircuits the migration task enhancement
16
16
  def self.include_models?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridgetown-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-09 00:00:00.000000000 Z
11
+ date: 2023-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown