Skip to content

Commit 571d99f

Browse files
rahulbileTom Kirkpatrick
authored andcommitted
feat: optionally allow to pass options for updateAttributes
1 parent 2361e39 commit 571d99f

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ When a model method that is controlled by the Finite State Machine is called it
3131
"StateMachine": {
3232
"stateProperty": "status",
3333
"events": [
34-
{ "name": "activate", "from": "none", "to": "active" },
34+
{ "name": "activate", "from": "none", "to": "active", "transitionOptions": { "skipBeforeSave" : true } },
3535
{ "name": "cancel", "from": "active", "to": "canceled" },
3636
{ "name": "reactivate", "from": "canceled", "to": "active" },
3737
{ "name": "expire", "from": [ "active", "canceled" ], "to": "expired" }

lib/mixins/state-machine.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ module.exports = function StateMachine(Model, settings) {
2424
// Persist the updated state in the database.
2525
onenter: function onenter(options) {
2626
debug(`Finalizing state: ${options.to}`)
27-
return options.instance.updateAttribute(stateProperty, options.to)
27+
let transitionOptions = null
28+
const eventOptions = _.find(settings.events, { 'name': options.name })
29+
30+
if (eventOptions && eventOptions.transitionOptions) {
31+
transitionOptions = eventOptions.transitionOptions
32+
}
33+
34+
return options.instance.updateAttribute(stateProperty, options.to, transitionOptions)
2835
.then(result => {
2936
options.instance = result
3037
return Model.notifyObserversOf('fsm:onenter', options)

test/fullcube-state-machine/common/models/subscription.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"stateProperty": "status",
7373
"events": [
7474
{ "name": "activate", "from": "none", "to": "active" },
75-
{ "name": "cancel", "from": "active", "to": "canceled" },
75+
{ "name": "cancel", "from": "active", "to": "canceled", "transitionOptions": { "skipBeforeSave" : true } },
7676
{ "name": "reactivate", "from": "canceled", "to": "active" },
7777
{ "name": "expire", "from": [ "active", "canceled" ], "to": "expired" }
7878
]

test/test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,36 @@ describe('State changes', function() {
7878
})
7979
})
8080

81+
describe('Transition Options', function() {
82+
before(function() {
83+
return Subscription.create({ status: 'active' })
84+
.then(subscription => {
85+
this.subscription = subscription
86+
})
87+
})
88+
89+
before(function() {
90+
sinon.spy(Subscription.prototype, 'updateAttribute')
91+
})
92+
93+
it('Should have called updateAttribute with transitionOptions', function() {
94+
return this.subscription.cancel()
95+
.then(subscription => {
96+
this.subscription = subscription
97+
expect(subscription).to.have.property('status', 'canceled')
98+
expect(Subscription.prototype.updateAttribute).to.be.calledWith('status', 'canceled', { skipBeforeSave: true })
99+
})
100+
})
101+
102+
it('Should have not called updateAttribute with skipBeforeSave transitionOptions', function() {
103+
return this.subscription.expire()
104+
.then(subscription => {
105+
expect(subscription).to.have.property('status', 'expired')
106+
expect(Subscription.prototype.updateAttribute).to.be.calledWith('status', 'expired')
107+
})
108+
})
109+
})
110+
81111
describe('Observers', function() {
82112
const event = 'cancel'
83113
const fromState = 'active'

0 commit comments

Comments
 (0)