geekq / workflow

Ruby finite-state-machine-inspired API for modeling workflow
MIT License
1.75k stars 207 forks source link

Touch ActiveRecord instances when changing states #150

Closed lwoodson closed 2 years ago

lwoodson commented 9 years ago

Currently when invoking transition event methods with ActiveRecord models, only the workflow_state column is updated in the database. The updated_at column is not modified. This can be seen here:

https://github.com/geekq/workflow/blob/master/lib/workflow/adapters/active_record.rb#L19

I would propose that the default behavior should be that updated_at should be updated when workflow state's change. If you agree @geekq, I can put together a pull request for this functionality.

geekq commented 9 years ago

I agree, makes sense

lwoodson commented 9 years ago

Alright I'll put something together soon and get a PR back.

vitaly commented 9 years ago

I'd say it makes sense to just use save!. saving just the state column without the rest of the changed attributes can create an invalid record in the database. i.e. one that would not be .valid? after a find.

for example, if 'rejected' state requires 'reject_reason' to be set, the record in the db will not hold the reject_reason if the object is not saved after transition.

lwoodson commented 9 years ago

Will be putting the PR together tonight or tomorrow night. I agree, @vitaly . I'll make sure that the entire changed state of the instance is saved and that its accomplished with a single DB call.

vitaly commented 9 years ago

I think the whole change is quite simple:

      def persist_workflow_state(new_value)
        write_attribute(self.class.workflow_column, new_value)
        save!
      end
lwoodson commented 9 years ago

Yep, just haven't had time. Feel free to make the PR yourself, if you like. I'm pushing a big feature to (work) production on Tuesday, so I'm in grind mode.

vitaly commented 9 years ago

https://github.com/geekq/workflow/pull/154

reicheltd commented 9 years ago

I dont get it. I have just switched from aasm gem to workflow, only to find out, it skips validations on transitions. "This way it is possible to deal with the validation and to save the pending changes to a record at some later point instead of the moment when transition occurs." Stupid explanation.