well_formed-pundit 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb057708bb13973cbd0c53de24ab3192545ff35dd573eefdb0f858042046828f
4
+ data.tar.gz: f0fd614c650858f13659b23bae675c6043b91625a759e936259d8327c5b68592
5
+ SHA512:
6
+ metadata.gz: 629f94d148cf0fdee0c1348d78945f4b9f8f4db6061d5445eb4cafc3d87ee7b0fbddb70b3676f8f431be7d10290b5a77c676ce6005f972413803821e1d0e2909
7
+ data.tar.gz: 9507603657d833691d19ceee63393cba33482fe825d4921c080b9cc07c1ab9141e6be9546b7ae98ee1859d4ab8edd9bc2bd9c4b8519279d48ce2304754ece2fe
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # well_formed-pundit
2
+
3
+ [Pundit](https://github.com/varvet/pundit) authorization integration for [WellFormed](https://github.com/bmorrall/well_formed) form objects.
4
+
5
+ Adds `policy`, `authorize!`, and `policy_scope` helpers directly to any WellFormed form, using the form's built-in `resource` and `user` references.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ bundle add well_formed-pundit
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Require the gem in your application:
16
+
17
+ ```ruby
18
+ require "well_formed-pundit"
19
+ ```
20
+
21
+ `WellFormed::Pundit` is automatically included into all WellFormed forms — no `include` required.
22
+
23
+ ### `authorize!`
24
+
25
+ Raise `Pundit::NotAuthorizedError` if the user is not permitted to perform an action on the resource:
26
+
27
+ ```ruby
28
+ class CreateArticleForm < WellFormed::ResourceForm
29
+ resource_alias :article
30
+
31
+ attribute :title, :string
32
+ attribute :body, :string
33
+
34
+ validates :title, presence: true
35
+
36
+ def perform
37
+ authorize!(:create?) # authorizes resource
38
+ authorize!(parent_record, :update?) # authorizes a different record
39
+ # proceed with save ...
40
+ end
41
+ end
42
+ ```
43
+
44
+ ### `policy`
45
+
46
+ Access the resolved Pundit policy instance directly. Defaults to the form's `resource`, but accepts an optional record argument:
47
+
48
+ ```ruby
49
+ form.policy # => ArticlePolicy for resource
50
+ form.policy.create? # => true / false
51
+ form.policy(other) # => policy resolved for a different record
52
+ ```
53
+
54
+ ### `policy_scope`
55
+
56
+ Resolve a scoped collection for the current user:
57
+
58
+ ```ruby
59
+ articles = form.policy_scope(Article.all)
60
+ ```
61
+
62
+ ## API
63
+
64
+ | Method | Description |
65
+ |--------|-------------|
66
+ | `policy(record = resource)` | Returns the Pundit policy instance for `record` and `user` |
67
+ | `authorize!(query)` | Raises `Pundit::NotAuthorizedError` unless the user is authorized for `resource` |
68
+ | `authorize!(record, query)` | Raises `Pundit::NotAuthorizedError` unless the user is authorized for `record` |
69
+ | `policy_scope(collection)` | Returns the policy scope resolved for `user` |
70
+
71
+ ## License
72
+
73
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new(:rubocop)
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WellFormed
4
+ module Pundit
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WellFormed
4
+ module Pundit
5
+ # Returns the Pundit policy instance for the given record (defaults to resource).
6
+ def policy(record = resource)
7
+ ::Pundit::PolicyFinder.new(record).policy!.new(user, record)
8
+ end
9
+
10
+ # Returns the scoped collection for the current user.
11
+ def policy_scope(collection)
12
+ ::Pundit::PolicyFinder.new(collection).scope!.new(user, collection).resolve
13
+ end
14
+
15
+ # Raises Pundit::NotAuthorizedError if the user is not authorized for the given query.
16
+ # Optionally pass an explicit record as the first argument; defaults to resource.
17
+ def authorize!(record_or_query, query = nil)
18
+ if query.nil?
19
+ ::Pundit.authorize(user, resource, record_or_query)
20
+ else
21
+ ::Pundit.authorize(user, record_or_query, query)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "well_formed"
4
+ require "pundit"
5
+ require_relative "well_formed/pundit/version"
6
+ require_relative "well_formed/pundit"
7
+
8
+ WellFormed::WithUser.register_extension(WellFormed::Pundit)
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: well_formed-pundit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Morrall
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: well_formed
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.1.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 0.1.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: pundit
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ email:
41
+ - bemo56@hotmail.com
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - ".rspec"
47
+ - README.md
48
+ - Rakefile
49
+ - lib/well_formed-pundit.rb
50
+ - lib/well_formed/pundit.rb
51
+ - lib/well_formed/pundit/version.rb
52
+ homepage: https://github.com/bmorrall/well_formed
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.1.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.6.9
71
+ specification_version: 4
72
+ summary: Pundit authorization integration for well_formed
73
+ test_files: []