Comments (13)
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.
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.
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.
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.
Can you show me the definition of confirm!
in your model?
from micromachine.
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.
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.
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.
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.
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.
Ok! Good luck and again, feel free to contact me anytime.
from micromachine.
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.
Glad you found the issue :-)
from micromachine.
Related Issues (9)
- :any transition state HOT 1
- Bang methods in the README example HOT 3
- Test suite fails to run on ruby 2.1.2 HOT 3
- Change to callback parameters in 1.2 is backwards-incompatible HOT 4
- Trigerring Event Automatically
- Sharing some love HOT 1
- Add `#triggerable_events` method? HOT 7
- Rubygems version is not the latest version HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micromachine.