dynamic-active-model-rails 0.13.0 → 0.14.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: a8515d28d9394768614fb8cd2f4cf49173ab1d5f97c905debbf3f9dc85d21f21
4
- data.tar.gz: 33388b8956cc6106da4d6d94d0255a15b66d6156be30a49cfa9c941f31f3ed4e
3
+ metadata.gz: e99654c467d82a550320a4952955c2ac035a218940ba178f4aab57ea6036c438
4
+ data.tar.gz: fc7f43e364ced45bd2d52e0ea918ae4498ea690d3414a1c830c20f03495dc836
5
5
  SHA512:
6
- metadata.gz: ed7b1653b8ea5643e50b797a0cad76714958d204052ee97ba44cd10eed2e3f2103e20bcad4c16ee7277a12886ef85cceb6421a8ca41c485dec72dbac67b89fc4
7
- data.tar.gz: 42a9e8a11125f32d8e9ea403662e232d602595c776c7829a05f7759fd14f4c9fa88822100fd92dd52c2e2188fb39c1d95082eb62c0bef16f6f5c2c17ba13beed
6
+ metadata.gz: edd5f4bacf38a6a2e3fa610da7f6fe4aa01e781d7675feeb39164e97085842fbdaeb85d6ba8bc027b09672a58adaec9f875db1cdf352868a5c91b38ecece64cb
7
+ data.tar.gz: 6e5616c8bb2b5ca5c3fc7868f51d6b22b9c40ec0bfb6fd3d8c9942e57fc321eb7c445b1a09628416c71aee958b0c926c2cf7ca2239cce482fa9f5de48b5fcbbf
data/README.md CHANGED
@@ -13,6 +13,21 @@ gem 'dynamic-active-model-rails'
13
13
 
14
14
  `dynamic-active-model-rails` is released in lockstep with `dynamic-active-model` and pins the same version.
15
15
 
16
+ ## Generators
17
+
18
+ ```bash
19
+ bin/rails generate dynamic_active_model:install # DB, app/models/db/
20
+ bin/rails generate dynamic_active_model:install grant_db # GrantDB, app/models/grant_db/
21
+ bin/rails generate dynamic_active_model:database cars --connection cars
22
+ bin/rails generate dynamic_active_model:extension grant_db users # app/models/grant_db/users.ext.rb
23
+ ```
24
+
25
+ - **`install [NAME]`** creates `config/initializers/dynamic_active_model.rb`, declaring the first database (default `db`), and its models folder.
26
+ - **`database NAME`** adds another `add_database` line to the initializer and creates the folder. It refuses a namespace the app already declares.
27
+ - **`extension DATABASE TABLE`** creates an extension file. `DATABASE` can be a name (`grant_db`) or a namespace (`GrantDB`). The generator uses the app's configuration, so a custom `extensions_path:` or `extensions_suffix:` is respected.
28
+
29
+ `--connection NAME` points the database at a `database.yml` entry. The generators warn if the current environment has no such entry. Without `--connection`, the database shares `ApplicationRecord`'s connection.
30
+
16
31
  ## Configuration
17
32
 
18
33
  ```ruby
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require_relative '../database_arguments'
5
+
6
+ module DynamicActiveModel
7
+ module Generators
8
+ # rails g dynamic_active_model:database NAME [--connection=NAME]
9
+ class DatabaseGenerator < ::Rails::Generators::Base
10
+ include DatabaseArguments
11
+
12
+ desc 'Declares another database in config/initializers/dynamic_active_model.rb ' \
13
+ '(e.g. cars => CarsDB in app/models/cars_db/).'
14
+
15
+ argument :name, type: :string, banner: 'NAME'
16
+
17
+ def check_initializer
18
+ return if File.exist?(File.join(destination_root, INITIALIZER))
19
+
20
+ raise Thor::Error, "#{INITIALIZER} not found; run rails g dynamic_active_model:install first"
21
+ end
22
+
23
+ def check_not_declared
24
+ raise Thor::Error, "#{definition.module_name} is already declared" if declared?
25
+ end
26
+
27
+ def add_database
28
+ inject_into_file INITIALIZER, " #{add_database_line}\n", before: /^end\b/
29
+ end
30
+
31
+ def create_models_folder
32
+ create_extensions_folder
33
+ end
34
+
35
+ def check_connection
36
+ warn_about_missing_connection
37
+ end
38
+
39
+ private
40
+
41
+ # @return [Boolean] Whether the app already declares this namespace
42
+ def declared?
43
+ DynamicActiveModel::Rails.configuration.definitions.any? { |db| db.module_name == definition.module_name }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicActiveModel
4
+ module Generators
5
+ # Shared --connection option and helpers for generators that declare a database
6
+ module DatabaseArguments
7
+ INITIALIZER = 'config/initializers/dynamic_active_model.rb'
8
+
9
+ def self.included(base)
10
+ base.class_option :connection,
11
+ type: :string,
12
+ desc: "database.yml entry to connect to (default: share ApplicationRecord's connection)"
13
+ end
14
+
15
+ private
16
+
17
+ # @return [DynamicActiveModel::Rails::DatabaseDefinition] Naming for the NAME argument
18
+ def definition
19
+ @definition ||= DynamicActiveModel::Rails::DatabaseDefinition.new(name)
20
+ end
21
+
22
+ # @return [String] e.g. "config.add_database :cars, :cars"
23
+ def add_database_line
24
+ args = [name.underscore.to_sym.inspect]
25
+ args << options[:connection].to_sym.inspect if options[:connection]
26
+ "config.add_database #{args.join(', ')}"
27
+ end
28
+
29
+ # Creates the database's models folder for its extension files
30
+ # @return [void]
31
+ def create_extensions_folder
32
+ create_file File.join(definition.extensions_path(destination_root), '.keep')
33
+ end
34
+
35
+ # Warns when --connection names a database.yml entry the current environment lacks
36
+ # @return [void]
37
+ def warn_about_missing_connection
38
+ return unless options[:connection]
39
+ return if ActiveRecord::Base.configurations.configs_for(env_name: ::Rails.env, name: options[:connection])
40
+
41
+ say_status :warning, "config/database.yml has no #{options[:connection]} entry for #{::Rails.env}", :yellow
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module DynamicActiveModel
6
+ module Generators
7
+ # rails g dynamic_active_model:extension DATABASE TABLE
8
+ class ExtensionGenerator < ::Rails::Generators::Base
9
+ source_root File.expand_path('templates', __dir__)
10
+ desc 'Creates an extension file for a table of a declared database ' \
11
+ '(e.g. grant_db users => app/models/grant_db/users.ext.rb).'
12
+
13
+ argument :database, type: :string, banner: 'DATABASE'
14
+ argument :table, type: :string, banner: 'TABLE'
15
+
16
+ def create_extension
17
+ template 'extension.rb.tt', File.join(definition.extensions_path(destination_root), file_name)
18
+ end
19
+
20
+ private
21
+
22
+ # The declared database named by DATABASE, as a name (grant_db) or namespace (GrantDB)
23
+ # @return [DynamicActiveModel::Rails::DatabaseDefinition]
24
+ # @raise [Thor::Error] If no such database is declared
25
+ def definition
26
+ @definition ||= declared.find { |db| [database, default_module_name].include?(db.module_name) } ||
27
+ raise(Thor::Error, "no database #{database} is declared " \
28
+ "(declared: #{declared.map(&:module_name).join(', ')})")
29
+ end
30
+
31
+ # @return [Array<DynamicActiveModel::Rails::DatabaseDefinition>]
32
+ def declared
33
+ DynamicActiveModel::Rails.configuration.definitions
34
+ end
35
+
36
+ # @return [String] Namespace DATABASE maps to by default, e.g. "GrantDB"
37
+ def default_module_name
38
+ DynamicActiveModel::Rails::DatabaseDefinition.new(database).module_name
39
+ end
40
+
41
+ # @return [String] e.g. "users.ext.rb"
42
+ def file_name
43
+ "#{table}#{definition.extensions_suffix}"
44
+ end
45
+
46
+ # @return [String] e.g. "GrantDB::User"
47
+ def model_name
48
+ "#{definition.module_name}::#{definition.table_class_names[table] || table.classify}"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extends <%= model_name %> (table <%= table %>). Columns and associations that
4
+ # follow foreign key conventions are detected from the schema; add the rest here:
5
+ # through/polymorphic associations, validations, scopes and methods.
6
+ update_model do
7
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require_relative '../database_arguments'
5
+
6
+ module DynamicActiveModel
7
+ module Generators
8
+ # rails g dynamic_active_model:install [NAME] [--connection=NAME]
9
+ class InstallGenerator < ::Rails::Generators::Base
10
+ include DatabaseArguments
11
+
12
+ source_root File.expand_path('templates', __dir__)
13
+ desc 'Creates config/initializers/dynamic_active_model.rb declaring a first database ' \
14
+ '(default: db, i.e. DB in app/models/db/).'
15
+
16
+ argument :name, type: :string, default: 'db', banner: 'NAME'
17
+
18
+ def create_initializer
19
+ template 'dynamic_active_model.rb.tt', INITIALIZER
20
+ end
21
+
22
+ def create_models_folder
23
+ create_extensions_folder
24
+ end
25
+
26
+ def check_connection
27
+ warn_about_missing_connection
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Each database's tables become ActiveRecord models in a <Name>DB namespace. Models
4
+ # are built on first use and rebuilt after code reloads and migrations. Per-table
5
+ # extensions go in app/models/<name>_db/<table_name>.ext.rb.
6
+ # https://github.com/dougyouch/dynamic-active-model/tree/master/dynamic-active-model-rails
7
+ #
8
+ # Add databases with bin/rails g dynamic_active_model:database NAME, or by hand:
9
+ # config.add_database :cars, :cars do |db| # CarsDB, "cars" entry in database.yml
10
+ # db.skip_tables 'legacy_*'
11
+ # end
12
+ DynamicActiveModel::Rails.configure do |config|
13
+ <%= add_database_line %>
14
+ end
15
+
16
+ # Setup that touches the models (e.g. has_paper_trail) belongs in a load hook,
17
+ # which runs after every build of <%= definition.module_name %>'s models:
18
+ # ActiveSupport.on_load(<%= definition.load_hook.inspect %>) do
19
+ # get_model!(:users).has_paper_trail
20
+ # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic-active-model-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.13.0
32
+ version: 0.14.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 0.13.0
39
+ version: 0.14.0
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: railties
42
42
  requirement: !ruby/object:Gem::Requirement
@@ -69,6 +69,12 @@ files:
69
69
  - lib/dynamic-active-model/rails/lazy_namespace.rb
70
70
  - lib/dynamic-active-model/rails/railtie.rb
71
71
  - lib/dynamic-active-model/rails/schema_change_hook.rb
72
+ - lib/generators/dynamic_active_model/database/database_generator.rb
73
+ - lib/generators/dynamic_active_model/database_arguments.rb
74
+ - lib/generators/dynamic_active_model/extension/extension_generator.rb
75
+ - lib/generators/dynamic_active_model/extension/templates/extension.rb.tt
76
+ - lib/generators/dynamic_active_model/install/install_generator.rb
77
+ - lib/generators/dynamic_active_model/install/templates/dynamic_active_model.rb.tt
72
78
  homepage: https://github.com/dougyouch/dynamic-active-model/tree/master/dynamic-active-model-rails
73
79
  licenses:
74
80
  - MIT