Module: Transactify::ClassMethods

Defined in:
lib/transactify.rb

Instance Method Summary collapse

Instance Method Details

#ctransactify(*cmethods) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/transactify.rb', line 16

def ctransactify(*cmethods)
  interceptor = const_get("#{name.demodulize}Interceptor")
  klass = const_get(name)
  helper = const_set("Transactify#{SecureRandom.hex}Helper", Module.new)
  cmethods.each do |method_name|
    interceptor.module_eval do
      helper.send :define_singleton_method, :prepended do |base|
        define_method(method_name) do |*args, &block|
          ActiveRecord::Base.transaction do
            super(*args, &block)
          end
        end
      end
      (class << klass; self; end).module_eval do
        prepend(helper)
      end
    end
  end
end

#transactify(*cmethods) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/transactify.rb', line 36

def transactify(*cmethods)
  interceptor = const_get("#{name.demodulize}Interceptor")
  cmethods.each do |method_name|
    interceptor.module_eval do
      define_method(method_name) do |*args, &block|
        ActiveRecord::Base.transaction do
          super(*args, &block)
        end
      end
    end
  end
end