subroutine 0.10.0.beta3 → 0.10.0.beta4

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
  SHA256:
3
- metadata.gz: ad452be684c7aa5f556a1d203c5f6d02a8780600f74276743c164391a91d6fde
4
- data.tar.gz: 3f2d5c3303bb579c3741a10e659af80b0adcb7c3a64373dcf6f7f2d82bb37703
3
+ metadata.gz: 9d7662863cde650cd9b5392a5c53c45a0b63db8f6ff5651dac88701c0e204505
4
+ data.tar.gz: 2ccdbc5c345686f95741848f72c3112363d8c2bd8d84899c2e66e4f65af10784
5
5
  SHA512:
6
- metadata.gz: aa63944122d676ca984525137e05430766b767b20fee7f65c216b45c5ecf523781c9d4e35119d17d7fd99dd58580f43e67405b72a7027886c96166103dac6bc5
7
- data.tar.gz: 21c97fa2d75c0fbbd85f691d89e90666cddf490b7dd37fc676a76543bb1b6a5c7654959a6ba2aa19640ccd658ea8f0786c405bafbb2e7f63d0ba7ce64496cd08
6
+ metadata.gz: 941c27d1b188ec739751ee9a7e396d64f63498215a1b946308dd8e70ec6537c2997312ead77ce09a2fa5f48d77211e15514d46905bb91ad8ccbaa02044d8b2f7
7
+ data.tar.gz: 02024556adf1ae6c76f16258613e18f58f23c46f11a5171201763b0d1546090710234d6d7ac08f96c330bb8bee032b4503bae72141ea01438d0efda85a48c4e5
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "subroutine/failure"
4
+
5
+ module Subroutine
6
+ module AssociationFields
7
+ class AssociationTypeMismatchError < ::Subroutine::Failure
8
+ end
9
+ end
10
+ end
@@ -3,6 +3,7 @@
3
3
  require "delegate"
4
4
  require "active_support/concern"
5
5
  require "subroutine/association_fields/configuration"
6
+ require "subroutine/association_fields/association_type_mismatch_error"
6
7
 
7
8
  module Subroutine
8
9
  module AssociationFields
@@ -86,6 +87,7 @@ module Subroutine
86
87
  config = get_field_config(field_name)
87
88
 
88
89
  if config&.behavior == :association
90
+ maybe_raise_on_type_mismatch!(config, value)
89
91
  set_field(config.foreign_type_method, value&.class&.name, opts) if config.polymorphic?
90
92
  set_field(config.foreign_key_method, value&.id, opts)
91
93
  elsif config&.behavior == :association_component
@@ -154,5 +156,19 @@ module Subroutine
154
156
  scope.find(_fk)
155
157
  end
156
158
 
159
+ def maybe_raise_on_type_mismatch!(config, record)
160
+ return if config.polymorphic?
161
+ return if record.nil?
162
+
163
+ klass = config.inferred_class_name.constantize
164
+
165
+ return if record.class <= klass || record.class >= klass
166
+
167
+ message = "#{klass}(##{klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
168
+
169
+ errors.add(:base, message)
170
+ raise Subroutine::AssociationFields::AssociationTypeMismatchError, self
171
+ end
172
+
157
173
  end
158
174
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Subroutine
4
+ module Auth
5
+ class AuthorizationNotDeclaredError < ::StandardError
6
+
7
+ def initialize(msg = nil)
8
+ super(msg || "Authorization management has not been declared on this class")
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Subroutine
4
+ module Auth
5
+ class NotAuthorizedError < ::StandardError
6
+
7
+ def initialize(msg = nil)
8
+ msg = I18n.t("errors.#{msg}", default: "Sorry, you are not authorized to perform this action.") if msg.is_a?(Symbol)
9
+ msg ||= I18n.t("errors.unauthorized", default: "Sorry, you are not authorized to perform this action.")
10
+ super msg
11
+ end
12
+
13
+ def status
14
+ 401
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -1,32 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "subroutine/auth/authorization_not_declared_error"
4
+ require "subroutine/auth/not_authorized_error"
5
+
3
6
  module Subroutine
4
7
  module Auth
5
8
 
6
9
  extend ActiveSupport::Concern
7
10
 
8
- class NotAuthorizedError < ::StandardError
9
-
10
- def initialize(msg = nil)
11
- msg = I18n.t("errors.#{msg}", default: "Sorry, you are not authorized to perform this action.") if msg.is_a?(Symbol)
12
- msg ||= I18n.t("errors.unauthorized", default: "Sorry, you are not authorized to perform this action.")
13
- super msg
14
- end
15
-
16
- def status
17
- 401
18
- end
19
-
20
- end
21
-
22
- class AuthorizationNotDeclaredError < ::StandardError
23
-
24
- def initialize(msg = nil)
25
- super(msg || "Authorization management has not been declared on this class")
26
- end
27
-
28
- end
29
-
30
11
  included do
31
12
  class_attribute :authorization_declared, instance_writer: false
32
13
  self.authorization_declared = false
@@ -5,7 +5,7 @@ module Subroutine
5
5
  MAJOR = 0
6
6
  MINOR = 10
7
7
  PATCH = 0
8
- PRE = "beta3"
8
+ PRE = "beta4"
9
9
 
10
10
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
 
@@ -13,6 +13,10 @@ module Subroutine
13
13
  @fred ||= ::User.new(id: 2, email_address: "fred@example.com")
14
14
  end
15
15
 
16
+ def account
17
+ @account ||= ::Account.new(id: 1)
18
+ end
19
+
16
20
  def test_it_sets_accessors_on_init
17
21
  op = SimpleAssociationOp.new user: doug
18
22
  assert_equal "User", op.user_type
@@ -143,5 +147,12 @@ module Subroutine
143
147
  assert_equal false, op.field_provided?(:admin_type)
144
148
  end
145
149
 
150
+ def test_it_ensures_the_correct_type_of_resource_is_provded_to_an_association
151
+ op = SimpleAssociationOp.new
152
+ assert_raises ::Subroutine::AssociationFields::AssociationTypeMismatchError do
153
+ op.user = account
154
+ end
155
+ end
156
+
146
157
  end
147
158
  end
data/test/support/ops.rb CHANGED
@@ -27,6 +27,18 @@ class AdminUser < ::User
27
27
 
28
28
  end
29
29
 
30
+ class Account
31
+
32
+ include ::ActiveModel::Model
33
+
34
+ attr_accessor :id
35
+
36
+ def self.find(id)
37
+ new(id: id)
38
+ end
39
+
40
+ end
41
+
30
42
  ## Ops ##
31
43
 
32
44
  class SignupOp < ::Subroutine::Op
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subroutine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0.beta3
4
+ version: 0.10.0.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
@@ -145,9 +145,12 @@ files:
145
145
  - gemfiles/am60.gemfile
146
146
  - lib/subroutine.rb
147
147
  - lib/subroutine/association_fields.rb
148
+ - lib/subroutine/association_fields/association_type_mismatch_error.rb
148
149
  - lib/subroutine/association_fields/component_configuration.rb
149
150
  - lib/subroutine/association_fields/configuration.rb
150
151
  - lib/subroutine/auth.rb
152
+ - lib/subroutine/auth/authorization_not_declared_error.rb
153
+ - lib/subroutine/auth/not_authorized_error.rb
151
154
  - lib/subroutine/failure.rb
152
155
  - lib/subroutine/fields.rb
153
156
  - lib/subroutine/fields/configuration.rb