{"id":15175164,"url":"https://github.com/javierav/pundit-before","last_synced_at":"2026-02-07T07:33:39.537Z","repository":{"id":65288547,"uuid":"584822147","full_name":"javierav/pundit-before","owner":"javierav","description":"Before hook for Pundit","archived":false,"fork":false,"pushed_at":"2023-01-15T19:39:45.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"development","last_synced_at":"2025-11-15T05:26:35.397Z","etag":null,"topics":["before","hook","pundit","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/javierav.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-03T15:51:25.000Z","updated_at":"2023-01-15T22:54:05.000Z","dependencies_parsed_at":"2023-01-16T07:16:05.473Z","dependency_job_id":null,"html_url":"https://github.com/javierav/pundit-before","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/javierav/pundit-before","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javierav%2Fpundit-before","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javierav%2Fpundit-before/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javierav%2Fpundit-before/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javierav%2Fpundit-before/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/javierav","download_url":"https://codeload.github.com/javierav/pundit-before/tar.gz/refs/heads/development","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javierav%2Fpundit-before/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29189355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T05:07:31.176Z","status":"ssl_error","status_checked_at":"2026-02-07T05:06:15.227Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["before","hook","pundit","rails","ruby"],"created_at":"2024-09-27T12:04:13.650Z","updated_at":"2026-02-07T07:33:39.522Z","avatar_url":"https://github.com/javierav.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pundit::Before\n\n![CI](https://github.com/javierav/pundit-before/workflows/CI/badge.svg)\n\nAdds `before` hook to pundit policy classes to resolve things like varvet/pundit#474. Inspired by action_policy\n[pre-checks](https://actionpolicy.evilmartians.io/#/pre_checks).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"pundit-before\"\n```\n\nAnd then execute:\n\n```shell\nbundle install\n```\n\n## Usage\n\nUse `allow!` inside callback method or block to return `true` without evaluating `edit?` method defined in policy.\n\n```ruby\nclass UserPolicy \u003c ApplicationPolicy\n  include Pundit::Before\n\n  before :check_admin\n\n  def edit?\n    false\n  end\n\n  private\n\n  def check_admin\n    allow! if user.admin?\n  end\nend\n\nUserPolicy.new(User.new(admin: true), record).edit?  # =\u003e true\nUserPolicy.new(User.new(admin: false), record).edit? # =\u003e false\n```\n\nUse `deny!` inside callback method or block to return `false` without evaluating `edit?` method defined in policy.\n\n```ruby\nclass UserPolicy \u003c ApplicationPolicy\n  include Pundit::Before\n\n  before :check_admin\n\n  def edit?\n    true\n  end\n\n  private\n\n  def check_admin\n    deny! unless user.admin?\n  end\nend\n\nUserPolicy.new(User.new(admin: true), record).edit?  # =\u003e true\nUserPolicy.new(User.new(admin: false), record).edit? # =\u003e false\n```\n\nInternally `before` hook is implemented as `ActiveSupport::Callbacks`, so the callback chain will halt if do any call to\n`allow!` or `deny!` method. It's similar as Rails controller action filters works.\n\n### block form\n\n```ruby\nclass UserPolicy \u003c ApplicationPolicy\n  include Pundit::Before\n\n  before do\n    allow! if user.admin?\n  end\n\n  def edit?\n    false\n  end\nend\n```\n\n### skip before hook\n\n```ruby\nclass UserPolicy \u003c ApplicationPolicy\n  include Pundit::Before\n\n  before :check_admin\n\n  def edit?\n    false\n  end\n\n  private\n\n  def check_admin\n    allow! if user.admin?\n  end\nend\n\nclass OperatorPolicy \u003c UserPolicy\n  skip_before :check_admin\nend\n\nUserPolicy.new(User.new(admin: true), record).edit?     # =\u003e true\nOperatorPolicy.new(User.new(admin: true), record).edit? # =\u003e false\n```\n\n### using `only` modifier\n\n```ruby\nclass UserPolicy \u003c ApplicationPolicy\n  include Pundit::Before\n\n  before :check_admin, only: :update?\n\n  def edit?\n    false\n  end\n\n  private\n\n  def check_admin\n    allow! if user.admin?\n  end\nend\n\nUserPolicy.new(User.new(admin: true), record).edit? # =\u003e false\n```\n\n### using `except` modifier\n\n```ruby\nclass UserPolicy \u003c ApplicationPolicy\n  include Pundit::Before\n\n  before :check_admin, except: :edit?\n\n  def edit?\n    false\n  end\n\n  def destroy?\n    false\n  end\n\n  private\n\n  def check_admin\n    allow! if user.admin?\n  end\nend\n\nUserPolicy.new(User.new(admin: true), record).edit?    # =\u003e false\nUserPolicy.new(User.new(admin: true), record).destroy? # =\u003e true\n```\n\n### calling multiple methods\n\n```ruby\nclass UserPolicy \u003c BasePolicy\n  before :check_presence, :check_admin\n\n  def edit?\n    false\n  end\n\n  private\n\n  def check_presence\n    deny! unless user.present?\n  end\n\n  def check_admin\n    allow! if user.admin?\n  end\nend\n\nUserPolicy.new(nil, record).edit?                    # =\u003e false\nUserPolicy.new(User.new(admin: false), record).edit? # =\u003e false\nUserPolicy.new(User.new(admin: true), record).edit?  # =\u003e true\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.\nYou can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bin/rake install`.\n\nTo release a new version, update the version number in `version.rb`, and then run `bin/rake release`,\nwhich will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to\n[rubygems.org](https://rubygems.org).\n\n## License\n\nCopyright © 2023 Javier Aranda. Released under the terms of the [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavierav%2Fpundit-before","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjavierav%2Fpundit-before","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavierav%2Fpundit-before/lists"}