punditry 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e81287ab76fb1990f108538563b7b29f6b2a218
4
+ data.tar.gz: 708696dc885b6923d89e5bad137d64724642fbeb
5
+ SHA512:
6
+ metadata.gz: fca8d0fe3a47075c8bb7934c94e31e457a5b59145687180ec376ab9f396bc042378be216fabec4b18a9480aeae62f76922e9bd1d4121232b090339b0c7123258
7
+ data.tar.gz: 2dc9ca0a8c4fdfe43c11adbf3346f39e6a3b343cdfe73698ecb4b95ac244e660aad531be0bf8ffb8106d48c7be2fd3d051abdaee2e231313226de6dbe484683d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in punditry.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jason Kriss
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Punditry
2
+
3
+ A super-slim wrapper on top of [Pundit](https://github.com/elabs/pundit).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'punditry'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Include Punditry in your application controller:
16
+
17
+ ``` ruby
18
+ class ApplicationController < ActionController::Base
19
+ include Punditry::Controller
20
+ end
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ Punditry is essentially Pundit with a few minor additions. Instead of calling `authorize`, you call `authorize!`. This delegates to `authorize` underneath but will also return the passed in resource, allowing you to eliminate a line of code:
26
+
27
+ ``` ruby
28
+ # with Pundit
29
+ def update
30
+ @post = Post.find(params[:id])
31
+ authorize @post
32
+ if @post.update(post_params)
33
+ redirect_to @post
34
+ else
35
+ render :edit
36
+ end
37
+ end
38
+ ```
39
+
40
+ ``` ruby
41
+ # with Punditry
42
+ def update
43
+ @post = authorize!(Post.find(params[:id]))
44
+ if @post.update(post_params)
45
+ redirect_to @post
46
+ else
47
+ render :edit
48
+ end
49
+ end
50
+ ```
51
+
52
+ Woohoo! Big win! I also prefer the bang method to indicate that it raises an error when it is not authorized, but that's a matter of taste.
53
+
54
+ `authorize!` has one more addition. If you pass in a collection, it also calls `policy_scope` on the resource. This makes the assumption that you want to authorize `index` actions as well, instead of only scoping them. This is another matter of taste. Also, reflecting this assumption, Punditry verifies that every action calls `authorize!` including `index`. If you need to opt out of this, you can simply call `skip_authorization` in your controller:
55
+
56
+ ``` ruby
57
+ class PostsController < ApplicationController
58
+ skip_authorization only: :check
59
+
60
+ def check
61
+ # some action that does not require authorization
62
+ end
63
+ end
64
+ ```
65
+
66
+ `Punditry::Controller` gives you one other helper method. If you follow the recommendations [here](https://github.com/elabs/pundit#strong-parameters), then you can simply pass a resource to `whitelist` and get back the permitted attributes for that resource:
67
+
68
+ ``` ruby
69
+ def update
70
+ @post = authorize!(Post.find(params[:id]))
71
+ if @post.update(whitelist(@post))
72
+ redirect_to @post
73
+ else
74
+ render :edit
75
+ end
76
+ end
77
+ ```
78
+
79
+ Punditry also provides you with a base policy `Punditry::Policy` that all of your polices should inherit from. This policy is very basic, but using it allows you to write tests like [this](http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/). Simply require `punditry/rspec` in your `spec_helper/rails_helper`.
80
+
81
+ That's it. Punditry simply leverages the power and simplicity of Pundit while making things just slightly more convenient.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/punditry.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "punditry/controller"
2
+ require "punditry/policy"
3
+ require "punditry/version"
4
+
5
+ module Punditry
6
+
7
+ end
@@ -0,0 +1,31 @@
1
+ require "pundit"
2
+
3
+ module Punditry
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ include Pundit
8
+
9
+ included do
10
+ after_action :verify_authorized
11
+ after_action :verify_policy_scoped, only: :index
12
+ end
13
+
14
+ module ClassMethods
15
+ def skip_authorization(options = {})
16
+ skip_before_action(:verify_authorized, options)
17
+ skip_before_action(:verify_policy_scoped, options)
18
+ end
19
+ end
20
+
21
+ private
22
+ def authorize!(resource)
23
+ resource = policy_scope(resource) if resource.respond_to?(:to_a)
24
+ authorize(resource) && resource
25
+ end
26
+
27
+ def whitelist(resource)
28
+ params.permit(*policy(resource).permitted_attributes)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,60 @@
1
+ module Punditry
2
+ class Policy
3
+ attr_reader :user, :resource
4
+
5
+ def initialize(user, resource)
6
+ @user = user
7
+ @resource = resource
8
+ end
9
+
10
+ def index?
11
+ false
12
+ end
13
+
14
+ def new?
15
+ false
16
+ end
17
+
18
+ def create?
19
+ false
20
+ end
21
+
22
+ def show?
23
+ false
24
+ end
25
+
26
+ def edit?
27
+ false
28
+ end
29
+
30
+ def update?
31
+ false
32
+ end
33
+
34
+ def destroy?
35
+ false
36
+ end
37
+
38
+ class Scope
39
+ attr_reader :user, :scope
40
+
41
+ def initialize(user, scope)
42
+ @user = user
43
+ @scope = scope
44
+ end
45
+
46
+ def resolve
47
+ scope
48
+ end
49
+ end
50
+
51
+ private
52
+ def parent
53
+ resource.instance_variable_get(:@association).owner if collection?
54
+ end
55
+
56
+ def collection?
57
+ resource.is_a?(ActiveRecord::Associations::CollectionProxy)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ RSpec::Matchers.define :authorize do |action|
2
+ match do |policy|
3
+ policy.public_send("#{action}?")
4
+ end
5
+
6
+ failure_message do |policy|
7
+ "#{policy.class} does not authorize #{action} on #{policy.resource} for #{policy.user.inspect}."
8
+ end
9
+
10
+ failure_message_when_negated do |policy|
11
+ "#{policy.class} does not forbid #{action} on #{policy.resource} for #{policy.user.inspect}."
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Punditry
2
+ VERSION = "0.1.0"
3
+ end
data/punditry.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'punditry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "punditry"
8
+ spec.version = Punditry::VERSION
9
+ spec.authors = ["Jason Kriss"]
10
+ spec.email = ["jasonkriss@gmail.com"]
11
+ spec.summary = %q{A super-slim wrapper on top of Pundit.}
12
+ spec.homepage = "https://github.com/jasonkriss/punditry"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "pundit"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: punditry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Kriss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pundit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - jasonkriss@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/punditry.rb
68
+ - lib/punditry/controller.rb
69
+ - lib/punditry/policy.rb
70
+ - lib/punditry/rspec.rb
71
+ - lib/punditry/version.rb
72
+ - punditry.gemspec
73
+ homepage: https://github.com/jasonkriss/punditry
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A super-slim wrapper on top of Pundit.
97
+ test_files: []