bridgetown-activerecord 1.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86121e3d1de53dc4a6cb5af343f14532e57052ccd3fc2e86c8e87996d6325a72
4
- data.tar.gz: a95b95bd75f2832c103f1595b4f4faaa54fd4b0dbad3a751c87026dbb0091027
3
+ metadata.gz: ef8ced2ced4ceb5c4945477bb4ff888effd806d532b923f08524963210f211dd
4
+ data.tar.gz: 9945be6d6dc6c7d11089ae5d227102d23f5f61c92d10d2891fbb45da12a6b4ed
5
5
  SHA512:
6
- metadata.gz: 6c60311f9a4b459010156dd772f4b7515ddb9ce91aa10168faddd31cb349950918c86df428e1db694531dba263b6263ec1e69bc12aa7aae14fe450268f5ca97e
7
- data.tar.gz: 95f8564b8a9f4fc36a0a85bd75d7f57d59e66de6a951a1c9f0bb3c5f5f549e186e66397b5c0aa50f00753b706ab195973268de0bc459e230e26f9e4bd818c816
6
+ metadata.gz: 55d827960c968db59a1477dc39945c54c6f6f55a9d040da866f30295e9902563b64325fb29eae2a3e69a656fa5913534a75daaebff11daf69f91445b2a07afd0
7
+ data.tar.gz: 072241e0d27f71b085c5a9f54cfeae05c7a0ef34f4ad4e0bc74207579dbd6deb343cbded7edea844eeedc8901bd57032b60dae11dd7eaff1364edf2435ae48f1
data/CHANGELOG.md CHANGED
@@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## Unreleased
9
9
 
10
- - ...
10
+ ## [2.0.0] - 2022-10-08
11
11
 
12
- ## 0.1.0 - YYYY-MM-DD
12
+ - Upgrade to initializers system in Bridgetown 1.2
13
+
14
+ ## 1.0.0 - 2022-04-11
13
15
 
14
16
  - First version
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
- # Bridgetown ActiveRecord plugin
1
+ # Bridgetown Active Record plugin
2
+
3
+ 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!
2
4
 
3
5
  ## Installation
4
6
 
5
- It's recommended you run our automation script to set up your project to support ActiveRecord and DB models:
7
+ > **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.
8
+
9
+ It's recommended you run our automation script to set up your project to support Active Record and DB models:
6
10
 
7
11
  ```shell
8
12
  $ bin/bridgetown apply https://github.com/bridgetownrb/bridgetown-activerecord
@@ -11,7 +15,7 @@ $ bin/bridgetown apply https://github.com/bridgetownrb/bridgetown-activerecord
11
15
  Or for a fully manual setup:
12
16
 
13
17
  ```shell
14
- $ bundle add bridgetown-activerecord -g bridgetown_plugins
18
+ $ bundle add bridgetown-activerecord
15
19
  ```
16
20
 
17
21
  then replicate the individual steps outlined in the [automation script](https://github.com/bridgetownrb/bridgetown-activerecord/blob/main/bridgetown.automation.rb).
@@ -22,9 +26,88 @@ You will need to decide on your database adapter of choice. For a typical Postgr
22
26
  $ bundle add pg
23
27
  ```
24
28
 
29
+ 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.
30
+
25
31
  ## Usage
26
32
 
27
- TBC
33
+ Let's create a simple Movie model to load and save our favorite movies. Add the file `models/movie.rb`:
34
+
35
+ ```rb
36
+ class Movie < ApplicationRecord
37
+ validates :title, presence: true, uniqueness: { case_insensitive: true }
38
+
39
+ def uppercase_title
40
+ title.upcase
41
+ end
42
+ end
43
+ ```
44
+
45
+ Then add a new migration for the Movie model.
46
+
47
+ ```sh
48
+ $ bin/bridgetown db:new_migration name=create_movies
49
+ ```
50
+
51
+ That will create a new migration file in `db/migrate`. Edit the file so it looks like this:
52
+
53
+ ```ruby
54
+ class CreateMovies < ActiveRecord::Migration[7.0]
55
+ def change
56
+ create_table :movies do |t|
57
+ t.string :title, null: false
58
+ t.string :director
59
+ t.integer :year
60
+
61
+ t.timestamps
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ Awesome, now let's create the database and run the migration!
68
+
69
+ ```sh
70
+ $ bin/bridgetown db:create
71
+ $ bin/bridgetown db:migrate
72
+ ```
73
+
74
+ If all goes well, the database should be created, the migration run, a new `db/schema.rb` saved, and schema annotations added to the top of `models/movie.rb`!
75
+
76
+ Let's try it out in the Bridgetown console.
77
+
78
+ ```sh
79
+ $ bin/bridgetown console
80
+ ```
81
+
82
+ ```ruby
83
+ > Movie.count
84
+ 0
85
+ > Movie.create(title: "Free Guy", director: "Shawn Levy", year: "2021")
86
+ > Movie.count
87
+ 1
88
+ > Movie.last
89
+ #<Movie:0x0000000109e26378> {
90
+ :id => 1,
91
+ :title => "Free Guy",
92
+ :director => "Shawn Levy",
93
+ :year => 2021,
94
+ :created_at => 2022-03-12 23:24:28.98137 UTC,
95
+ :updated_at => 2022-03-12 23:24:28.98137 UTC
96
+ }
97
+ ```
98
+
99
+ You're ready to roll to take full advantage of Active Record database models in your Bridgetown site!
100
+
101
+ ## Changing the Models Directory
102
+
103
+ 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`:
104
+
105
+ ```ruby
106
+ require "bridgetown-activerecord"
107
+ BridgetownActiveRecord.load_tasks(models_dir: "app/models")
108
+ ```
109
+
110
+ (Don't forget to update your autoload path in `config/initializers.rb` accordingly.)
28
111
 
29
112
  ## Testing
30
113
 
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.required_ruby_version = ">= 2.7.0"
19
19
 
20
- spec.add_dependency "bridgetown", ">= 1.0", "< 2.0"
20
+ spec.add_dependency "bridgetown", ">= 1.2.0.beta2", "< 2.0"
21
21
  spec.add_dependency "activerecord", "~> 7.0"
22
22
  spec.add_dependency "standalone_migrations", "~> 7.0"
23
23
 
@@ -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,19 +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", "BridgetownActiveRecord.load_tasks\n", :after => "Bridgetown.load_tasks\n"
66
+ add_initializer :"bridgetown-activerecord"
66
67
 
67
68
  say_status :active_record, "The plugin has been configured. For usage help visit:"
68
69
  say_status :active_record, "https://github.com/bridgetownrb/bridgetown-activerecord/blob/main/README.md"
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV["RAILS_ENV"] = Bridgetown.environment
4
+
5
+ require "active_record"
6
+ require "active_support/configuration_file"
7
+
8
+ module BridgetownActiveRecord
9
+ def self.load_tasks(models_dir: "models")
10
+ ENV["BRIDGETOWN_ACTIVERECORD_MODELS_DIR"] ||= models_dir
11
+ load File.expand_path("../tasks/database.rake", __dir__)
12
+ end
13
+
14
+ def self.db_configuration(config)
15
+ ActiveSupport::ConfigurationFile.parse(File.join(config.root_dir, "config", "database.yml"))
16
+ end
17
+
18
+ def self.log_writer
19
+ Bridgetown::LogWriter.new.tap(&:enable_prefix)
20
+ end
21
+ end
22
+
23
+ Bridgetown.initializer :"bridgetown-activerecord" do |config|
24
+ ActiveRecord::Base.establish_connection(
25
+ BridgetownActiveRecord.db_configuration(config)[Bridgetown.environment]
26
+ )
27
+ ActiveRecord::Base.logger = BridgetownActiveRecord.log_writer
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgetownActiveRecord
4
- VERSION = "1.0.0"
4
+ VERSION = "2.1.0"
5
5
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bridgetown"
4
- require "bridgetown-activerecord/builder"
4
+ require "bridgetown-activerecord/initializer"
@@ -5,14 +5,17 @@ ENV["SCHEMA"] = File.join(Dir.pwd, "db", "schema.rb")
5
5
  require "standalone_migrations"
6
6
  StandaloneMigrations::Tasks.load_tasks
7
7
 
8
+ require "./#{ENV.fetch("BRIDGETOWN_ACTIVERECORD_MODELS_DIR", "models")}/application_record"
9
+
8
10
  # Setup Annotate so it runs on db:migrate, etc.
9
- require "annotate"
10
- require "./models/application_record"
11
- ENV["model_dir"] = "models"
12
- Annotate::Helpers.class_eval do
13
- # We need to redefine this because our custom model_dir shortcircuits the migration task enhancement
14
- def self.include_models?
15
- true
11
+ if Bridgetown.environment.development?
12
+ require "annotate"
13
+ ENV["model_dir"] = ENV.fetch("BRIDGETOWN_ACTIVERECORD_MODELS_DIR", "models")
14
+ Annotate::Helpers.class_eval do
15
+ # We need to redefine this because our custom model_dir shortcircuits the migration task enhancement
16
+ def self.include_models?
17
+ true
18
+ end
16
19
  end
20
+ Annotate.load_tasks
17
21
  end
18
- Annotate.load_tasks
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: 1.0.0
4
+ version: 2.1.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-04-10 00:00:00.000000000 Z
11
+ date: 2022-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: 1.2.0.beta2
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '2.0'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '1.0'
29
+ version: 1.2.0.beta2
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '2.0'
@@ -116,7 +116,7 @@ files:
116
116
  - bridgetown-activerecord.gemspec
117
117
  - bridgetown.automation.rb
118
118
  - lib/bridgetown-activerecord.rb
119
- - lib/bridgetown-activerecord/builder.rb
119
+ - lib/bridgetown-activerecord/initializer.rb
120
120
  - lib/bridgetown-activerecord/version.rb
121
121
  - lib/tasks/database.rake
122
122
  homepage: https://github.com/bridgetownrb/bridgetown-activerecord
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ENV["RAILS_ENV"] = Bridgetown.environment
4
-
5
- require "active_record"
6
- require "active_support/configuration_file"
7
-
8
- module BridgetownActiveRecord
9
- def self.load_tasks
10
- load File.expand_path("../tasks/database.rake", __dir__)
11
- end
12
-
13
- class Builder < Bridgetown::Builder
14
- def self.db_configuration(site)
15
- ActiveSupport::ConfigurationFile.parse(site.in_root_dir("config", "database.yml"))
16
- end
17
-
18
- def self.log_writer
19
- Bridgetown::LogWriter.new.tap(&:enable_prefix)
20
- end
21
-
22
- Bridgetown::Hooks.register_one :site, :after_init, reloadable: false do |site|
23
- ActiveRecord::Base.establish_connection db_configuration(site)[Bridgetown.environment]
24
- ActiveRecord::Base.logger = log_writer
25
- end
26
-
27
- # no-op
28
- def build; end
29
- end
30
- end
31
-
32
- BridgetownActiveRecord::Builder.register