pundit-plus 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e24b7b92951d89eacea12daeb0883a4210e1f900d3dd472fd069119f197a978
4
- data.tar.gz: 2de203579d764e39b71c72c3bc3dc0c13138ae7d97d87f0b0676ee0925156fd7
3
+ metadata.gz: ce22ded18b7ecf472d8fa7f35e15e5a6653a57d5222cc28d88326da396470f1e
4
+ data.tar.gz: ecf9b251f941b71687937a9acf5e63b7459ad8919271213a3402b98b2606b94f
5
5
  SHA512:
6
- metadata.gz: f6fa345e74e3a2dd1a3756782226fab68a0d1e005f9f993b0273132c18648fe8616c938a2fce78df2ed3d26b3744fe42e08a7ff80876ad3ca0e755f3e9ab3a80
7
- data.tar.gz: 29c5545f4294059194b327dbbc7f3b8ff81463e97dcfcc767a30d053ed29e782d7c53bdfe9a67f69e9ed92a4790ec59ebe432510eaff2d62a5b34aafdee7a13e
6
+ metadata.gz: e5836505f3537b4d8399c09933edab89ab3ce0730816c3d7f38aad5003399d587ab4d600ce187ed218b99386345d1f7fe4d4c16c190b16475e69030b754568db
7
+ data.tar.gz: 8f022172f750e43af4724da0a480324c946a0ed90ed8cab0d0547132b01ea76ab14803a6027d53faa3740455f0308617ea8f35be06db47b51c4cee850e7e6fee
data/CHANGELOG.md CHANGED
@@ -2,7 +2,17 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- ## [2024-04-10]
5
+ ## [0.1.2] - 2024-04-10
6
+
7
+ ### Added
8
+ - Fixed the missing changelog information for the previous version.
9
+
10
+ ## [0.1.1] - 2024-04-10
11
+
12
+ ### Added
13
+ - Added params_for_action support in controller Authorization
14
+
15
+ ## [0.1.0] - 2024-04-10
6
16
 
7
17
  ### Added
8
18
  - Initial project setup with basic Pundit integration.
data/README.md CHANGED
@@ -42,6 +42,18 @@ class MyPolicy < ApplicationPolicy
42
42
  super
43
43
  end
44
44
  end
45
+
46
+ def initialize(user, record)
47
+ @user = user
48
+ @record = record
49
+ end
50
+
51
+ # Return the params for the action but require a special key
52
+ def params_for_create(params)
53
+ params_for(@record, params, :create).tap do |params|
54
+ params.require(:special_key)
55
+ end
56
+ end
45
57
  end
46
58
  ```
47
59
 
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pundit"
4
+
5
+ module Pundit
6
+ module Plus
7
+ # This module adds to the Pundit::Authorization module so that anywhere it is
8
+ # included will also receive the methods added in this.
9
+ module Authorization
10
+ # Return the params that are required or permitted for the given action.
11
+ #
12
+ # Define a method in your policy class called `params_for_#{action}` to
13
+ # return the params for that action. If no such method exists, then
14
+ # `permitted_attributes` is called to return the params.
15
+ #
16
+ # In your policy, define the relevant method to receive parameters and
17
+ # specify permitted or required parameters.
18
+ #
19
+ # @example
20
+ # class MyPolicy < ApplicationPolicy
21
+ # def params_for_create(params)
22
+ # params.require(:user).permit(*permitted_attributes_for_create).tap do |permitted|
23
+ # permitted.require(:special_value)
24
+ # end
25
+ # end
26
+ # end
27
+ def params_for_action(record, action = action_name)
28
+ if policy(record).respond_to?(:"params_for_#{action}")
29
+ policy(record).send(:"params_for_#{action}", params)
30
+ else
31
+ permitted_attributes(record, action)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ Pundit::Authorization.include(Pundit::Plus::Authorization)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Pundit
4
4
  module Plus
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
data/lib/pundit/plus.rb CHANGED
@@ -2,11 +2,33 @@
2
2
 
3
3
  require_relative "plus/version"
4
4
  require_relative "plus/custom_exception"
5
+ require_relative "plus/authorization"
5
6
 
6
7
  module Pundit
7
8
  module Plus
9
+ # The default exception to raise when authorization fails.
8
10
  def exception_from(query:)
9
11
  Pundit::NotAuthorizedError
10
12
  end
13
+
14
+ # Return the params that are required or permitted for the given action.
15
+ #
16
+ # This assumes that you will define a `permitted_attributes` method in your
17
+ # policy class to return the permitted attributes for the record. Or you may
18
+ # define a method called `permitted_attributes_for_#{action}` to return the
19
+ # permitted attributes for that action.
20
+ protected def params_for(record, params, action_name)
21
+ param_method = if respond_to?(:"permitted_attributes_for_#{action_name}")
22
+ "permitted_attributes_for_#{action_name}"
23
+ else
24
+ "permitted_attributes"
25
+ end
26
+
27
+ required = params.require(
28
+ PolicyFinder.new(record).param_key
29
+ ).permit(
30
+ *send(param_method)
31
+ )
32
+ end
11
33
  end
12
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit-plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-10 00:00:00.000000000 Z
11
+ date: 2024-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pundit
@@ -50,6 +50,7 @@ files:
50
50
  - README.md
51
51
  - Rakefile
52
52
  - lib/pundit/plus.rb
53
+ - lib/pundit/plus/authorization.rb
53
54
  - lib/pundit/plus/custom_exception.rb
54
55
  - lib/pundit/plus/version.rb
55
56
  homepage: https://github.com/SOFware/pundit-plus