Permalink
Browse files

Merge in Jan De Poorter's [DefV] named_scope addition to AR persisten…

…ce layer, with some organizational clean up.
  • Loading branch information...
1 parent 270d9e1 commit 83bbe4d082b40d7fb7334c7fc5efd3bfe9d64ffb Scott Barron committed May 30, 2008
Showing with 24 additions and 1 deletion.
  1. +4 −0 CHANGELOG
  2. +20 −1 lib/persistence/active_record_persistence.rb
View
@@ -1,5 +1,9 @@
+* Use named_scope in AR persistence layer, if available [Jan De Poorter]
+
* Incremented version number
+
* Cleaned up aasm_states_for_select to return the value as a string
+
* Specs and bug fixes for the ActiveRecordPersistence, keeping persistence columns in sync
Allowing for nil values in states for active record
Only set state to default state before_validation_on_create
@@ -36,7 +36,19 @@ def self.included(base)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
- base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
+ base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
+
+ if base.respond_to?(:named_scope)
+ base.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods)
+
+ base.class_eval do
+ class << self
+ alias_method :aasm_state_without_named_scope, :aasm_state
+ alias_method :aasm_state, :aasm_state_with_named_scope
+ end
+ end
+ end
+
base.before_validation_on_create :aasm_ensure_initial_state
end
@@ -211,6 +223,13 @@ def aasm_read_state
end
end
end
+
+ module NamedScopeMethods
+ def aasm_state_with_named_scope name, options = {}
+ aasm_state_without_named_scope name, options
+ self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.scopes.include?(name)
+ end
+ end
end
end
end

0 comments on commit 83bbe4d

Please sign in to comment.