pundit_extra 0.2.0 → 0.3.1

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
- SHA1:
3
- metadata.gz: 340bc87e37b86b2aea59c99812302fa2e616af91
4
- data.tar.gz: 79f3d9754889cfdd68b6abd182758a1546634238
2
+ SHA256:
3
+ metadata.gz: 77376d6ce79786e54a8a0be60fc52fc45e643f578dcd248df97cbf6a2fdd5232
4
+ data.tar.gz: e3fa899de77740a36d984fcf7e8e50ed78fb70d3f5d8fa2b6038ac688aef87cc
5
5
  SHA512:
6
- metadata.gz: e589da8632dad772df73e3418e4f0f1523127c3b9dd3c3e31bff25373ffdf1fec361647d16380fc76033bbbc07a6931129b53b7fa37f4425041e2d93c900cc1a
7
- data.tar.gz: 6c0ef3c7cb6f5288d3ef617428722930dae635db8f79de522c23f0bdccda240f940bf42d5ddb31f279fe5545043b0d7101e43accd73a168e03fb8d4ea78b54f6
6
+ metadata.gz: 000b5790efde79b36c76c3c02207019ee88b827948c1786ece090f07b2789b13f67a857ddb2b6def903cc6a22502eedb74d4dfc741bfceb8f581260c00762e25
7
+ data.tar.gz: e5d5fb8b5342c0167423f80ef2b554ab1a71fb1910061837f3ace599e0f2eda715df41514cbb2b982cc37c748c89f98301d4a4c17ed7d78da8678a57caf5b4a4
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # PunditExtra
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/pundit_extra.svg)](http://badge.fury.io/rb/pundit_extra)
4
- [![Build Status](https://travis-ci.org/DannyBen/pundit_extra.svg?branch=master)](https://travis-ci.org/DannyBen/pundit_extra)
5
- [![Code Climate](https://codeclimate.com/github/DannyBen/pundit_extra/badges/gpa.svg)](https://codeclimate.com/github/DannyBen/pundit_extra)
6
- [![Dependency Status](https://gemnasium.com/DannyBen/pundit_extra.svg)](https://gemnasium.com/DannyBen/pundit_extra)
3
+ [![Gem Version](https://badge.fury.io/rb/pundit_extra.svg)](https://badge.fury.io/rb/pundit_extra)
4
+ [![Build Status](https://github.com/DannyBen/pundit_extra/workflows/Test/badge.svg)](https://github.com/DannyBen/pundit_extra/actions?query=workflow%3ATest)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/61990b2b88d45ea6c89d/maintainability)](https://codeclimate.com/github/DannyBen/pundit_extra/maintainability)
6
+
7
+ ---
7
8
 
8
9
  This library borrows functionality from [CanCan(Can)][2] and adds it to [Pundit][1].
9
10
 
@@ -32,7 +33,7 @@ Add to your `ApplicationController`:
32
33
 
33
34
  ```ruby
34
35
  class ApplicationController < ActionController::Base
35
- include Pundit
36
+ include Pundit::Authorization
36
37
  include PunditExtra
37
38
  end
38
39
  ```
@@ -62,7 +63,7 @@ end
62
63
  ```
63
64
 
64
65
  The `load_resource` filter will create the appropriate instance variable
65
- based onm the current action.
66
+ based on the current action.
66
67
 
67
68
  The `authorize_resource` filter will call Pundit's `authorize @model` in each
68
69
  action.
@@ -96,7 +97,7 @@ class TasksController < ApplicationController
96
97
  end
97
98
 
98
99
  def create
99
- # this happens automatically
100
+ # this happens automatically
100
101
  # @task = Task.new task_params
101
102
  # authorize @task
102
103
  end
@@ -1,10 +1,10 @@
1
1
  module PunditExtra
2
2
  def self.included(_base)
3
- if defined? ActionController::Base
4
- ActionController::Base.class_eval do
5
- include PunditExtra::Helpers
6
- include PunditExtra::ResourceAutoload
7
- end
3
+ return unless defined? ActionController::Base
4
+
5
+ ActionController::Base.class_eval do
6
+ include PunditExtra::Helpers
7
+ include PunditExtra::ResourceAutoload
8
8
  end
9
9
  end
10
10
  end
@@ -5,7 +5,7 @@ module PunditExtra
5
5
  end
6
6
 
7
7
  def can?(action, resource)
8
- policy(resource).send "#{action}?"
8
+ policy(resource).send :"#{action}?"
9
9
  end
10
10
 
11
11
  def cannot?(*args)
@@ -1,69 +1,101 @@
1
- require 'active_support/concern'
2
-
3
- module PunditExtra
4
- module ResourceAutoload
5
- extend ActiveSupport::Concern
6
-
7
- module ClassMethods
8
- def load_resource(options={})
9
- before_action :load_resource, options.dup
10
- end
11
-
12
- def authorize_resource(options={})
13
- before_action :authorize_resource, options.dup
14
- end
15
-
16
- def skip_authorization(options={})
17
- before_action :skip_authorization_and_scope, options.dup
18
- end
19
-
20
- def load_and_authorize_resource(options={})
21
- # :nocov:
22
- load_resource options
23
- authorize_resource options
24
- # :nocov:
25
- end
26
- end
27
-
28
- def load_resource
29
- scope = resource_class
30
- action = params[:action]
31
- varname = resource_name
32
- if action == 'index'
33
- varname = controller_name
34
- resource = policy_scope resource_class
35
- elsif ['new', 'create'].include? action
36
- resource = scope.new
37
- resource.attributes = send("#{resource_name}_params") if action == 'create'
38
- elsif params[:id]
39
- resource = scope.find params[:id]
40
- else
41
- resource = nil
42
- end
43
- instance_variable_set "@#{varname}", resource
44
- end
45
-
46
- def authorize_resource
47
- resource = resource_instance || resource_class
48
- authorize resource
49
- end
50
-
51
- def skip_authorization_and_scope
52
- action = params[:action]
53
- skip_policy_scope if action == 'index'
54
- skip_authorization
55
- end
56
-
57
- def resource_name
58
- controller_name.singularize
59
- end
60
-
61
- def resource_class
62
- resource_name.classify.constantize
63
- end
64
-
65
- def resource_instance
66
- instance_variable_get "@#{resource_name}"
67
- end
68
- end
69
- end
1
+ require 'active_support/concern'
2
+
3
+ module PunditExtra
4
+ module ResourceAutoload
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def load_resource(options = {})
9
+ before_action :load_resource, options.dup
10
+ end
11
+
12
+ def authorize_resource(options = {})
13
+ before_action :authorize_resource, options.dup
14
+ end
15
+
16
+ def skip_authorization(options = {})
17
+ before_action :skip_authorization_and_scope, options.dup
18
+ end
19
+
20
+ def load_and_authorize_resource(options = {})
21
+ # :nocov:
22
+ load_resource options
23
+ authorize_resource options
24
+ # :nocov:
25
+ end
26
+ end
27
+
28
+ def load_resource
29
+ scope = resource_class
30
+ action = params[:action]
31
+ varname = resource_name
32
+
33
+ if action == 'index'
34
+ varname = controller_name
35
+ resource = policy_scope resource_class
36
+
37
+ elsif action == 'new'
38
+ resource = scope.new
39
+
40
+ elsif action == 'create'
41
+ resource = scope.new
42
+ resource.attributes = resource_attributes resource, action
43
+
44
+ elsif action == 'update'
45
+ resource = scope.find params[:id]
46
+ resource.attributes = resource_attributes resource, action
47
+
48
+ elsif params[:id]
49
+ resource = scope.find params[:id]
50
+
51
+ else
52
+ resource = nil
53
+ end
54
+
55
+ instance_variable_set :"@#{varname}", resource
56
+ end
57
+
58
+ def authorize_resource
59
+ resource = resource_instance || resource_class
60
+ authorize resource
61
+ end
62
+
63
+ def skip_authorization_and_scope
64
+ action = params[:action]
65
+ skip_policy_scope if action == 'index'
66
+ skip_authorization
67
+ end
68
+
69
+ def resource_name
70
+ controller_name.singularize
71
+ end
72
+
73
+ def resource_class
74
+ resource_name.classify.constantize
75
+ end
76
+
77
+ def resource_instance
78
+ instance_variable_get :"@#{resource_name}"
79
+ end
80
+
81
+ def resource_attributes(resource, action)
82
+ if has_permitted_attributes? resource, action
83
+ return permitted_attributes resource
84
+ end
85
+
86
+ candidates = ["#{action}_params", "#{resource_name}_params"]
87
+ candidates.each do |candidate|
88
+ return send(candidate) if respond_to? candidate, true
89
+ end
90
+
91
+ {}
92
+ end
93
+
94
+ def has_permitted_attributes?(resource, action)
95
+ return true if policy(resource).respond_to? :"permitted_attributes_for_#{action}"
96
+ return true if policy(resource).respond_to? :permitted_attributes
97
+
98
+ false
99
+ end
100
+ end
101
+ end
@@ -1,3 +1,3 @@
1
1
  module PunditExtra
2
- VERSION = "0.2.0"
2
+ VERSION = '0.3.1'
3
3
  end
data/lib/pundit_extra.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "pundit_extra/controller_mixin"
2
- require "pundit_extra/helpers"
3
- require "pundit_extra/resource_autoload"
4
- require "pundit_extra/version"
1
+ require 'pundit_extra/controller_mixin'
2
+ require 'pundit_extra/helpers'
3
+ require 'pundit_extra/resource_autoload'
4
+ require 'pundit_extra/version'
metadata CHANGED
@@ -1,57 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit_extra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-24 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: combustion
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.5'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '0.5'
27
- - !ruby/object:Gem::Dependency
28
- name: runfile
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0.7'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.7'
41
- - !ruby/object:Gem::Dependency
42
- name: runfile-tasks
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.4'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.4'
11
+ date: 2024-07-29 00:00:00.000000000 Z
12
+ dependencies: []
55
13
  description: Add some helpers and additional functionality to Pundit.
56
14
  email: db@dannyben.com
57
15
  executables: []
@@ -67,8 +25,9 @@ files:
67
25
  homepage: https://github.com/DannyBen/pundit_extra
68
26
  licenses:
69
27
  - MIT
70
- metadata: {}
71
- post_install_message:
28
+ metadata:
29
+ rubygems_mfa_required: 'true'
30
+ post_install_message:
72
31
  rdoc_options: []
73
32
  require_paths:
74
33
  - lib
@@ -76,16 +35,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
35
  requirements:
77
36
  - - ">="
78
37
  - !ruby/object:Gem::Version
79
- version: 2.0.0
38
+ version: 3.0.0
80
39
  required_rubygems_version: !ruby/object:Gem::Requirement
81
40
  requirements:
82
41
  - - ">="
83
42
  - !ruby/object:Gem::Version
84
43
  version: '0'
85
44
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 2.4.6
88
- signing_key:
45
+ rubygems_version: 3.5.14
46
+ signing_key:
89
47
  specification_version: 4
90
48
  summary: Additions for Pundit
91
49
  test_files: []