Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on workflow 0.8.0, ree and rails 2.3.11, I added a new "first state", but the code in on_entry isn't being executed #32

Closed
chewmanfoo opened this issue May 23, 2011 · 2 comments

Comments

@chewmanfoo
Copy link

I have the following workflow configuration:

# scheduled_change.rb
  workflow do
                    # "The initial state of a workflow is the first state defined"
    state :planning do
      event :advance, :transitions_to => :planning_approved
      event :skip_to_complete, :transitions_to => :completed

      on_entry do
                   # send a "scheduled_change has been created" email
        @subject = "A new Scheduled Change has been created. [not yet started]."
        @subject += " '" + name + "'"
        @subject += " planned on " + target_date.strftime('%m/%d/%Y')
        @subject += " at " + target_time + " "  + time_zone.name
        @subject += ". Expected duration: " + duration.to_s + " hours."
        @recipient = creator.email
        send_email(@subject, @recipient, "created")
                     # send a "scheduled_change needs your approval" email
        @subject = "A new Scheduled Change has been created NEEDING YOUR APPROVAL."
        @subject += " '" + name + "'"
        @subject += " planned on " + target_date.strftime('%m/%d/%Y')
        @subject += " at " + target_time + " "  + time_zone.name
        @subject += ". Expected duration: " + duration.to_s + " hours."
        @recipient = carrier.advocates.size > 0 ? carrier.advocates.map(&:email) : creator.email
        logger.info "Sending NEEDING YOUR APPROVAL to #{@recipient.inspect}"
        send_email(@subject, @recipient, "requesting_approval")
      end
    end

    state :planning_approved do
      event :advance, :transitions_to => :running

      on_entry do
                         # send a "scheduled_change approved" email
        @subject = "A new Scheduled Change has been approved."
        @subject += " '" + name + "'"
        @subject += " planned on " + target_date.strftime('%m/%d/%Y')
        @subject += " at " + target_time + " "  + time_zone.name
        @subject += ". Expected duration: " + duration.to_s + " hours."
        @recipient = carrier.advocates.size > 0 ? carrier.advocates.map(&:email) << creator.email : creator.email
        send_email(@subject, @recipient, "created")
      end
    end

    state :running do
      event :advance, :transitions_to => :completed

      on_entry do
                      # send a "scheduled_change has started" email
        @recipient = SCHEDULED_CHANGE_BROADCAST_ADDRESS_EOS
        @subject = "Scheduled Change #{name} has started"
        @subject += ". Expected duration: " + duration.to_s + " hours."
        send_email(@subject, @recipient, "status")
      end
    end

    state :completed do
      event :reset, :transitions_to => :planning

      on_entry do
                 # send a "scheduled_change has completed" email
        @recipient = SCHEDULED_CHANGE_BROADCAST_ADDRESS_EOS
        @subject = "Scheduled Change #{name} has completed"
        if scheduled_change_outcome
          @subject += ". Result: " + scheduled_change_outcome.name.upcase + "."
        end
        status="completed"
        send_email(@subject, @recipient, "status")
      end
    end

  end

  def send_email(_sub, _to, _type)
    case _type
      when "created"
        ScheduledChangeMailer::deliver_scheduled_change_created_mail(_to, _sub, id)
      when "requesting_approval"
        ScheduledChangeMailer::deliver_scheduled_change_requesting_approval_mail(_to, _sub, id)
      when "status"
        ScheduledChangeMailer::deliver_scheduled_change_state_mail(_to, _sub, id)
    end
  end

But the code in :planning on_entry is no being executed. Can you please look over and see if you can tell me why?

Thanks! I appreciate your plugin!

Jason

@geekq
Copy link
Owner

geekq commented Jul 14, 2011

Hi,

do I understand you correctly, that you expect the 'on_entry' for the first state
to be executed on the object creation? This is not the designed behaviour.
on_entry is only invoked on transitions for existing objects.

The example code is pretty long. If you still have any questions please
provide the complete minimal example in form of a test
showing your expectations. Then just reopen the issue.

Best Regards,

Vladimir

@geekq geekq closed this as completed Jul 14, 2011
@chewmanfoo
Copy link
Author

In order to resolve this, I created a new "pre-state" which always automatically transitions to my first state (:planning above). That way the :planning state can fire off code when workflow enters that state.

I would think this would be the desired behavior. If you map workflow to a stoplight with three states :red, :yellow, :green, then your stoplight never lights up the the first light on the first go-round (but if the workflow loops back to the first state, then that state lights up on ever subsequent state change.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants