rails_drivers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '03862c5a464a55793074ecb863ac4e7eb4530172923a5f0a8207d8051bf54f96'
4
+ data.tar.gz: ffc80ffc2db7d8f71f5598f388fa3cce4efc8399362aa841d3b21cc776508df3
5
+ SHA512:
6
+ metadata.gz: 2a89e522b5a3fac4be2323be01f8d9e2085e2b54ceee263b1835b75bbae7df2397d68b57c690bcf11f7df1ca5efce2be9add211bcd7a247a8d96e3d3dc1bf7e0
7
+ data.tar.gz: df27e76af55099c0031644db8a156124ffa2b72d9ef67ef6a4427119f3c35497979814348bf86cf8ec4a20065834d6dc3839f27647f248eccee2cf2c16812fff
data/MIT-LICENSE ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,94 @@
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 way to put code into a different folder, but there are some rules we like to follow in order to reduce application complexity:
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
+ Thankfully, we can test that these rules are adhered to by removing drivers before running the test suite.
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 one useful property: they can be freely added and removed from your project without breaking anything.
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
+ (Optional) Add these lines to your `spec/rails_helper.rb`
82
+
83
+ ```ruby
84
+ Dir[Rails.root.join("drivers/*/spec/support/*.rb")].each { |f| require f }
85
+
86
+ RSpec.configure do |config|
87
+ Dir[Rails.root.join('drivers/*/spec')].each { |x| config.project_source_dirs << x }
88
+ Dir[Rails.root.join('drivers/*/lib')].each { |x| config.project_source_dirs << x }
89
+ Dir[Rails.root.join('drivers/*/app')].each { |x| config.project_source_dirs << x }
90
+ end
91
+ ```
92
+
93
+ ## License
94
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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'
data/bin/driver ADDED
@@ -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,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsDrivers
4
+ module Setup
5
+ DRIVER_PATHS = %w[
6
+ app
7
+ app/models
8
+ app/views
9
+ app/controllers
10
+ app/mailers
11
+ config/initializers
12
+ db db/migrate
13
+ lib
14
+ ].freeze
15
+
16
+ #
17
+ # This allows Rails to find models, views, controllers, etc inside of drivers.
18
+ #
19
+ def setup_paths
20
+ # This REPLACE_DEFAULT_PATH_WITH_DRIVER constant gets defined by bin/driver when we want
21
+ # to run a command in the context of a driver instead of the main rails app.
22
+ if defined?(REPLACE_DEFAULT_PATH_WITH_DRIVER)
23
+ replace_rails_paths_with_driver(REPLACE_DEFAULT_PATH_WITH_DRIVER)
24
+ else
25
+ add_every_driver_to_rails_paths
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def rails_config
32
+ Rails.application.config
33
+ end
34
+
35
+ def replace_rails_paths_with_driver(driver_name)
36
+ DRIVER_PATHS.each do |path|
37
+ rails_config.paths[path] = "drivers/#{driver_name}/#{path}"
38
+ rails_config.autoload_paths = ["#{rails_config.root}/drivers/#{driver_name}/lib"]
39
+ end
40
+ end
41
+
42
+ def add_every_driver_to_rails_paths
43
+ Dir['drivers/*'].each do |driver|
44
+ DRIVER_PATHS.each do |path|
45
+ rails_config.paths[path] << "#{driver}/#{path}"
46
+ end
47
+
48
+ # We want to autoload driver/*/lib folders
49
+ rails_config.autoload_paths += ["#{rails_config.root}/#{driver}/lib"]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsDrivers
4
+ VERSION = '0.1.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,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_drivers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nigel Baillie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-12 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
+ description: Like Rails Engines, but without the friction. Your Rails app can't access
56
+ them, and they can't access each other.
57
+ email:
58
+ - nbaillie@degica.com
59
+ executables:
60
+ - driver
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - MIT-LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - bin/driver
68
+ - lib/generators/driver/USAGE
69
+ - lib/generators/driver/driver_generator.rb
70
+ - lib/generators/driver/templates/initializer.rb.erb
71
+ - lib/generators/driver/templates/module.rb.erb
72
+ - lib/generators/driver/templates/routes.rb.erb
73
+ - lib/rails_drivers.rb
74
+ - lib/rails_drivers/railtie.rb
75
+ - lib/rails_drivers/routes.rb
76
+ - lib/rails_drivers/setup.rb
77
+ - lib/rails_drivers/version.rb
78
+ - lib/tasks/rails_drivers_tasks.rake
79
+ homepage: https://github.com/degica/rails_drivers
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.0.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: De-coupled separation of concerns for Rails
102
+ test_files: []