sublease 0.1.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.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +62 -0
  3. data/.travis.yml +68 -0
  4. data/CHANGELOG.md +3 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +29 -0
  7. data/Gemfile.lock +141 -0
  8. data/LICENSE +21 -0
  9. data/README.md +126 -0
  10. data/Rakefile +36 -0
  11. data/app/models/sublease/lodger.rb +71 -0
  12. data/app/models/sublease/tenant.rb +29 -0
  13. data/bin/rails +14 -0
  14. data/config/locales/de.yml +28 -0
  15. data/config/locales/en.yml +28 -0
  16. data/config/locales/es.yml +28 -0
  17. data/config/locales/fr.yml +28 -0
  18. data/config/locales/it.yml +28 -0
  19. data/config/locales/po.yml +28 -0
  20. data/gemfiles/Gemfile.jruby.rails-4.1-stable +29 -0
  21. data/gemfiles/Gemfile.jruby.rails-4.1-stable.lock +129 -0
  22. data/gemfiles/Gemfile.jruby.rails-4.2-stable +29 -0
  23. data/gemfiles/Gemfile.jruby.rails-4.2-stable.lock +152 -0
  24. data/gemfiles/Gemfile.jruby.rails-5.0-stable +29 -0
  25. data/gemfiles/Gemfile.jruby.rails-5.0-stable.lock +163 -0
  26. data/gemfiles/Gemfile.rails-4.1-stable +29 -0
  27. data/gemfiles/Gemfile.rails-4.1-stable.lock +115 -0
  28. data/gemfiles/Gemfile.rails-4.2-stable +29 -0
  29. data/gemfiles/Gemfile.rails-4.2-stable.lock +140 -0
  30. data/gemfiles/Gemfile.rails-5.0-stable +29 -0
  31. data/gemfiles/Gemfile.rails-5.0-stable.lock +141 -0
  32. data/lib/generators/sublease/install/USAGE +5 -0
  33. data/lib/generators/sublease/install/install_generator.rb +9 -0
  34. data/lib/generators/sublease/install/templates/sublease.rb +23 -0
  35. data/lib/sublease.rb +132 -0
  36. data/lib/sublease/engine.rb +5 -0
  37. data/lib/sublease/errors.rb +10 -0
  38. data/lib/sublease/tenant_switcher.rb +75 -0
  39. data/lib/sublease/version.rb +5 -0
  40. data/sublease.gemspec +28 -0
  41. data/test/dummy/README.rdoc +28 -0
  42. data/test/dummy/Rakefile +6 -0
  43. data/test/dummy/app/assets/images/.keep +0 -0
  44. data/test/dummy/app/assets/javascripts/application.js +13 -0
  45. data/test/dummy/app/assets/javascripts/lodgers.js +2 -0
  46. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  47. data/test/dummy/app/assets/stylesheets/lodgers.css +4 -0
  48. data/test/dummy/app/controllers/application_controller.rb +7 -0
  49. data/test/dummy/app/controllers/concerns/.keep +0 -0
  50. data/test/dummy/app/controllers/lodgers_controller.rb +7 -0
  51. data/test/dummy/app/helpers/application_helper.rb +2 -0
  52. data/test/dummy/app/helpers/lodgers_helper.rb +2 -0
  53. data/test/dummy/app/mailers/.keep +0 -0
  54. data/test/dummy/app/models/.keep +0 -0
  55. data/test/dummy/app/models/concerns/.keep +0 -0
  56. data/test/dummy/app/models/lodger.rb +3 -0
  57. data/test/dummy/app/models/tenant.rb +3 -0
  58. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  59. data/test/dummy/app/views/lodgers/index.html.erb +6 -0
  60. data/test/dummy/bin/bundle +3 -0
  61. data/test/dummy/bin/rails +4 -0
  62. data/test/dummy/bin/rake +4 -0
  63. data/test/dummy/config.ru +4 -0
  64. data/test/dummy/config/application.rb +23 -0
  65. data/test/dummy/config/boot.rb +5 -0
  66. data/test/dummy/config/database.yml +23 -0
  67. data/test/dummy/config/environment.rb +5 -0
  68. data/test/dummy/config/environments/development.rb +37 -0
  69. data/test/dummy/config/environments/production.rb +78 -0
  70. data/test/dummy/config/environments/test.rb +39 -0
  71. data/test/dummy/config/initializers/assets.rb +8 -0
  72. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  73. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  74. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  75. data/test/dummy/config/initializers/inflections.rb +16 -0
  76. data/test/dummy/config/initializers/mime_types.rb +4 -0
  77. data/test/dummy/config/initializers/session_store.rb +3 -0
  78. data/test/dummy/config/initializers/sublease.rb +24 -0
  79. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  80. data/test/dummy/config/locales/en.yml +23 -0
  81. data/test/dummy/config/routes.rb +3 -0
  82. data/test/dummy/config/secrets.yml +22 -0
  83. data/test/dummy/db/migrate/20180114164205_create_tenants.rb +10 -0
  84. data/test/dummy/db/migrate/20180115012642_create_lodgers.rb +9 -0
  85. data/test/dummy/db/schema.rb +31 -0
  86. data/test/dummy/lib/assets/.keep +0 -0
  87. data/test/dummy/log/.keep +0 -0
  88. data/test/dummy/public/404.html +67 -0
  89. data/test/dummy/public/422.html +67 -0
  90. data/test/dummy/public/500.html +66 -0
  91. data/test/dummy/public/favicon.ico +0 -0
  92. data/test/fixtures/lodgers.yml +14 -0
  93. data/test/fixtures/tenants.yml +18 -0
  94. data/test/integration/navigation_test.rb +41 -0
  95. data/test/lib/generators/sublease/install_generator_test.rb +25 -0
  96. data/test/models/sublease/lodger_test.rb +67 -0
  97. data/test/models/sublease/tenant_test.rb +25 -0
  98. data/test/sublease_test.rb +133 -0
  99. data/test/test_helper.rb +23 -0
  100. metadata +265 -0
@@ -0,0 +1,5 @@
1
+ module Sublease
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Sublease
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module Sublease
2
+ class ConfigError < StandardError
3
+ end
4
+
5
+ class TenantModelNotSet < StandardError
6
+ end
7
+
8
+ class TenantNotFound < StandardError
9
+ end
10
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sublease
4
+ module TenantSwitcher
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_action :sublease_switch_on_domain, if: :sublease_switch_on_domain?
9
+ before_action :sublease_switch_on_subdomain, if: :sublease_switch_on_subdomain?
10
+ before_action :sublease_switch_on_subdomain_and_domain, if: :sublease_switch_on_subdomain_and_domain?
11
+ end
12
+
13
+ private
14
+
15
+ def sublease_error_tenant_not_found(message)
16
+ raise Sublease::TenantNotFound, message
17
+ end
18
+
19
+ def sublease_model
20
+ model = Sublease.tenant_model
21
+ if model.class == String
22
+ model = model.capitalize.constantize
23
+ end
24
+ return model
25
+ end
26
+
27
+ def sublease_set_current_tenant(tenant)
28
+ Sublease.current_tenant = tenant
29
+ Sublease.current_tenant_domain = tenant.domain
30
+ Sublease.current_tenant_subdomain = tenant.subdomain
31
+ end
32
+
33
+ def sublease_switch_on_domain
34
+ return if Sublease.current_tenant_domain == request.domain
35
+ model = sublease_model
36
+ tenant = model.where(domain: request.domain).first
37
+ if tenant.nil?
38
+ sublease_error_tenant_not_found(I18n.t('sublease.errors.domain_not_found', domain: request.domain))
39
+ end
40
+ sublease_set_current_tenant(tenant)
41
+ end
42
+
43
+ def sublease_switch_on_domain?
44
+ (Sublease.switch_on_domain == true)
45
+ end
46
+
47
+ def sublease_switch_on_subdomain
48
+ return if Sublease.current_tenant_subdomain == request.subdomain
49
+ model = sublease_model
50
+ tenant = model.where(subdomain: request.subdomain).first
51
+ if tenant.nil?
52
+ sublease_error_tenant_not_found(I18n.t('sublease.errors.subdomain_not_found', subdomain: request.subdomain))
53
+ end
54
+ sublease_set_current_tenant(tenant)
55
+ end
56
+
57
+ def sublease_switch_on_subdomain?
58
+ (Sublease.switch_on_subdomain == true)
59
+ end
60
+
61
+ def sublease_switch_on_subdomain_and_domain
62
+ return if ((Sublease.current_tenant_subdomain == request.subdomain) && (Sublease.current_tenant_domain == request.domain))
63
+ model = sublease_model
64
+ tenant = model.where(domain: request.domain, subdomain: request.subdomain).first
65
+ if tenant.nil?
66
+ sublease_error_tenant_not_found(I18n.t('sublease.errors.subdomain_and_domain_not_found', subdomain: request.subdomain, domain: request.domain))
67
+ end
68
+ sublease_set_current_tenant(tenant)
69
+ end
70
+
71
+ def sublease_switch_on_subdomain_and_domain?
72
+ (Sublease.switch_on_subdomain_and_domain == true)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sublease
4
+ VERSION = '0.1.0'
5
+ end
data/sublease.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'sublease/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'sublease'
7
+ s.version = Sublease::VERSION
8
+ s.authors = ['bacrossland']
9
+ s.email = ['bacrossland@gmail.com']
10
+
11
+ s.summary = %q{Sublease is a tenanting engine for Rails apps using a shared database or schema.}
12
+ s.description = %q{Sublease is a tenanting engine for Rails 4.2 and greater applications using a shared database or schema.}
13
+ s.homepage = 'https://github.com/bacrossland/sublease'
14
+ s.license = 'MIT'
15
+
16
+ s.files = `git ls-files`.split($/)
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+
19
+ s.required_ruby_version = '>= 2.3.1'
20
+
21
+ s.add_dependency 'rails', '>= 4.1', '< 6.0'
22
+
23
+ s.add_runtime_dependency('request_store', '>= 1.4.0')
24
+
25
+ s.add_development_dependency 'sqlite3'
26
+ s.add_development_dependency 'shoulda'
27
+
28
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,7 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+
6
+ include Sublease::TenantSwitcher
7
+ end
File without changes
@@ -0,0 +1,7 @@
1
+ class LodgersController < ApplicationController
2
+ def index
3
+ @current_tenant_name = Sublease.current_tenant.name
4
+ @default_tenant_name = Sublease.default_tenant.name unless Sublease.default_tenant.nil?
5
+ @lodgers = Sublease.current_tenant.lodgers
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module LodgersHelper
2
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ class Lodger < Sublease::Lodger
2
+ belongs_to_sublease(:tenant)
3
+ end
@@ -0,0 +1,3 @@
1
+ class Tenant < Sublease::Tenant
2
+ has_many_subleases(:lodgers)
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,6 @@
1
+ Current Tenant: <span id="current_tenant_name"><%= @current_tenant_name %></span>
2
+ <br/>
3
+ Lodgers for Current Tenant:<br/>
4
+ <% @lodgers.each do |l| %>
5
+ <%= l.name %><br/>
6
+ <% end %>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "sublease"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+ end
22
+ end
23
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,23 @@
1
+ # SQLite version 3.x
2
+ # gem 'activerecord-jdbcsqlite3-adapter'
3
+ #
4
+ # Configure Using Gemfile
5
+ # gem 'activerecord-jdbcsqlite3-adapter'
6
+ #
7
+ default: &default
8
+ adapter: sqlite3
9
+
10
+ development:
11
+ <<: *default
12
+ database: db/development.sqlite3
13
+
14
+ # Warning: The database defined as "test" will be erased and
15
+ # re-generated from your development database when you run "rake".
16
+ # Do not set this db to the same as development or production.
17
+ test:
18
+ <<: *default
19
+ database: db/test.sqlite3
20
+
21
+ production:
22
+ <<: *default
23
+ database: db/production.sqlite3
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!
@@ -0,0 +1,37 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Do not eager load code on boot.
10
+ config.eager_load = false
11
+
12
+ # Show full error reports and disable caching.
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send.
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger.
20
+ config.active_support.deprecation = :log
21
+
22
+ # Raise an error on page load if there are pending migrations.
23
+ config.active_record.migration_error = :page_load
24
+
25
+ # Debug mode disables concatenation and preprocessing of assets.
26
+ # This option may cause significant delays in view rendering with a large
27
+ # number of complex assets.
28
+ config.assets.debug = true
29
+
30
+ # Adds additional error checking when serving assets at runtime.
31
+ # Checks for improperly declared sprockets dependencies.
32
+ # Raises helpful error messages.
33
+ config.assets.raise_runtime_errors = true
34
+
35
+ # Raises error for missing translations
36
+ # config.action_view.raise_on_missing_translations = true
37
+ end
@@ -0,0 +1,78 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # Code is not reloaded between requests.
5
+ config.cache_classes = true
6
+
7
+ # Eager load code on boot. This eager loads most of Rails and
8
+ # your application in memory, allowing both threaded web servers
9
+ # and those relying on copy on write to perform better.
10
+ # Rake tasks automatically ignore this option for performance.
11
+ config.eager_load = true
12
+
13
+ # Full error reports are disabled and caching is turned on.
14
+ config.consider_all_requests_local = false
15
+ config.action_controller.perform_caching = true
16
+
17
+ # Enable Rack::Cache to put a simple HTTP cache in front of your application
18
+ # Add `rack-cache` to your Gemfile before enabling this.
19
+ # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
20
+ # config.action_dispatch.rack_cache = true
21
+
22
+ # Disable Rails's static asset server (Apache or nginx will already do this).
23
+ config.serve_static_assets = false
24
+
25
+ # Compress JavaScripts and CSS.
26
+ config.assets.js_compressor = :uglifier
27
+ # config.assets.css_compressor = :sass
28
+
29
+ # Do not fallback to assets pipeline if a precompiled asset is missed.
30
+ config.assets.compile = false
31
+
32
+ # Generate digests for assets URLs.
33
+ config.assets.digest = true
34
+
35
+ # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
36
+
37
+ # Specifies the header that your server uses for sending files.
38
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
39
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
40
+
41
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
42
+ # config.force_ssl = true
43
+
44
+ # Set to :debug to see everything in the log.
45
+ config.log_level = :info
46
+
47
+ # Prepend all log lines with the following tags.
48
+ # config.log_tags = [ :subdomain, :uuid ]
49
+
50
+ # Use a different logger for distributed setups.
51
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
52
+
53
+ # Use a different cache store in production.
54
+ # config.cache_store = :mem_cache_store
55
+
56
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server.
57
+ # config.action_controller.asset_host = "http://assets.example.com"
58
+
59
+ # Ignore bad email addresses and do not raise email delivery errors.
60
+ # Set this to true and configure the email server for immediate delivery to raise delivery errors.
61
+ # config.action_mailer.raise_delivery_errors = false
62
+
63
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
64
+ # the I18n.default_locale when a translation cannot be found).
65
+ config.i18n.fallbacks = true
66
+
67
+ # Send deprecation notices to registered listeners.
68
+ config.active_support.deprecation = :notify
69
+
70
+ # Disable automatic flushing of the log to improve performance.
71
+ # config.autoflush_log = false
72
+
73
+ # Use default logging formatter so that PID and timestamp are not suppressed.
74
+ config.log_formatter = ::Logger::Formatter.new
75
+
76
+ # Do not dump schema after migrations.
77
+ config.active_record.dump_schema_after_migration = false
78
+ end