Skip to content

Commit

Permalink
Update library and tests to work with Rails 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jlogsdon committed Feb 15, 2012
1 parent 1749b17 commit 7693272
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
16 changes: 12 additions & 4 deletions lib/acts_as_approvable/acts_as_approvable.rb
Expand Up @@ -71,7 +71,7 @@ def approvals_off
def without_approval(&block)
enable = self.approvals_active
approvals_off
class_eval &block
yield(self)
ensure
approvals_on if enable
end
Expand All @@ -95,7 +95,7 @@ def approval
# @return [String] one of `'pending'`, `'approved`' or `'rejected'`.
def approval_state
if self.class.approvable_field
read_attribute(self.class.approvable_field)
send(self.class.approvable_field)
else
approval.state
end
Expand All @@ -107,7 +107,7 @@ def approval_state
# @param [String] state one of `'pending'`, `'approved`' or `'rejected'`.
def set_approval_state(state)
return unless self.class.approvable_field
write_attribute(self.class.approvable_field, state)
send("#{self.class.approvable_field}=".to_sym, state)
end

##
Expand Down Expand Up @@ -269,11 +269,19 @@ def after_reject(approval); end
def without_approval(&block)
enable = approvals_on? # If we use #approvals_enabled? the global state might be incorrectly applied.
approvals_off
instance_eval &block
yield(self)
ensure
approvals_on if enable
end

def save_without_approval(*args)
without_approval { |i| save(*args) }
end

def save_without_approval!(*args)
without_approval { |i| save!(*args) }
end

private
def approvable_save
@approval.save if @approval.present? && @approval.new_record?
Expand Down
12 changes: 7 additions & 5 deletions lib/acts_as_approvable/approval.rb
Expand Up @@ -144,11 +144,12 @@ def approve!(force = false)
data[attr] = value if item.attribute_names.include?(attr)
end

item.without_approval { update_attributes!(data) }
elsif create? && item.approval_state.present?
item.without_approval { set_approval_state('approved'); save! }
item.attributes = data
elsif create?
item.set_approval_state('approved')
end

item.save_without_approval!
update_attributes!(:state => 'approved')
run_item_callback(:after_approve)
end
Expand All @@ -162,10 +163,11 @@ def reject!(reason = nil)
raise ActsAsApprovable::Error::Locked if locked?
return unless run_item_callback(:before_reject)

if create? && item.approval_state.present?
item.without_approval { set_approval_state('rejected'); save! }
if create?
item.set_approval_state('rejected')
end

item.save_without_approval!
update_attributes!(:state => 'rejected', :reason => reason)
run_item_callback(:after_reject)
end
Expand Down
18 changes: 12 additions & 6 deletions test/acts_as_approvable_model_test.rb
Expand Up @@ -46,15 +46,15 @@ def teardown

context 'that is altered using #without_approval' do
should 'not have an approval object' do
@project.without_approval { update_attribute(:description, 'updated') }
@project.without_approval { |i| i.update_attribute(:description, 'updated') }
assert @project.approvals.empty?
end

should 'correctly restore approval queue state' do
assert @project.approvals_on?
@project.approvals_off
assert !@project.approvals_on?
@project.without_approval { update_attribute(:description, 'updated') }
@project.without_approval { |i| i.update_attribute(:description, 'updated') }
assert !@project.approvals_on?
end
end
Expand Down Expand Up @@ -101,7 +101,7 @@ def teardown
end

context 'that is altered using #without_approval' do
setup { @game.without_approval { update_attribute(:title, 'updated') } }
setup { @game.without_approval { |i| i.update_attribute(:title, 'updated') } }

should 'not have an approval object' do
assert @game.approvals.empty?
Expand Down Expand Up @@ -284,7 +284,10 @@ def teardown
end

context 'when approved' do
setup { @user.approve!; @user.reload }
setup do
@user.approve!
@user.reload
end

should 'be approved' do
assert @user.approved?
Expand All @@ -298,7 +301,10 @@ def teardown
end

context 'when rejected' do
setup { @user.reject!; @user.reload }
setup do
@user.reject!
@user.reload
end

should 'be rejected' do
assert @user.rejected?
Expand All @@ -313,7 +319,7 @@ def teardown

context '.without_approval' do
should 'disable approvals for the given block' do
@user = User.without_approval { create }
@user = User.without_approval { |m| m.create }
assert @user.approval.nil?
end
end
Expand Down
8 changes: 3 additions & 5 deletions test/acts_as_approvable_ownership_test.rb
Expand Up @@ -4,9 +4,7 @@ class ActsAsApprovableOwnershipTest < Test::Unit::TestCase
load_schema

def teardown
ActiveRecord::Base.send(:subclasses).each do |klass|
klass.delete_all
end
truncate
end

context 'with default configuration' do
Expand Down Expand Up @@ -44,7 +42,7 @@ def self.available_owners
@employee = Employee.create
@approval = @employee.approval

@user1, @user2 = User.without_approval { [create, create] }
@user1, @user2 = User.without_approval { |m| [m.create, m.create] }
end

context '#assign' do
Expand Down Expand Up @@ -90,7 +88,7 @@ def self.available_owners

context 'with some assigned owners' do
setup do
@user3 = User.without_approval { create }
@user3 = User.without_approval { |m| m.create }

@approval.assign(@user1)
Employee.create.approval.assign(@user3)
Expand Down
9 changes: 7 additions & 2 deletions test/test_helper.rb
Expand Up @@ -47,9 +47,14 @@ def load_schema
ActiveRecord::Migration.suppress_messages do
load(File.dirname(__FILE__) + '/schema.rb')
end

ar_classes.each { |klass| klass.reset_column_information }
end

def ar_classes
ActiveRecord::Base.send(ActiveRecord::Base.respond_to?(:descendants) ? :descendants : :subclasses)
end

def truncate
klasses = ActiveRecord::Base.send(ActiveRecord::Base.respond_to?(:descendants) ? :descendants : :subclasses)
klasses.each { |klass| klass.delete_all }
ar_classes.each { |klass| klass.delete_all }
end

0 comments on commit 7693272

Please sign in to comment.