This repository has been archived by the owner on Nov 11, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1 @@ | |||
# Include hook code here | require 'when' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |