Git Product home page Git Product logo

Comments (13)

soveran avatar soveran commented on May 24, 2024

Hello @bbnnt, it's fine to ask here but it would be good to have something other than "issues" for this kind of discussions!

Anyway, I have to ask you something: do you always call save after calling confirm!? If that's the case, you can put that logic in the on(:confirmed) block and revert the state if necessary. Here's an example that you can adapt to your specific case:

fsm.on(:confirmed) do
  if save
    send_email
  else
    fsm.trigger(:cancel)
  end
end

from micromachine.

benbonnet avatar benbonnet commented on May 24, 2024

Well, I was thinking again about it; felt a bit dumb to have asked as this solution would seem obvious… without being sure though. Thanks a lot for confirming ! And for this gem, tiny and very clean

Best

from micromachine.

soveran avatar soveran commented on May 24, 2024

Thank you! And in the future, feel free to open issues for questions like this if you can't afford IRC or emails. By the way, my email address is listed on my profile, so you can contact me anytime. Good luck!

from micromachine.

benbonnet avatar benbonnet commented on May 24, 2024

Almost there… I've set it all; weirdly if save does check the validations, but the change does not actually make it to the db (the model's column value does not change)

fsm.on("confirmed") do
   if save # checks validations but does not save to the db
      UserMailer.notify_confirmation(self.user).deliver_now!
      self.update_columns(publication_state: "confirmed") # this one needed
   else
      fsm.trigger(:cancel) # calling with fms.cancel! return an error
   end
 end

from micromachine.

soveran avatar soveran commented on May 24, 2024

Can you show me the definition of confirm! in your model?

from micromachine.

benbonnet avatar benbonnet commented on May 24, 2024

It's a bit rewritten, the column is named publication_state

def draft!
  publication.trigger(:draft)
end

def confirm!
  publication.trigger(:confirm)
end

def publish!
  publication.trigger(:publish)
end

def publication
  @publication ||= begin
    fsm = MicroMachine.new(publication_state || "draft")

    fsm.when(:draft, "published" => "draft", "confirmed" => "draft")
    fsm.on("drafted") do
      puts "Drafted"
    end

    fsm.when(:confirm, "draft" => "confirmed")
    fsm.on("confirmed") do
      if save
        UserMailer.notify_submission(self).deliver_now!
        self.update_columns(publication_state: "confirmed")
      else
        fsm.trigger(:draft)
      end
    end

    fsm.when(:publish, "confirmed" => "published")
    fsm.on("published") do
      # will send another mail
    end

    fsm.on(:any) {
      self.publication_state = publication.state
    }

    fsm

  end
end

I'm using it with rails5, could it bother ?
Hope I did not wrote dumb things; thanks a lot for your help

from micromachine.

soveran avatar soveran commented on May 24, 2024

Yes, the reason why that happens is because the callbacks defined for :any are the last to be executed. That means at the time the on("confirmed") block runs, the publication_state attribute is not yet updated. What you can do to avoid two roundrips to the database, is to update publication_state in the on("confirmed") block, like this:

fsm.on("confirmed") do
  self.publication_state = publication.state

  if save
    notify_submission!
  else
    draft!
  end
end

from micromachine.

benbonnet avatar benbonnet commented on May 24, 2024

All good now.

I feel like there are some bugs somehow in rails5; without being sure. I started by trying the same (sending an email when the column changed) with dirty methods (verifying with publication_state_changed?)

Dirty methods or micromachine : works perfectly in dev, column get schanged, the mail's sent.But in production; absolutely no clue why the mail is not sent. big mystery

But it goes out of topic. Thanks a lot for helping understand better the use of this gem

from micromachine.

soveran avatar soveran commented on May 24, 2024

Are you able to send other emails? Can you log a message right before and after calling notify_submission!? I'm sorry I'm not familiar with Rails, but in any case it should work and I'd like to help you debug the issue.

from micromachine.

benbonnet avatar benbonnet commented on May 24, 2024

that's a very rails thing for sure; mails sent, etc… weirdness for this specific case.

Issue's solved so far and knowledge acquired, and pretty happy to be able to use micromachine in the future

from micromachine.

soveran avatar soveran commented on May 24, 2024

Ok! Good luck and again, feel free to contact me anytime.

from micromachine.

benbonnet avatar benbonnet commented on May 24, 2024

That was just a silent smtp thing for those emails not being sent (different provider in dev and prod, hence this unobvious problem)

thx a lot again 👍 💯 ; very happy to go further with micromachine then

from micromachine.

soveran avatar soveran commented on May 24, 2024

Glad you found the issue :-)

from micromachine.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.