pragma-operation 1.5.0 → 1.6.0

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
2
  SHA1:
3
- metadata.gz: 0d6cc18cf80b23f1275cdea3bdbdb5f898ab18bc
4
- data.tar.gz: 432bbd348981310bff295afb60c4e188d6767f4d
3
+ metadata.gz: 9eb3cf53720469a289a53f3a93125af42f1dce5e
4
+ data.tar.gz: 69ef331da2c7f241928fa6328d5fd5d30574d693
5
5
  SHA512:
6
- metadata.gz: 34357046619e0302eacf52f6143c72eab02324a8ce781eb1f400afcb315f06cba2ef9aa469c67a68174fb117c8d281410458c27cc078f909c57a4f4768918a1d
7
- data.tar.gz: 530f950bb5901d6436edb3954ab4553483b6f4375c8f37fa36dfe5805e2595dca6885295c7d55aa3f130e911932f08ca999bebafd87e71b36ac9b5a726604312
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+
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Pragma
3
3
  module Operation
4
- VERSION = '1.5.0'
4
+ VERSION = '1.6.0'
5
5
  end
6
6
  end
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.5.0
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-19 00:00:00.000000000 Z
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: interactor