Skip to content

Instantly share code, notes, and snippets.

@palkan
Created August 17, 2018 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palkan/a4c482eeb8453ef6b103ee05f8c2f077 to your computer and use it in GitHub Desktop.
Save palkan/a4c482eeb8453ef6b103ee05f8c2f077 to your computer and use it in GitHub Desktop.
[draft] Pundit to Action Policy

From Pundit to ActionPolicy:

  • Remove include Pundit from ApplicationController
  • Add alias authorize authorize!
  • Add authorize :current_user, as: :user
  • Add include ActionPolicy::Policy::Core to ApplicationPolicy
  • Update ApplicationPolicy#initialize:
def initialize(target, user:)
  # ...
end
  • Add policy helper:
helper_method :policy

def policy(record)
  policy_for(record)
end

NOTE: policy defined above is not equal to allowed_to? since it doesn't take into account pre-checks.

  • Replace RSpec helper:
# require 'pundit/rspec'
require 'action_policy/rspec/pundit_syntax'
  • TODO: scopes migration

When everything is green, it's time to fully migrate to ActionPolicy:

  • make ApplicationPolicy inherit from ActionPolicy::Base
  • migrate view helpers (from policy(..) to allowed_to?)
  • re-write specs using simple non-DSL syntax
  • add controller specs (add require 'action_policy/rspec')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment