Skip to content
Browse files

directory structure

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@306 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent 0355037 commit 74f68a2f1c6b2d6679cd93b4ad4a3dba13acc2df dcroak committed
Showing with 99 additions and 2 deletions.
  1. +1 −1 init.rb
  2. +52 −0 lib/callbacks.rb
  3. 0 lib/filters.rb
  4. +40 −0 lib/validations.rb
  5. +6 −1 lib/when.rb
View
2 init.rb
@@ -1 +1 @@
-# Include hook code here
+require 'when'
View
52 lib/callbacks.rb
@@ -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
View
0 lib/filters.rb
No changes.
View
40 lib/validations.rb
@@ -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
View
7 lib/when.rb
@@ -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.
Something went wrong with that request. Please try again.