Policy objects are great. However, I don't 100% agree with examples in this post. Putting methods like active? in the model leads to fat models, but I'd argue it does not break the SRP (well... ActiveRecord itself breaks it, but it does not break it more than it's already broken ;) ). In your example, active? method relies solely on the state of the model instance, which IMO justifies putting it in the model.
In my mind policy objects usually coordinate the model with something else, often current_user. Having model know anything about current user is a huge smell, so policy object is much better at handling it. Something like this:
yeah, you're right, I probably wouldn't have created a Policy object for such example, I just wanted to show the idea and keep the code as simple and straightforward as possible :)
Policy objects are great. However, I don't 100% agree with examples in this post. Putting methods like
active?
in the model leads to fat models, but I'd argue it does not break the SRP (well... ActiveRecord itself breaks it, but it does not break it more than it's already broken ;) ). In your example,active?
method relies solely on the state of the model instance, which IMO justifies putting it in the model.In my mind policy objects usually coordinate the model with something else, often
current_user
. Having model know anything about current user is a huge smell, so policy object is much better at handling it. Something like this:with
I think this might be more convincing for people who want to put everything in the model.
yeah, you're right, I probably wouldn't have created a Policy object for such example, I just wanted to show the idea and keep the code as simple and straightforward as possible :)
Sure thing.
By the way, which do you think is better? ;)
I think if we call it
Policies::Article
then we should acceptarticle
in the initializer, so I would go with the following: