Skip to content

Instantly share code, notes, and snippets.

@mpeychich
Created September 21, 2009 23:43
Show Gist options
  • Save mpeychich/190634 to your computer and use it in GitHub Desktop.
Save mpeychich/190634 to your computer and use it in GitHub Desktop.
module EnablerDisabler
def enable(method)
self.class.send(:class_variable_get, '@@enabler_status')[method.to_s] = true
end
def disable(method)
self.class.send(:class_variable_get, '@@enabler_status')[method.to_s] = false
end
def self.included(base)
base.class_eval do
@@enabler_status = {}
def self.enabled?(method)
@@enabler_status[method.to_s]
end
# only add enabler/disabler for instance methods we have defined on the model (excludes the ar methods or those added by plugins)
instance_methods.reject{|m| (ActiveRecord::Base.instance_methods + ['enable', 'disable']).include?(m)}.each do |m|
@@enabler_status.update(m => true)
define_method(m) do |*args|
super if self.class.enabled?(m)
end
end
end
end
end
class DummyCheckout < Checkout
include EnablerDisabler
# by default, disable these methods since they are automatically called in Checkout#initialize
[:check_user, :determine_state, :check_prior_purchase, :process_state, :extract_gift_options, :extract_payment_options].each do |m|
@@enabler_status[m.to_s] = false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment