Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
directory structure
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@306 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
dcroak committed Feb 12, 2008
1 parent 0355037 commit 74f68a2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion init.rb
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
# Include hook code here require 'when'
52 changes: 52 additions & 0 deletions lib/callbacks.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,52 @@
module ActiveRecordHook
module Callbacks

def self.included(klass)
class << klass

callbacks = ActiveRecord::Callbacks::CALLBACKS

callbacks.each do |callback|
src = <<-END;
def #{callback}_with_conditions (*callbacks, &block)
options = callbacks.extract_options!
if block_given?
callbacks << block
end
callbacks.each do |callback|
#{callback}_without_conditions do |record|
unless (! options[:if].nil? && ! evaluate_condition(options[:if], record)) ||
(! options[:unless].nil? && evaluate_condition(options[:unless], record))
execute_callback callback, record, :#{callback}
end
end
end
end
alias_method_chain :#{callback}, :conditions
END
class_eval src, __FILE__, __LINE__
end

def execute_callback(callback, record, method)
if callback.class == Symbol
record.send callback
# elsif callback.class == String
# eval callback, record.send(:binding)
# elsif callback.class == Proc ||
# callback.class == Method
# callback.call record
# else
# if callback.respond_to?(method)
# callback.send method, record
# else
# raise ActiveRecordError,
# 'Callbacks must be a symbol denoting the message to send, a string to be evaluated, a block to be invoked or a class object responding to the callback message (#before_create, #after_create, etc).'
# end
end
end

end
end

end
end
Empty file added lib/filters.rb
Empty file.
40 changes: 40 additions & 0 deletions lib/validations.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,40 @@
module ActiveRecordHook
module Validations

def self.included(klass)
class << klass

validations = ActiveRecord::Validations::VALIDATIONS

validations.each do |validation|
src = <<-END;
def #{validation}_with_conditions (*validations, &block)
options = validations.extract_options!
if block_given?
validations << block
end
validations.each do |validation|
#{validation}_without_conditions do |record|
unless (! options[:if].nil? && ! evaluate_condition(options[:if], record)) ||
(! options[:unless].nil? && evaluate_condition(options[:unless], record))
execute_validation validation, record, :#{validation}
end
end
end
end
alias_method_chain :#{validation}, :conditions
END
class_eval src, __FILE__, __LINE__
end

def execute_validation(validation, record, method)
if validation.class == Symbol
record.send validation
end
end

end
end

end
end
7 changes: 6 additions & 1 deletion lib/when.rb
Original file line number Original file line Diff line number Diff line change
@@ -1 +1,6 @@
# When require 'validations'
require 'filters'
require 'callbacks'

ActiveRecord::Base.send :include, ActiveRecordHook::Callbacks
ActiveRecord::Base.send :include, ActiveRecordHook::Validations

0 comments on commit 74f68a2

Please sign in to comment.