Skip to content

Instantly share code, notes, and snippets.

@pechorin
Created April 24, 2012 15:26
Show Gist options
  • Save pechorin/2480593 to your computer and use it in GitHub Desktop.
Save pechorin/2480593 to your computer and use it in GitHub Desktop.
опа
# Concern
module SkipSpecificCallback
extend ActiveSupport::Concern
module ClassMethods
def with_skip(callback_name, *args)
options = args.extract_options!
raise Exception, ":on not specified" if options[:on].blank?
attribute_name = "skip_#{callback_name}"
send(:class_attribute, attribute_name)
send options[:on] do |record|
unless record.send(attribute_name)
record.send(callback_name)
end
end
end
end
end
# class
class A < ActiveRecord::Base
include SkipSpecificCallback
with_skip :run_alert, :on => :after_create
private
def run_alert
puts "alert"
end
end
# runtime
a1 = A.new
a1.skip_run_alert = true
a1.create # => nil
a2 = A.new
a2.skip_run_alert = false
a2.create # => "alert"
# можно хуйнуть прямо для всех объектов ;)
A.skip_run_alert = true
a3 = A.new
a3.create # => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment