Skip to content

Commit

Permalink
Added before_validation callback
Browse files Browse the repository at this point in the history
  • Loading branch information
francois committed Jan 15, 2009
1 parent 7916fef commit 47c6a5b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
18 changes: 10 additions & 8 deletions lib/active_presenter/base.rb
Expand Up @@ -3,7 +3,7 @@ module ActivePresenter
#
class Base
include ActiveSupport::Callbacks
define_callbacks :before_save, :after_save
define_callbacks :before_validation, :before_save, :after_save

class_inheritable_accessor :presented
self.presented = {}
Expand Down Expand Up @@ -87,13 +87,15 @@ def errors
# Returns boolean based on the validity of the presentables by calling valid? on each of them.
#
def valid?
presented.keys.each do |type|
presented_inst = send(type)

merge_errors(presented_inst, type) unless presented_inst.valid?
if run_callbacks_with_halt(:before_validation)
presented.keys.each do |type|
presented_inst = send(type)

merge_errors(presented_inst, type) unless presented_inst.valid?
end

errors.empty?
end

errors.empty?
end

# Save all of the presentables, wrapped in a transaction.
Expand All @@ -120,10 +122,10 @@ def save
# Returns true on success, will raise otherwise.
#
def save!
raise ActiveRecord::RecordInvalid.new(self) unless valid?
raise ActiveRecord::RecordNotSaved unless run_callbacks_with_halt(:before_save)

ActiveRecord::Base.transaction do
valid? # collect errors before potential exception raise
presented_instances.each { |i| i.save! }
end

Expand Down
12 changes: 9 additions & 3 deletions test/base_test.rb
Expand Up @@ -83,18 +83,18 @@
end

expect ActiveRecord::Base.to.receive(:transaction) do
s = SignupPresenter.new
s = SignupPresenter.new(:user_login => "da", :user_password => "seekrit")
s.save!
end

expect User.any_instance.to.receive(:save!) do
s = SignupPresenter.new
s = SignupPresenter.new(:user_login => "da", :user_password => "seekrit")
s.save!
end

expect Account.any_instance.to.receive(:save!) do
User.any_instance.stubs(:save!)
s = SignupPresenter.new
s = SignupPresenter.new(:user_login => "da", :user_password => "seekrit")
s.save!
end

Expand Down Expand Up @@ -177,4 +177,10 @@

expect SamePrefixPresenter.new.to.be.respond_to?(:account_title)
expect SamePrefixPresenter.new.to.be.respond_to?(:account_info_info)

expect [:before_validation, :before_save, :after_save] do
returning(CallbackOrderingPresenter.new) do |presenter|
presenter.save!
end.steps
end
end
27 changes: 27 additions & 0 deletions test/test_helper.rb
Expand Up @@ -68,6 +68,33 @@ class SamePrefixPresenter < ActivePresenter::Base
presents :account, :account_info
end

class CallbackOrderingPresenter < ActivePresenter::Base
presents :account

before_validation :do_before_validation
before_save :do_before_save
after_save :do_after_save

attr_reader :steps

def initialize(params={})
super
@steps = []
end

def do_before_validation
@steps << :before_validation
end

def do_before_save
@steps << :before_save
end

def do_after_save
@steps << :after_save
end
end

def hash_for_user(opts = {})
{:login => 'jane', :password => 'seekrit' }.merge(opts)
end
Expand Down

0 comments on commit 47c6a5b

Please sign in to comment.