From 9e348d718bdcc87d2560b39f820d49dbd93e69a1 Mon Sep 17 00:00:00 2001 From: Scott Barron Date: Thu, 29 May 2008 13:10:31 -0700 Subject: [PATCH] Add .find_in_state, .count_in_state, .calculate_in_state back to AR layer --- TODO | 1 - lib/persistence/active_record_persistence.rb | 24 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index 0b146c4..d016188 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ Before Next Release: * Add #aasm_next_state_for_event * Add #aasm_next_states_for_event - Cool ideas from users: * Support multiple state machines on one object (Chris Nelson) diff --git a/lib/persistence/active_record_persistence.rb b/lib/persistence/active_record_persistence.rb index 9488143..a44b237 100644 --- a/lib/persistence/active_record_persistence.rb +++ b/lib/persistence/active_record_persistence.rb @@ -74,6 +74,30 @@ module AASM @aasm_column end + def find_in_state(number, state, *args) + with_state_scope state do + find(number, *args) + end + end + + def count_in_state(state, *args) + with_state_scope state do + count(*args) + end + end + + def calculate_in_state(state, *args) + with_state_scope state do + calculate(*args) + end + end + + protected + def with_state_scope(state) + with_scope :find => {:conditions => ["#{table_name}.#{aasm_column} = ?", state.to_s]} do + yield if block_given? + end + end end module InstanceMethods