pundit_extra 0.3.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: 1db5271d1f61522d4edfc7709a139fe0079e531e
4
- data.tar.gz: 15b0b99878f3b3d9160dee62be09868bfbfb304f
2
+ SHA256:
3
+ metadata.gz: 77376d6ce79786e54a8a0be60fc52fc45e643f578dcd248df97cbf6a2fdd5232
4
+ data.tar.gz: e3fa899de77740a36d984fcf7e8e50ed78fb70d3f5d8fa2b6038ac688aef87cc
5
5
  SHA512:
6
- metadata.gz: a592288a7a03b00725f4da5a32a73235f18aa418283e731c7f12fb93462860e188096e5f61b6fde98afd2539b2b0fb55856b9799d4f86ffa45c8afec44f62ed1
7
- data.tar.gz: a0713629734c444abec72b2c39af8865797213192725ebbbfa65a12d159a792e3891f661cd1d1781c3f8b67d875f35c3cb526c65794a755c1c06de1477baf8b9
6
+ metadata.gz: 000b5790efde79b36c76c3c02207019ee88b827948c1786ece090f07b2789b13f67a857ddb2b6def903cc6a22502eedb74d4dfc741bfceb8f581260c00762e25
7
+ data.tar.gz: e5d5fb8b5342c0167423f80ef2b554ab1a71fb1910061837f3ace599e0f2eda715df41514cbb2b982cc37c748c89f98301d4a4c17ed7d78da8678a57caf5b4a4
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # PunditExtra
2
2
 
3
- [![Gem](https://img.shields.io/gem/v/pundit_extra.svg?style=flat-square)](https://rubygems.org/gems/pundit_extra)
4
- [![Travis](https://img.shields.io/travis/DannyBen/pundit_extra.svg?style=flat-square)](https://travis-ci.org/DannyBen/pundit_extra)
5
- [![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/pundit_extra.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/pundit_extra)
6
- [![Gemnasium](https://img.shields.io/gemnasium/DannyBen/pundit_extra.svg?style=flat-square)](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
  ```
@@ -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,91 +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
-
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 params[:id]
45
- resource = scope.find params[:id]
46
-
47
- else
48
- resource = nil
49
- end
50
-
51
- instance_variable_set "@#{varname}", resource
52
- end
53
-
54
- def authorize_resource
55
- resource = resource_instance || resource_class
56
- authorize resource
57
- end
58
-
59
- def skip_authorization_and_scope
60
- action = params[:action]
61
- skip_policy_scope if action == 'index'
62
- skip_authorization
63
- end
64
-
65
- def resource_name
66
- controller_name.singularize
67
- end
68
-
69
- def resource_class
70
- resource_name.classify.constantize
71
- end
72
-
73
- def resource_instance
74
- instance_variable_get "@#{resource_name}"
75
- end
76
-
77
- def resource_attributes(resource, action)
78
- if has_permitted_attributes? resource, action
79
- permitted_attributes(resource)
80
- else
81
- send "#{resource_name}_params"
82
- end
83
- end
84
-
85
- def has_permitted_attributes?(resource, action)
86
- return true if policy(resource).respond_to? "permitted_attributes_for_#{action}"
87
- return true if policy(resource).respond_to? :permitted_attributes
88
- false
89
- end
90
- end
91
- 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.3.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.3.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-08-13 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.6.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: []