Skip to content

Commit

Permalink
Initial pundit-plus implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Apr 10, 2024
0 parents commit 19f72a0
Show file tree
Hide file tree
Showing 19 changed files with 567 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: 'bundler'
directory: '/'
schedule:
interval: 'weekly'
64 changes: 64 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: Testing

on:
push:
branches-ignore: [master]

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
version: ["3.2"]
env:
NOTIFY_SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
NOTIFY_SLACK_NOTIFY_CHANNEL: "oss-notices"
NOTIFY_CURRENT_REPOSITORY_URL: "${{ github.server_url }}/${{ github.repository }}"
NOTIFY_TEST_RUN_ID: "${{ github.run_id }}"
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.version }}
bundler-cache: true
- name: Update Bundle
run: bundle check || bundle install
- name: Set ownership
run: |
# this is to fix GIT not liking owner of the checkout dir
git config --global --add safe.directory "$GITHUB_WORKSPACE"
- uses: amancevice/setup-code-climate@v1
with:
cc_test_reporter_id: ${{ secrets.CC_TEST_REPORTER_ID }}
- run: cc-test-reporter before-build
- name: Test
run: bundle exec rake
- run: cc-test-reporter after-build
if: ${{ github.event_name != 'pull_request' }}

linter:
name: Linter
runs-on: ubuntu-latest
strategy:
matrix:
version: ["3.2"]
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.version }}
bundler-cache: true
- run: bundle exec standardrb
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
spec/examples.txt
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--require spec_helper
--require debug
--order random
4 changes: 4 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "simplecov"
SimpleCov.start do
add_filter "/spec/"
end
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All notable changes to this project will be documented in this file.

## [2024-04-10]

### Added
- Initial project setup with basic Pundit integration.
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec

gem "debug"
gem "rake"
gem "rspec"
gem "simplecov"
gem "yard"
gem "standard"
133 changes: 133 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
PATH
remote: .
specs:
pundit-plus (0.1.0)
pundit
pundit-matchers

GEM
remote: https://rubygems.org/
specs:
activesupport (7.1.3.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.7)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
docile (1.4.0)
drb (2.2.1)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.12.0)
rdoc
reline (>= 0.4.2)
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
minitest (5.22.3)
mutex_m (0.2.0)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
psych (5.1.2)
stringio
pundit (2.3.1)
activesupport (>= 3.0.0)
pundit-matchers (3.1.2)
rspec-core (~> 3.12)
rspec-expectations (~> 3.12)
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
racc (1.7.3)
rainbow (3.1.1)
rake (13.2.1)
rdoc (6.6.3.1)
psych (>= 4.0.0)
regexp_parser (2.9.0)
reline (0.5.1)
io-console (~> 0.5)
rexml (3.2.6)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.62.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.2)
parser (>= 3.3.0.4)
rubocop-performance (1.20.2)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
standard (1.35.1)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.62.0)
standard-custom (~> 1.0.0)
standard-performance (~> 1.3)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.3.1)
lint_roller (~> 1.1)
rubocop-performance (~> 1.20.2)
stringio (3.1.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
yard (0.9.36)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
debug
pundit-plus!
rake
rspec
simplecov
standard
yard

BUNDLED WITH
2.5.6
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 SOFware LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Pundit::Plus

Add some extra features to Pundit.

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add pundit-plus

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install pundit-plus

## Usage

This includes [Pundit](https://github.com/varvet/pundit) and [Pundit Matchers](https://github.com/pundit-community/pundit-matchers).

Follow the instructions for using Pundit and Pundit Matchers.

Include the `Pundit::Plus` module in your policy classes to add extra features to Pundit. For example:

```ruby
class ApplicationPolicy
include Pundit::Plus
# this module defines the default exception_from behavior
end

class MyPolicy < ApplicationPolicy
# Then you can use your own exception classes
class CustomException < Pundit::NotAuthorizedError
def initialize(options = {})
options[:message] ||= "You are not authorized to perform this action."
super(options)
end
end

def exeception_from(query:)
if query == :show?
CustomException
else
super
end
end
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/SOFware/pundit-plus.
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "minitest/test_task"

Minitest::TestTask.create

task default: :test
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "pundit/plus"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
12 changes: 12 additions & 0 deletions lib/pundit/plus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative "plus/version"
require_relative "plus/custom_exception"

module Pundit
module Plus
def exception_from(query:)
Pundit::NotAuthorizedError
end
end
end
24 changes: 24 additions & 0 deletions lib/pundit/plus/custom_exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "pundit"

module Pundit
module Plus
# This module is prepended to Pundit to allow for custom exceptions to be
# raised by policies.
module CustomException
# This method is called by Pundit when a policy raises an exception.
#
# The default implementation raises the exception that was passed to
# `authorize`, but you can override this method in your policy classes to
# handle the exception differently.
#
# To make use of this method define the `exception_from` method in your
# policy class.
def raise(klass, query:, record:, policy:)
super(policy.exception_from(query:), query: query, record: record, policy: policy)
end
end
end
end
Pundit.singleton_class.prepend(Pundit::Plus::CustomException)
Loading

0 comments on commit 19f72a0

Please sign in to comment.