<commit_before>require 'refinery/multisites/configuration'

module RefineryMultisitesActionControllerBaseDecoration
  extend ActiveSupport::Concern

  included do
    protect_from_forgery with: :reset_session
  end

  def self.prepended(base)
    base.prepend_before_action :load_schema, unless: -> { Refinery::Multisites.single_tenant_mode? }
  end

  private

  def self.included(base)
    base.helper_method :current_subdomain, :current_user_owner?
  end

  def current_subdomain
    @current_subdomain ||=
      current_account.subdomain unless Refinery::Multisites.single_tenant_mode?
  end

  def current_user_owner?
    current_account.owner == current_refinery_user unless Refinery::Multisites.single_tenant_mode?
  end

  def current_account
    @current_account ||= Refinery::Multisites::Account.find_by(subdomain: request.subdomain)
  end

  def load_schema
    if request.subdomain.present?
      if current_account
        Apartment::Tenant.switch!(request.subdomain)
      else
        redirect_to root_url(subdomain: false)
      end
    else
      Apartment::Tenant.switch!("public")
    end
  end
end

ActionController::Base.send :prepend, RefineryMultisitesActionControllerBaseDecoration<commit_msg>Tidy up action_controller_base_decorator<commit_after>require 'refinery/multisites/configuration'

module RefineryMultisitesActionControllerBaseDecorator
  extend ActiveSupport::Concern

  included do
    protect_from_forgery with: :reset_session
  end

  def self.prepended(base)
    base.prepend_before_action :load_schema, unless: -> { Refinery::Multisites.single_tenant_mode? }
  end

  private

  def self.included(base)
    base.helper_method :current_subdomain, :current_user_owner?
  end

  def current_subdomain
    @current_subdomain ||=
      current_account.subdomain unless Refinery::Multisites.single_tenant_mode?
  end

  def current_user_owner?
    current_account.owner == current_refinery_user unless Refinery::Multisites.single_tenant_mode?
  end

  def current_account
    @current_account ||= Refinery::Multisites::Account.find_by(subdomain: request.subdomain)
  end

  def load_schema
    if request.subdomain.present?
      if current_account
        Apartment::Tenant.switch!(request.subdomain)
      else
        redirect_to root_url(subdomain: false)
      end
    else
      Apartment::Tenant.switch!("public")
    end
  end
end

ActionController::Base.send :prepend, RefineryMultisitesActionControllerBaseDecorator
{'fraction_non_alphanumeric': 0.07320048800325335, 'fraction_numerical': 0.0, 'mean_word_length': 4.681293302540416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368467', 'n_tokens_mistral': 761, 'n_tokens_neox': 732, 'n_words': 133}