Reputation: 7172
I'm looking at the ruby gem workflow: https://github.com/geekq/workflow
The examples and other examples on the web have the workflow code in the model itself. I believe this is a violation of SOLID. Also, business logic would also be encoded in the model class (actions taken as the model goes though several states), which is a violation of the MVC contract.
Example:
class Message
workflow do
state :spam_check do
event :is_spam, transitions_to => :destroy
event :is_not_spam,:transitions_to => :finished
end
end
# business logic
def is_spam
self.user.spammer_score += 1
if self.user.spammer_score > 5
self.user.destroy
end
end
end
Is this good programming? If not, where in a rails project should state changes be coded?
Upvotes: 1
Views: 397
Reputation: 593
Daniel, it seems that the code listing you shared and the code listing on the Ruby Gem Workflow (https://github.com/geekq/workflow) - define states, events, transitions and actions on the model objects.
Code Listing from the Gem Workflow link
class Article
include Workflow
workflow do
state :new do
event :submit, :transitions_to => :awaiting_review
end
state :awaiting_review do
event :review, :transitions_to => :being_reviewed
end
state :being_reviewed do
event :accept, :transitions_to => :accepted
event :reject, :transitions_to => :rejected
end
state :accepted
state :rejected
end
end
For MVC and SOLID, I think if this code can be modified in such a way that Workflow applies to Controller instead of the Model.
Model is an instance object of domain entities - Message or Article. They don't define workflow themselves. They can be in any state any time. It is the system requirements that has a context of states for any given model objects. For example a message itself doesn't care if its spam. An Article itself does not have a tag that its new, rejected, approved, submitted or being reviewed.
Hence if Controller is given the Workflow responsibilities, then controller will handle events, will take actions and will do state transitions. In that, the actions taken by the controller may perform necessary work to modify model objects.
Real like analogy: Author starts writing article in his notebook (with pen). Keeps it with him till its not satisfactory. The article itself has know meaning that its new / fresh. Then author submits the article for review. The article moves to the reviewer desk, yet Article itself can't define the state that it can't be modified. and so on.
Does that make sense!?
Upvotes: 0
Reputation: 7172
After some study...
I think that a state diagram represents the state of the model and can easily be abused to be a work flow engine.
Upvotes: 0