Skip to content

Commit

Permalink
keep it dry
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Nov 5, 2012
1 parent f2eeb2e commit c466d79
Showing 4 changed files with 49 additions and 89 deletions.
1 change: 1 addition & 0 deletions lib/aasm/persistence.rb
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ def self.set_persistence(base)
# Use a fancier auto-loading thingy, perhaps. When there are more persistence engines.
hierarchy = base.ancestors.map {|klass| klass.to_s}

require File.join(File.dirname(__FILE__), 'persistence', 'base')
require File.join(File.dirname(__FILE__), 'persistence', 'read_state')
if hierarchy.include?("ActiveRecord::Base")
require File.join(File.dirname(__FILE__), 'persistence', 'active_record_persistence')
36 changes: 1 addition & 35 deletions lib/aasm/persistence/active_record_persistence.rb
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ module ActiveRecordPersistence
# end
#
def self.included(base)
base.extend AASM::Persistence::Base::ClassMethods
base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
base.send(:include, AASM::Persistence::ReadState) unless base.method_defined?(:aasm_read_state)
@@ -46,41 +47,6 @@ def self.included(base)
end

module ClassMethods
# Maps to the aasm_column in the database. Defaults to "aasm_state". You can write:
#
# create_table :foos do |t|
# t.string :name
# t.string :aasm_state
# end
#
# class Foo < ActiveRecord::Base
# include AASM
# end
#
# OR:
#
# create_table :foos do |t|
# t.string :name
# t.string :status
# end
#
# class Foo < ActiveRecord::Base
# include AASM
# aasm_column :status
# end
#
# This method is both a getter and a setter
def aasm_column(column_name=nil)
if column_name
AASM::StateMachine[self].config.column = column_name.to_sym
# @aasm_column = column_name.to_sym
else
AASM::StateMachine[self].config.column ||= :aasm_state
# @aasm_column ||= :aasm_state
end
# @aasm_column
AASM::StateMachine[self].config.column
end

def find_in_state(number, state, *args)
with_state_scope state do
46 changes: 46 additions & 0 deletions lib/aasm/persistence/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module AASM
module Persistence
module Base

module ClassMethods
# Maps to the aasm_column in the database. Defaults to "aasm_state". You can write
# (example provided here for ActiveRecord, but it's true for Mongoid as well):
#
# create_table :foos do |t|
# t.string :name
# t.string :aasm_state
# end
#
# class Foo < ActiveRecord::Base
# include AASM
# end
#
# OR:
#
# create_table :foos do |t|
# t.string :name
# t.string :status
# end
#
# class Foo < ActiveRecord::Base
# include AASM
# aasm_column :status
# end
#
# This method is both a getter and a setter
def aasm_column(column_name=nil)
if column_name
AASM::StateMachine[self].config.column = column_name.to_sym
# @aasm_column = column_name.to_sym
else
AASM::StateMachine[self].config.column ||= :aasm_state
# @aasm_column ||= :aasm_state
end
# @aasm_column
AASM::StateMachine[self].config.column
end
end

end # Base
end # Persistence
end # AASM
55 changes: 1 addition & 54 deletions lib/aasm/persistence/mongoid_persistence.rb
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ module MongoidPersistence
# end
#
def self.included(base)
base.extend AASM::Persistence::Base::ClassMethods
base.extend AASM::Persistence::MongoidPersistence::ClassMethods
base.send(:include, AASM::Persistence::MongoidPersistence::InstanceMethods)
base.send(:include, AASM::Persistence::ReadState) unless base.method_defined?(:aasm_read_state)
@@ -59,60 +60,6 @@ def self.included(base)
end

module ClassMethods
# Maps to the aasm_column in the database. Deafults to "aasm_state". You can write:
#
# class Foo
# include Mongoid::Document
# include AASM
# field :aasm_state
# end
#
# OR:
#
# class Foo
# include Mongoid::Document
# include AASM
# field :status
# aasm_column :status
# end
#
# This method is both a getter and a setter
def aasm_column(column_name=nil)
if column_name
AASM::StateMachine[self].config.column = column_name.to_sym
# @aasm_column = column_name.to_sym
else
AASM::StateMachine[self].config.column ||= :aasm_state
# @aasm_column ||= :aasm_state
end
# @aasm_column
AASM::StateMachine[self].config.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

0 comments on commit c466d79

Please sign in to comment.