Module: CopyMachine::ActiveRecordExtensions::Base::ClassMethods

Defined in:
lib/copy_machine/active_record_extensions/base.rb

Instance Method Summary collapse

Instance Method Details

#find_by_sql_with_override(sql) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/copy_machine/active_record_extensions/base.rb', line 24

def find_by_sql_with_override(sql)
  records = find_by_sql_without_override(sql)
  
  if records.empty? and !@avoid_copy
    backtrace = Rails.backtrace_cleaner.clean(caller).first
    backtrace = backtrace.split(':')[0..1].join(":") unless backtrace.nil?

    copy_mode(sql, backtrace) do
      unless (proc = CopyMachine.config.sql_alteration).nil?
        sql = proc.call(sql)
      end
      records = find_by_sql_without_override(sql)
    end

    records.each { |r| cp r }
  end

  records
end

#with_alternate_connection(sql, backtrace) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/copy_machine/active_record_extensions/base.rb', line 14

def with_alternate_connection(sql, backtrace)
  ActiveSupport::Notifications.instrument("copy_machine.sql",
    :sql => sql, :name => self.name, :backtrace => backtrace) do

    ActiveRecord::Base.establish_connection :copy_machine
    yield
    ActiveRecord::Base.establish_connection Rails.env
  end
end