active_model_serializers-cancan
active_model_serializers-cancan copied to clipboard
REQ: flags for abilities
I use AMS to feed Backbone models, and in some cases I need the model to know whether it has permission to do something back on the server before attempting it. E.g. I want to show a lock icon if an update
will fail because the current_user.cannot? :edit, @object
.
Right now I do this:
class QuestionSerializer < ActiveModel::Serializer
attributes :id, :prompt, :feedback, :can_edit
def can_edit
current_ability = Ability.new(scope)
current_ability.can? :edit, @object
end
end
Instead I'd like to use a_m_s-cancan to make those flags declaratively:
class QuestionSerializer < ActiveModel::Serializer
attributes :id, :prompt, :feedback
cancan_flags :edit # adds `can_edit` property as above
end
I understand if the project maintainers aren't interested in doing this, so no expectations here. I made the issue to 1) see if there's enough demand that I try and 2) to document how to set up CanCan flags without loading this gem.
This functionality definitely sounds useful. IMO, it would make more sense to serialize this information in the meta
attribute rather than being at the top level.