pundit-plus 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e24b7b92951d89eacea12daeb0883a4210e1f900d3dd472fd069119f197a978
4
- data.tar.gz: 2de203579d764e39b71c72c3bc3dc0c13138ae7d97d87f0b0676ee0925156fd7
3
+ metadata.gz: 03ec5ae53181cd9cdd6292e90d86510d4e9f4ae6830b855f008653cab5f39589
4
+ data.tar.gz: '08c9991f40b92bdee66f747f58c8fd3b7ef3722c58e1c0264cae8811706b89fc'
5
5
  SHA512:
6
- metadata.gz: f6fa345e74e3a2dd1a3756782226fab68a0d1e005f9f993b0273132c18648fe8616c938a2fce78df2ed3d26b3744fe42e08a7ff80876ad3ca0e755f3e9ab3a80
7
- data.tar.gz: 29c5545f4294059194b327dbbc7f3b8ff81463e97dcfcc767a30d053ed29e782d7c53bdfe9a67f69e9ed92a4790ec59ebe432510eaff2d62a5b34aafdee7a13e
6
+ metadata.gz: eb3a022d9c0c68afe59ec8faa4ed96be129f8397139597bc6776463b6a06dae05b279dd0a82e1294e3a6df2bfd02ed231f8c1b7695d8610970ccdfe463823eff
7
+ data.tar.gz: a88a2245f2a68f102d1cecdcb813fc8437520cfb90f83b8a645cf928543ce00cbdc3bdb321ceeeb5db7d07879d7c93df788bf78c4e910006c6eb4036817351ab
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.1"
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.1
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