Module: DbLogTag

Extended by:
ActiveSupport::Concern
Defined in:
lib/db_log_tag.rb,
lib/db_log_tag/colors.rb,
lib/db_log_tag/enable.rb,
lib/db_log_tag/refinement.rb,
lib/db_log_tag/multiple_db.rb,
lib/db_log_tag/configuration.rb,
lib/rails/generators/db_log_tag/install_generator.rb

Defined Under Namespace

Modules: Colors, MultipleDb Classes: Configuration, InstallGenerator

Class Method Summary collapse

Class Method Details

.config {|configuration| ... } ⇒ Object

Yields:



21
22
23
24
# File 'lib/db_log_tag.rb', line 21

def config
  configuration.reset
  yield(configuration)
end

.configurationObject



17
18
19
# File 'lib/db_log_tag.rb', line 17

def configuration
  @configuration ||= DbLogTag::Configuration.new
end

.enable?Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/db_log_tag/enable.rb', line 8

def enable?
    @@envs ||= ["development", "test"]
    @@envs.include?(ENV["RAILS_ENV"])
end

.refinement_tag(refinement_proc, **options) ⇒ Object

helper method to create refinement module that add refinement tag to all queries be called on refinement’s scope

ex: class PersonJob

using DbLogTag.refinement_tag { |db, shard, role| "PersonJob" }

now all queries in this job will be automatically prepend refinement tag “PersonJob”



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/db_log_tag/refinement.rb', line 14

def self.refinement_tag(refinement_proc, **options)
  return Module.new {} unless DbLogTag.enable?
  
  # init an anonymous module
  # that will be `using`
  Module.new do
    refine ActiveRecord::Base.singleton_class do
      # decorating all querying methods
      # to add log_tag before call the origin query method
      (ActiveRecord::Querying::QUERYING_METHODS + [:all]).each do |q|
        define_method(q) do |*args, &block|
          log_tag(**options) do |db, shard, role| 
            refinement_proc.call(db, shard, role)
          end.send(q, *args, &block)
        end
      end
    end
  end
end

.set_enable_environment(envs) ⇒ Object



4
5
6
# File 'lib/db_log_tag/enable.rb', line 4

def set_enable_environment(envs)
    @@envs = envs.map(&:to_s)
end