pragma-operation 1.5.0 → 1.6.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 +4 -4
- data/doc/02-contracts.md +39 -0
- data/doc/03-policies.md +38 -0
- data/lib/pragma/operation/authorization.rb +9 -1
- data/lib/pragma/operation/validation.rb +8 -0
- data/lib/pragma/operation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9eb3cf53720469a289a53f3a93125af42f1dce5e
|
4
|
+
data.tar.gz: 69ef331da2c7f241928fa6328d5fd5d30574d693
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebe0ec7683d681eb68b14e4b424a98dbd5454e9b051205a544d078a448bc946e8232914b98049601762d21fd5860de2ba38b7d9a50b211b07a42b82a74eb34b3
|
7
|
+
data.tar.gz: 68120660aae43abb27be4ac0ecc475ef97ff4956caf764fb9cef2d217500e5acb25b7452641ea735c1062714d470eeade194277a5587631ca58a077fb3dc3faa
|
data/doc/02-contracts.md
CHANGED
@@ -113,3 +113,42 @@ module API
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
```
|
116
|
+
|
117
|
+
If you want to run some logic after validation, you can override the `#after_validation` method
|
118
|
+
in your operation. It takes the result of the validation as its first argument:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
module API
|
122
|
+
module V1
|
123
|
+
module Post
|
124
|
+
module Operation
|
125
|
+
class Create < Pragma::Operation::Base
|
126
|
+
contract API::V1::Post::Contract::Create
|
127
|
+
|
128
|
+
def call
|
129
|
+
post = Post.new
|
130
|
+
contract = build_contract(post)
|
131
|
+
|
132
|
+
unless validate(contract)
|
133
|
+
respond_with!(
|
134
|
+
status: :unprocessable_entity,
|
135
|
+
resource: nil
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
contract.save
|
140
|
+
|
141
|
+
respond_with status: :created, resource: post
|
142
|
+
end
|
143
|
+
|
144
|
+
protected
|
145
|
+
|
146
|
+
def after_validation(result)
|
147
|
+
# ...
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
```
|
data/doc/03-policies.md
CHANGED
@@ -115,6 +115,44 @@ module API
|
|
115
115
|
end
|
116
116
|
```
|
117
117
|
|
118
|
+
If you want to run some logic after authorization, you can override the `#after_authorization` method
|
119
|
+
in your operation. It takes the result of the authorization as its first argument:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
module API
|
123
|
+
module V1
|
124
|
+
module Post
|
125
|
+
module Operation
|
126
|
+
class Create < Pragma::Operation::Base
|
127
|
+
policy API::V1::Post::Policy
|
128
|
+
|
129
|
+
def call
|
130
|
+
post = Post.new(params)
|
131
|
+
|
132
|
+
unless authorize(post)
|
133
|
+
respond_with!(
|
134
|
+
status: :forbidden,
|
135
|
+
resource: nil # if you don't need error info
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
post.save!
|
140
|
+
|
141
|
+
respond_with status: :created, resource: post
|
142
|
+
end
|
143
|
+
|
144
|
+
protected
|
145
|
+
|
146
|
+
def after_authorization(result)
|
147
|
+
# ...
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
118
156
|
## Authorizing collections
|
119
157
|
|
120
158
|
To authorize a collection, use `#authorize_collection`. This will call `.accessible_by` on the
|
@@ -73,7 +73,9 @@ module Pragma
|
|
73
73
|
policy.resource.send("#{name}=", value)
|
74
74
|
end
|
75
75
|
|
76
|
-
policy.send("#{self.class.operation_name}?")
|
76
|
+
policy.send("#{self.class.operation_name}?").tap do |result|
|
77
|
+
after_authorization result
|
78
|
+
end
|
77
79
|
end
|
78
80
|
|
79
81
|
# Authorizes this operation on the provided resource or policy. If the user is not
|
@@ -93,6 +95,12 @@ module Pragma
|
|
93
95
|
)
|
94
96
|
end
|
95
97
|
|
98
|
+
# Runs after authorization is done.
|
99
|
+
#
|
100
|
+
# @param result [Boolean] the result of the authorization
|
101
|
+
def after_authorization(result)
|
102
|
+
end
|
103
|
+
|
96
104
|
# Scopes the provided collection.
|
97
105
|
#
|
98
106
|
# If no policy class is defined, simply returns the collection.
|
@@ -72,6 +72,8 @@ module Pragma
|
|
72
72
|
contract.validate(params)
|
73
73
|
else
|
74
74
|
contract.respond_to?(:validate) ? contract.validate : true
|
75
|
+
end.tap do |result|
|
76
|
+
after_validation(result)
|
75
77
|
end
|
76
78
|
end
|
77
79
|
|
@@ -91,6 +93,12 @@ module Pragma
|
|
91
93
|
respond_with_validation_errors!(contract) unless validate(contract)
|
92
94
|
end
|
93
95
|
|
96
|
+
# Runs after validation is done.
|
97
|
+
#
|
98
|
+
# @param result [Boolean] the result of the validation
|
99
|
+
def after_validation(result)
|
100
|
+
end
|
101
|
+
|
94
102
|
# Sets a response suitable for reporting validation errors.
|
95
103
|
#
|
96
104
|
# The response will be a 422 Unprocessable Entity, contain the +error_type+, +error_message+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pragma-operation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Desantis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: interactor
|