Sha256: c77e5abab1279319f3d6271eb6774e025f00c009159f4a9e16a0878260fb9902

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module ActsAsAFO

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    def acts_as_arter_flow_object
      has_one :arter_flow_object, :as => :afoable, :dependent => :destroy
      include InstanceMethods
    end

  end

  module InstanceMethods
    
    def after_create
      super
      create_arter_flow_objecjt
    end

    # lazy generating
    # 'cause before user run " script/generate acts_as_arter_flow_object", 
    # acts_as_arter_flow_object.rb has not been copied to app/models,
    # and this will caused a "uninitialized constant ActsAsAFO::InstanceMethods::ArterFlowObject" error.
    klazz = Kernel.const_get("ArterFlowObject") rescue nil
    unless klazz.nil?
      ArterFlowObject::STEPS.each do |s|
        module_eval <<-EOF
          def #{s}ed?
            !!arter_flow_object.send(:#{s}) rescue false
          end

          def toggle_#{s}!(usr)
            raise ActiveRecord::RecordNotFound.new("no corresponding arter flow object record found")  unless arter_flow_object
            arter_flow_object.toggle(:#{s})
            arter_flow_object.#{s}_updater = usr
            arter_flow_object.save
          end
        EOF
      end
    end 

    private

    def create_arter_flow_object
      arter_flow_object.create
    end
 
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_arter_flow_object-0.2.4 lib/afo/acts_as_afo.rb