Skip to content

Commit

Permalink
Merge in Jan De Poorter's [DefV] named_scope addition to AR persisten…
Browse files Browse the repository at this point in the history
…ce layer, with some organizational clean up.
  • Loading branch information
Scott Barron committed May 30, 2008
1 parent 270d9e1 commit 83bbe4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -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
Expand Down
21 changes: 20 additions & 1 deletion lib/persistence/active_record_persistence.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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.