action_policy icon indicating copy to clipboard operation
action_policy copied to clipboard

I18n does not seem to work with I18n Active Record

Open andreas-it-dev opened this issue 2 years ago • 1 comments

Tell us about your environment

Ruby Version: 3.1.3

Framework Version (Rails, whatever): 7.0.4.2

Action Policy Version: latest

Reproduction Script: n/a

What did you do?

i am using I18n active record but this doesnt seem to fly with the I18n support built into action_policy.

What did you expect to happen?

it should use a translation key thats actually stored in the db: action_policy.policy.session.create?

What actually happened?

it used the default key: action_policy.unauthorized

my application_controller looks like this:

  rescue_from ActionPolicy::Unauthorized do |e|
    message = {
      message: e.result.message,
      show: true,
      type: 'error' }
    render json: { message: message },
           status: :forbidden
  end

i have a SessionPolicy

class SessionPolicy < ApplicationPolicy
  authorize :user

  def create?
    false
  end

  def destroy?
    true
  end
end

which is called by this in my SessionsController:

  def create
    message = {
      message: I18n.t('session.create.unsuccessful'),
      show: true,
      type: 'error' }
    user = User.authenticate(session_params[:email], session_params[:password])
    authorize! user, with: SessionPolicy, context: { user: user }

 <snip>
  end

maybe i do something wrong?

andreas-it-dev avatar Feb 23 '23 08:02 andreas-it-dev

We have no assumptions on I18n backend, we simply call I18n.t: https://github.com/palkan/action_policy/blob/04a579088846e8f4bfba28cf37f2f0954a3e6cde/lib/action_policy/i18n.rb#L14

Can you please provide the value of the e.result.all_details? Also, e.result.rule and e.result.policy.identifier

palkan avatar Mar 14 '23 18:03 palkan