Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multi-database configuration via AR base model selection #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/rails_db/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ApplicationController < ActionController::Base
helper_method :per_page

before_action :verify_access
before_action :set_abstract_model_class

if RailsDb.http_basic_authentication_enabled
http_basic_authenticate_with name: RailsDb.http_basic_authentication_user_name,
Expand All @@ -21,6 +22,13 @@ def per_page
params[:per_page] || session[:per_page]
end

def set_abstract_model_class
return unless params[:abstract_model_class].present?

if (found = RailsDb.available_abstract_model_classes.find { |klass| klass.name == params[:abstract_model_class] })
RailsDb.abstract_model_class = found
end
end
end
end

19 changes: 19 additions & 0 deletions app/views/rails_db/shared/_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
<% if RailsDb.available_abstract_model_classes.many? %>
Do you want to see tables from a specific model base class?
<ul>
<% RailsDb.available_abstract_model_classes.each do |klass| %>
<li>
<% if klass == RailsDb.abstract_model_class %>
<strong>
<%= klass.name %>
</strong>
<% else %>
<%= link_to root_path(abstract_model_class: klass.name) do %>
<%= klass.name %>
<% end %>
<% end %>
</li>
<% end %>
</ul>
<% end %>

<h3>Tables</h3>

<input id='rails_db_tables_input' placeholder='... quick filter by table name'>
Expand Down
6 changes: 6 additions & 0 deletions lib/generators/templates/rails_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@

# # Sandbox mode (only read-only operations)
# config.sandbox = false

# # Configure to use something else than ActiveRecord::Base
# config.abstract_model_class = ApplicationRecord

# # Configure for multi-database environments to switch base models in the GUI
# config.available_abstract_model_classes = [ApplicationRecord, Tenant1::Base, Tenant2::Base]
end
end
10 changes: 10 additions & 0 deletions lib/rails_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ module RailsDb
mattr_accessor :verify_access_proc
@@verify_access_proc = proc { |controller| true }

## Switch out the ActiveRecord::Base class by any abstract descendant
mattr_accessor :abstract_model_class
@@abstract_model_class = ActiveRecord::Base

## Configure this in multi-database environments
mattr_accessor :available_abstract_model_classes
@@available_abstract_model_classes = [ActiveRecord::Base]

def self.setup
yield(self)
end
Expand All @@ -59,6 +67,8 @@ def self.use_default_configuration!
self.black_list_tables = self.white_list_tables = []
self.http_basic_authentication_enabled = false
self.verify_access_proc = proc { |controller| true }
self.abstract_model_class = ActiveRecord::Base
self.available_abstract_model_classes = [ActiveRecord::Base]
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_db/adapters/base_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class BaseAdapter

def self.execute_with_sandbox_if_needed
if RailsDb.sandbox
ActiveRecord::Base.transaction do
RailsDb.abstract_model_class.transaction do
yield
raise ActiveRecord::Rollback
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_db/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module RailsDb
module Connection

def connection
ActiveRecord::Base.connection
RailsDb.abstract_model_class.connection
rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(Rails.application.config.database_configuration[Rails.env]).connection
RailsDb.abstract_model_class.establish_connection(Rails.application.config.database_configuration[Rails.env]).connection
end

def columns
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_db/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def delete(id)

def create_model(table_name, &block)
begin
klass = Class.new(ActiveRecord::Base) do
klass = Class.new(RailsDb.abstract_model_class) do
def self.model_name
ActiveModel::Name.new(self, nil, table_name)
end
Expand All @@ -57,7 +57,7 @@ def self.model_name
end
klass.count # verify that it works, if not load other, hack
rescue
klass = ActiveRecord::Base.descendants.detect { |c| c.table_name == table_name }
klass = RailsDb.abstract_model_class.descendants.detect { |c| c.table_name == table_name }
end

add_ransack_methods(klass) if ransack_version >= Gem::Version.new('4.0.0')
Expand Down