lib/workflow.rb
# this method should be split up into Workflow::Integrator
# utility sub-classes, with an ActiveRecordIntegrator and a
# ClassIntegrator... that both respond to integrate! :)
this method should be split up into Workflow::Integrator
utility sub-classes, with an ActiveRecordIntegrator and a
ClassIntegrator... that both respond to integrate.
this method should be split up into Workflow :: Integrator utility sub - classes with an ActiveRecordIntegrator and a ClassIntegrator ... that both respond to integrate .
def append_features(receiver)
      receiver.instance_eval do
        def workflow(&specification)
          @workflow = Workflow.specify(self, &specification)
        end
        def workflow_states
          @workflow.states.collect(&:name)
        end
      end
      if receiver.superclass.to_s == 'ActiveRecord::Base'
        receiver.class_eval do
          alias_method :initialize_before_workflow, :initialize
          attr_accessor :workflow
          def initialize(attributes = nil)
            initialize_before_workflow(attributes)
            @workflow = Workflow.new(self.class)
            @workflow.bind_to(self)
          end
          def after_find
            @workflow = if workflow_state.nil?
              Workflow.new(self.class)
            else
              name = self.class.send("class_name_of_active_record_descendant", self.class)
              Workflow.reconstitute(workflow_state.to_sym, name.constantize)
            end
            @workflow.bind_to(self)
          end
          alias_method :before_save_before_workflow, :before_save
          def before_save
            before_save_before_workflow
            self.workflow_state = @workflow.state.to_s
          end
        end
      else
        receiver.class_eval do
          alias_method :initialize_before_workflow, :initialize
          attr_reader :workflow
          def initialize(*args, &block)
            initialize_before_workflow(*args, &block)
            @workflow = Workflow.new(self.class)
            @workflow.bind_to(self)
          end
        end
      end
    end
def append_features ( receiver ) receiver . instance_eval do def workflow ( & specification ) @workflow = Workflow . specify ( self , & specification ) end def workflow_states @workflow . states . collect ( & :name ) end end if receiver . superclass . to_s == 'ActiveRecord::Base' receiver . class_eval do alias_method :initialize_before_workflow , :initialize attr_accessor :workflow def initialize ( attributes = nil ) initialize_before_workflow ( attributes ) @workflow = Workflow . new ( self . class ) @workflow . bind_to ( self ) end def after_find @workflow = if workflow_state . nil? Workflow . new ( self . class ) else name = self . class . send ( "class_name_of_active_record_descendant" , self . class ) Workflow . reconstitute ( workflow_state . to_sym , name . constantize ) end @workflow . bind_to ( self ) end alias_method :before_save_before_workflow , :before_save def before_save before_save_before_workflow self . workflow_state = @workflow . state . to_s end end else receiver . class_eval do alias_method :initialize_before_workflow , :initialize attr_reader :workflow def initialize ( * args , & block ) initialize_before_workflow ( * args , & block ) @workflow = Workflow . new ( self . class ) @workflow . bind_to ( self ) end end end end
this method should be split up into Workflow::Integrator
utility sub-classes, with an ActiveRecordIntegrator and a
ClassIntegrator... that both respond to integrate!
this method should be split up into Workflow :: Integrator utility sub - classes with an ActiveRecordIntegrator and a ClassIntegrator ... that both respond to integrate!
# add a meta method # this should check the inheritance tree, as subclassed models # won't return ActiveRecord::Base, they'll return something else # despite being inherited from AR::Base... # active record gets this style of integration # anything else gets this style of integration
receiver