rails_drivers 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 575d8e9162b65431801bf6d419fe5fca254d65fdd3a840877ea1a0c186bd90bd
4
+ data.tar.gz: 06e51508f802ee83223d9b9a0bea05f91f93d0813cf1aba6503f29d63cb9d631
5
+ SHA512:
6
+ metadata.gz: 65a00d958eaf2d888c565149f840ac43f0048587b332c433c33734e38652bb31d5a90c6d3bb439eaca6c5e3c1b7b2fa7d9703f97a45dad40b059a3f3e05e298f
7
+ data.tar.gz: b5a03060c043e906ea25955a23513a6939a532fecb560c81cb3bc9075d708195d9bf60135e620a268ca025ec81a9ea90b17c0f0baf957761b091b330ef763a73
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Nigel Baillie
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,134 @@
1
+ # RailsDrivers
2
+
3
+ ## What are these "drivers"?
4
+
5
+ Each driver is like a mini Rails app that has full access to the main app. A driver has its own `app`, `config`, `spec`, and `db` folder.
6
+
7
+ Technically speaking, drivers are just a fancy name for putting code into a different folder. The advantage of doing this is that it provides clear-cut separation of concerns. If we follow a couple of simple rules, we can actually test that separation:
8
+
9
+ - Drivers should not touch other drivers
10
+ - The main app should not touch drivers directly
11
+
12
+ The "main app" refers to the files inside your `<project root>/app` directory.
13
+
14
+ If your test suite is good, you can test that these rules are adhered to by selectively adding and removing drivers before running your tests.
15
+
16
+ ## Aren't these just engines?
17
+
18
+ Very similar, yes. They use the same Rails facilities for adding new `app` paths, etc.
19
+
20
+ But Drivers have less friction. They can be freely added and removed from your project without breaking anything. There's no need to mess around with gems, vendoring, or dummy apps.
21
+
22
+ ## Usage
23
+
24
+ Every folder inside `drivers` has its own `app`, `config`, `db`, and `spec` folders. They are effectively a part of the overall Rails app.
25
+
26
+ ### Creating a new driver
27
+
28
+ Just run `rails g driver my_new_driver_name`.
29
+
30
+ ### Creating migrations for a driver
31
+
32
+ `bundle exec driver my_driver_name generate migration blah etc_etc:string`
33
+
34
+ The `driver` utility technically works with other generators and rake tasks, but is only guaranteed to work with migrations.
35
+ The reason is that some generators assume a particular path, rather than using the Rails path methods.
36
+
37
+ ### Testing for coupling
38
+
39
+ Since drivers are merged into your main application just like engines, there's nothing stopping them from accessing other drivers, and there's nothing stopping your main application from accessing drivers. In order to ensure those things don't happen, we have a handful of rake tasks:
40
+
41
+ 1. `rake driver:isolate[<name of driver>] # leaves you with only one driver`
42
+ 2. `rake driver:clear # removes all drivers`
43
+ 3. `rake driver:restore # restores all drivers`
44
+
45
+ Suppose you have a driver called `store` and a driver called `admin`. You don't want `store` and `admin` to talk to each other.
46
+
47
+ ```bash
48
+ # Run specs with store driver only
49
+ rake driver:isolate[store]
50
+ rspec --pattern '{spec,drivers/*/spec}/**{,/*/**}/*_spec.rb'
51
+ rake driver:restore
52
+
53
+ # Run specs with admin driver only
54
+ rake driver:isolate[admin]
55
+ rspec --pattern '{spec,drivers/*/spec}/**{,/*/**}/*_spec.rb'
56
+ rake driver:restore
57
+ ```
58
+
59
+ This lets you to ensure that the store and admin function properly without each other. Note we're running all of the main app's specs twice. This is good because we also want to make sure the main app is not reaching into drivers.
60
+
61
+ Of course there's nothing stopping you from using if-statements to detect whether a driver is present. It's up to you to determine what's a "safe" level of crossover. Generally, if you find yourself using a lot of those if-statements, you should consider rethinking which functionality belongs in a driver and which functionality belongs in your main app.
62
+
63
+ ## Installation
64
+ Add this line to your application's Gemfile:
65
+
66
+ ```ruby
67
+ gem 'rails_drivers'
68
+ ```
69
+
70
+ And then execute:
71
+ ```bash
72
+ $ bundle install
73
+ ```
74
+
75
+ Add this line to your routes.rb:
76
+
77
+ ```ruby
78
+ require 'rails_drivers/routes'
79
+ ```
80
+
81
+ ### RSpec
82
+
83
+ If you use RSpec, add these lines to your `spec/rails_helper.rb` or `spec/spec_helper.rb`:
84
+
85
+ ```ruby
86
+ Dir[Rails.root.join("drivers/*/spec/support/*.rb")].each { |f| require f }
87
+
88
+ RSpec.configure do |config|
89
+ Dir[Rails.root.join('drivers/*/spec')].each { |x| config.project_source_dirs << x }
90
+ Dir[Rails.root.join('drivers/*/lib')].each { |x| config.project_source_dirs << x }
91
+ Dir[Rails.root.join('drivers/*/app')].each { |x| config.project_source_dirs << x }
92
+ end
93
+ ```
94
+
95
+ ### Webpacker
96
+
97
+ If you use Webpacker, take a look at this snippet. You'll want to add the code between the comments:
98
+
99
+ ```javascript
100
+ // config/webpack/environment.js
101
+ const { environment } = require('@rails/webpacker')
102
+
103
+ //// Begin driver code ////
104
+ const { config } = require('@rails/webpacker')
105
+ const { sync } = require('glob')
106
+ const { basename, dirname, join, relative, resolve } = require('path')
107
+ const extname = require('path-complete-extname')
108
+
109
+ const getExtensionsGlob = () => {
110
+ const { extensions } = config
111
+ return extensions.length === 1 ? `**/*${extensions[0]}` : `**/*{${extensions.join(',')}}`
112
+ }
113
+
114
+ const addToEntryObject = (sourcePath) => {
115
+ const glob = getExtensionsGlob()
116
+ const rootPath = join(sourcePath, config.source_entry_path)
117
+ const paths = sync(join(rootPath, glob))
118
+ paths.forEach((path) => {
119
+ const namespace = relative(join(rootPath), dirname(path))
120
+ const name = join(namespace, basename(path, extname(path)))
121
+ environment.entry.set(name, resolve(path))
122
+ })
123
+ }
124
+
125
+ sync('drivers/*').forEach((driverPath) => {
126
+ addToEntryObject(join(driverPath, config.source_path));
127
+ })
128
+ //// End driver code ////
129
+
130
+ module.exports = environment
131
+ ```
132
+
133
+ ## License
134
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'RailsDrivers'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ APP_PATH = File.expand_path('config/application')
5
+ require_relative "#{Dir.pwd}/config/boot"
6
+
7
+ REPLACE_DEFAULT_PATH_WITH_DRIVER = ARGV.shift
8
+
9
+ possible_drivers = Dir['drivers/*'].map { |d| d.split('/').last }
10
+ unless possible_drivers.include?(REPLACE_DEFAULT_PATH_WITH_DRIVER)
11
+ puts "Unknown driver #{REPLACE_DEFAULT_PATH_WITH_DRIVER}. Must be one of [#{possible_drivers.join(', ')}]"
12
+ exit 1
13
+ end
14
+
15
+ require 'rails/commands'
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Generates the directory structure for a new driver.
3
+
4
+ Example:
5
+ rails generate driver new_feature
6
+
7
+ This will create:
8
+ drivers/new_feature
9
+ drivers/new_feature/app
10
+ drivers/new_feature/app/controllers/new_feature
11
+ drivers/new_feature/app/models/new_feature
12
+ drivers/new_feature/app/views/new_feature
13
+ drivers/new_feature/config
14
+ drivers/new_feature/spec
15
+ drivers/new_feature/lib
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DriverGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('templates', __dir__)
5
+
6
+ def create_driver_dir_structure
7
+ create_file "drivers/#{file_name}/app/models/#{file_name}/.keep", ''
8
+ create_file "drivers/#{file_name}/app/controllers/#{file_name}/.keep", ''
9
+ create_file "drivers/#{file_name}/app/views/#{file_name}/.keep", ''
10
+ create_file "drivers/#{file_name}/spec/.keep", ''
11
+ create_file "drivers/#{file_name}/db/migrate/.keep", ''
12
+ create_file "drivers/#{file_name}/lib/.keep", ''
13
+
14
+ template 'routes.rb.erb', "drivers/#{file_name}/config/routes.rb"
15
+ template 'initializer.rb.erb', "drivers/#{file_name}/config/initializers/#{file_name}_feature.rb"
16
+ template 'module.rb.erb', "drivers/#{file_name}/app/models/#{file_name}.rb"
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ # The core app (or other drivers) can check the presence of the
2
+ # <%= class_name %> driver with the following code snippit
3
+ #
4
+ # do_something if RailsDrivers.loaded.include(:<%= file_name %>)
5
+ #
6
+ # use with caution!
7
+ RailsDrivers.loaded << :<%= file_name %>
@@ -0,0 +1,5 @@
1
+ module <%= class_name %>
2
+ def self.table_name_prefix
3
+ '<%= plural_name.singularize %>_'
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ <%= Rails.application.class.name %>.routes.draw do
2
+ scope :<%= plural_name.singularize %> do
3
+ # TODO
4
+ # get '/my_path', to: '<%= file_name %>/my_controller'
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_drivers/version'
4
+ require 'rails_drivers/setup'
5
+ require 'rails_drivers/railtie'
6
+
7
+ module RailsDrivers
8
+ def self.loaded
9
+ @loaded ||= []
10
+ end
11
+
12
+ def self.freeze!
13
+ @loaded = @loaded&.freeze
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsDrivers
4
+ class Railtie < ::Rails::Railtie
5
+ include ::RailsDrivers::Setup
6
+
7
+ rake_tasks do
8
+ load File.expand_path("#{__dir__}/../tasks/rails_drivers_tasks.rake")
9
+ end
10
+
11
+ config.before_configuration { setup_paths }
12
+ config.after_initialize { RailsDrivers.freeze! }
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsDrivers
4
+ class Routes
5
+ def self.load_driver_routes
6
+ return if defined?(REPLACE_DEFAULT_PATH_WITH_DRIVER)
7
+
8
+ Dir[Rails.root.join('drivers/*')].each do |path|
9
+ load "#{path}/config/routes.rb" if File.exist?("#{path}/config/routes.rb")
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ # This is meant to be executed as soon as the file is required
16
+ RailsDrivers::Routes.load_driver_routes
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsDrivers
4
+ module Setup
5
+ DRIVER_PATHS = %w[
6
+ app
7
+ app/assets
8
+ app/models
9
+ app/views
10
+ app/controllers
11
+ app/mailers
12
+ config/initializers
13
+ db db/migrate
14
+ lib
15
+ ].freeze
16
+
17
+ #
18
+ # This allows Rails to find models, views, controllers, etc inside of drivers.
19
+ #
20
+ def setup_paths
21
+ # This REPLACE_DEFAULT_PATH_WITH_DRIVER constant gets defined by bin/driver when we want
22
+ # to run a command in the context of a driver instead of the main rails app.
23
+ if defined?(REPLACE_DEFAULT_PATH_WITH_DRIVER)
24
+ replace_rails_paths_with_driver(REPLACE_DEFAULT_PATH_WITH_DRIVER)
25
+ else
26
+ add_every_driver_to_rails_paths
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def rails_config
33
+ Rails.application.config
34
+ end
35
+
36
+ def replace_rails_paths_with_driver(driver_name)
37
+ DRIVER_PATHS.each do |path|
38
+ rails_config.paths[path] = "drivers/#{driver_name}/#{path}"
39
+ rails_config.autoload_paths = ["#{rails_config.root}/drivers/#{driver_name}/lib"]
40
+ end
41
+ end
42
+
43
+ def add_every_driver_to_rails_paths
44
+ Dir['drivers/*'].each do |driver|
45
+ DRIVER_PATHS.each do |path|
46
+ rails_config.paths[path] << "#{driver}/#{path}"
47
+ end
48
+
49
+ # We want to autoload driver/*/lib folders
50
+ rails_config.autoload_paths += ["#{rails_config.root}/#{driver}/lib"]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsDrivers
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :driver do
4
+ DriverError = Class.new(StandardError)
5
+
6
+ desc 'Removes every driver but the one specified. Can be undone with driver:restore.'
7
+ task :isolate, [:driver] do |_t, args|
8
+ include FileUtils
9
+
10
+ raise DriverError, 'No driver specified' if args.driver.blank?
11
+ raise DriverError, 'Driver not found' unless File.exist?("drivers/#{args.driver}")
12
+
13
+ mkdir_p 'tmp/drivers'
14
+ Dir['drivers/*'].each do |driver_path|
15
+ next if driver_path.include?("/#{args.driver}")
16
+
17
+ mv driver_path, "tmp/#{driver_path}"
18
+ puts "Moved #{driver_path} to tmp/drivers/"
19
+ end
20
+
21
+ rescue DriverError => e
22
+ puts e.message
23
+ end
24
+
25
+ desc 'Removes all drivers. Can be undone with driver:restore.'
26
+ task :clear do
27
+ include FileUtils
28
+
29
+ mkdir_p 'tmp/drivers'
30
+ Dir['drivers/*'].each do |driver_path|
31
+ mv driver_path, "tmp/#{driver_path}"
32
+ puts "Moved #{driver_path} to tmp/drivers/"
33
+ end
34
+ end
35
+
36
+ desc 'Undoes the effects of driver:isolate and driver:clear.'
37
+ task :restore do
38
+ include FileUtils
39
+
40
+ Dir['tmp/drivers/*'].each do |tmp_driver_path|
41
+ driver = tmp_driver_path.split('/').last
42
+ mv tmp_driver_path, "drivers/#{driver}"
43
+ puts "Moved #{tmp_driver_path} to drivers/"
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_drivers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Nigel Baillie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webpacker
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ description: Like Rails Engines, but without the friction. Your Rails app can't access
70
+ them, and they can't access each other.
71
+ email:
72
+ - nbaillie@degica.com
73
+ executables:
74
+ - driver
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - MIT-LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/driver
82
+ - lib/generators/driver/USAGE
83
+ - lib/generators/driver/driver_generator.rb
84
+ - lib/generators/driver/templates/initializer.rb.erb
85
+ - lib/generators/driver/templates/module.rb.erb
86
+ - lib/generators/driver/templates/routes.rb.erb
87
+ - lib/rails_drivers.rb
88
+ - lib/rails_drivers/railtie.rb
89
+ - lib/rails_drivers/routes.rb
90
+ - lib/rails_drivers/setup.rb
91
+ - lib/rails_drivers/version.rb
92
+ - lib/tasks/rails_drivers_tasks.rake
93
+ homepage: https://github.com/degica/rails_drivers
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.0.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: De-coupled separation of concerns for Rails
116
+ test_files: []