-
I'm wondering what the best way to do this would be. My scenario has two roles, Admin and Super Admin, and an invoice model with a Super Admins are allowed to adjust the How would you structure this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That's an interesting example. I would use something like that: # assuming the model is named Product
class ProductPolicy < ApplicationPolicy
params_filter do |params|
params.permit(:cost, :cost_override).tap do |filtered|
filtered.delete(:cost_override) if (filtered[:cost_override] / filtered[:cost].to_f) > 0.25 && !super_admin?
end
end
end Here I delete unauthorized parameter; raising Unauthorized can be an option, too. But I assume that both I think, using policies for form submit verification is not the right way of doing this. I would add a form object to handle this operation and use policies there: # very abstract code
class ProductForm < ApplicationForm
include ActionPolicy::Behaviour
attribute :cost, :cost_override
validate :cost_override_within_limits, unless: :manage_cost?
# ...
def cost_override_within_limits
errors.add(:cost_override, "must be <25% of the cost") if (product.cost_override / produce.cost > 0.25)
end
end So, we still use a policy here ( |
Beta Was this translation helpful? Give feedback.
That's an interesting example.
I would use something like that:
Here I delete unauthorized parameter; raising Unauthorized can be an option, too.
But I assume that both
cost
andcost_override
present in the params. I guess, it's possible to update only thecost_override
(or only thecost
) param, right?I think, using policies for form submit verification is not the right way of doing this. I woul…