lib/switch_point/model.rb in switch_point-0.7.0 vs lib/switch_point/model.rb in switch_point-0.8.0

- old
+ new

@@ -2,43 +2,30 @@ require 'switch_point/proxy_repository' module SwitchPoint module Model def self.included(model) + super model.singleton_class.class_eval do include ClassMethods - alias_method_chain :connection, :switch_point - alias_method_chain :cache, :switch_point - alias_method_chain :uncached, :switch_point + prepend MonkeyPatch end end - module ClassMethods - def connection_with_switch_point - if switch_point_proxy - switch_point_proxy.connection - else - connection_without_switch_point - end - end + def with_readonly(&block) + self.class.with_readonly(&block) + end - def cache_with_switch_point(&block) - if switch_point_proxy - switch_point_proxy.cache(&block) - else - cache_without_switch_point(&block) - end - end + def with_writable(&block) + self.class.with_writable(&block) + end - def uncached_with_switch_point(&block) - if switch_point_proxy - switch_point_proxy.uncached(&block) - else - uncached_without_switch_point(&block) - end - end + def transaction_with(*models, &block) + self.class.transaction_with(*models, &block) + end + module ClassMethods def with_readonly(&block) if switch_point_proxy switch_point_proxy.with_readonly(&block) else raise UnconfiguredError.new("#{name} isn't configured to use switch_point") @@ -57,11 +44,11 @@ assert_existing_switch_point!(name) @switch_point_name = name end def switch_point_proxy - if @switch_point_name + if defined?(@switch_point_name) ProxyRepository.checkout(@switch_point_name) elsif self == ActiveRecord::Base nil else superclass.switch_point_proxy @@ -93,9 +80,35 @@ ) end end writable_switch_points.uniq.size == 1 + end + end + + module MonkeyPatch + def connection + if switch_point_proxy + switch_point_proxy.connection + else + super + end + end + + def cache(&block) + if switch_point_proxy + switch_point_proxy.cache(&block) + else + super + end + end + + def uncached(&block) + if switch_point_proxy + switch_point_proxy.uncached(&block) + else + super + end end end end end