subroutine 0.10.0.beta3 → 0.10.0.beta4
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/lib/subroutine/association_fields/association_type_mismatch_error.rb +10 -0
- data/lib/subroutine/association_fields.rb +16 -0
- data/lib/subroutine/auth/authorization_not_declared_error.rb +13 -0
- data/lib/subroutine/auth/not_authorized_error.rb +19 -0
- data/lib/subroutine/auth.rb +3 -22
- data/lib/subroutine/version.rb +1 -1
- data/test/subroutine/association_test.rb +11 -0
- data/test/support/ops.rb +12 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d7662863cde650cd9b5392a5c53c45a0b63db8f6ff5651dac88701c0e204505
|
4
|
+
data.tar.gz: 2ccdbc5c345686f95741848f72c3112363d8c2bd8d84899c2e66e4f65af10784
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 941c27d1b188ec739751ee9a7e396d64f63498215a1b946308dd8e70ec6537c2997312ead77ce09a2fa5f48d77211e15514d46905bb91ad8ccbaa02044d8b2f7
|
7
|
+
data.tar.gz: 02024556adf1ae6c76f16258613e18f58f23c46f11a5171201763b0d1546090710234d6d7ac08f96c330bb8bee032b4503bae72141ea01438d0efda85a48c4e5
|
@@ -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
|
data/lib/subroutine/auth.rb
CHANGED
@@ -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
|
data/lib/subroutine/version.rb
CHANGED
@@ -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
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.
|
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
|