<commit_before>class ActiveRecord::Base
  def self.acts_as_tenant
    after_create do
      config = Rails.configuration.database_configuration
      password = config[Rails.env]["password"]
      ActiveRecord::Base.connection.execute "GRANT ALL PRIVILEGES ON *.* TO '#{self.name}'@'localhost' IDENTIFIED BY '#{password}' WITH GRANT OPTION;"
      ActiveRecord::Base.connection.execute "flush privileges;"
    end
  end

  def self.inherited(child)
    return unless FileTest.exist?(Multitenant::Mysql::ConfFile.full_path) # do nothing if no config file provided
    require Multitenant::Mysql::ConfFile.path

    model_name = child.to_s
    if Multitenant::Mysql.models.include? model_name
      view_name = model_name.to_s.downcase.pluralize + "_view"

      # check whether the view exists in db
      if ActiveRecord::Base.connection.table_exists? view_name
        child.class_eval do
          cattr_accessor :original_table_name

          self.original_table_name = self.table_name
          self.table_name = view_name
          self.primary_key = :id

          def self.new(*args)
            object = super(*args)
            object.id = nil # workaround for https://github.com/rails/rails/issues/5982
            object
          end
        end
      end
    end

    if Multitenant::Mysql.tenant == child
      child.send :acts_as_tenant
    end
  end
end<commit_msg>extracted code related to tenant depended model into a separate method for further reuse<commit_after>class ActiveRecord::Base
  def self.acts_as_tenants_bucket
    after_create do
      config = Rails.configuration.database_configuration
      password = config[Rails.env]["password"]
      ActiveRecord::Base.connection.execute "GRANT ALL PRIVILEGES ON *.* TO '#{self.name}'@'localhost' IDENTIFIED BY '#{password}' WITH GRANT OPTION;"
      ActiveRecord::Base.connection.execute "flush privileges;"
    end
  end

  def self.acts_as_tenant
    view_name = model_name.to_s.downcase.pluralize + "_view"
    # check whether the view exists in db
    if ActiveRecord::Base.connection.table_exists? view_name
      self.class_eval do
        cattr_accessor :original_table_name

        self.original_table_name = self.table_name
        self.table_name = view_name
        self.primary_key = :id

        def self.new(*args)
          object = super(*args)
          object.id = nil # workaround for https://github.com/rails/rails/issues/5982
          object
        end
      end
    end
  end

  def self.inherited(child)
    return unless FileTest.exist?(Multitenant::Mysql::ConfFile.full_path) # do nothing if no config file provided
    require Multitenant::Mysql::ConfFile.path

    model_name = child.to_s
    if Multitenant::Mysql.models.include? model_name
      child.send :acts_as_tenant
    end

    if Multitenant::Mysql.tenant == child
      child.send :acts_as_tenants_bucket
    end
  end
end
{'fraction_non_alphanumeric': 0.08549671166493597, 'fraction_numerical': 0.0027691242644513675, 'mean_word_length': 3.0083217753120666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3218958', 'n_tokens_mistral': 887, 'n_tokens_neox': 860, 'n_words': 201}