subroutine 0.6.3 → 0.7.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/lib/subroutine/association.rb +10 -15
- data/lib/subroutine/auth.rb +10 -15
- data/lib/subroutine/failure.rb +3 -3
- data/lib/subroutine/filtered_errors.rb +3 -2
- data/lib/subroutine/op.rb +19 -20
- data/lib/subroutine/output_not_set_error.rb +2 -1
- data/lib/subroutine/type_caster.rb +51 -25
- data/lib/subroutine/unknown_output_error.rb +2 -1
- data/lib/subroutine/version.rb +4 -4
- data/lib/subroutine.rb +4 -2
- data/test/subroutine/association_test.rb +16 -16
- data/test/subroutine/auth_test.rb +7 -7
- data/test/subroutine/base_test.rb +49 -50
- data/test/subroutine/type_caster_test.rb +59 -31
- data/test/support/ops.rb +28 -41
- data/test/test_helper.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7416fb00f0a0322ec831337e3afaf019d21cc83f4573afa13e8485a64ffb8e7c
|
4
|
+
data.tar.gz: 0d40d5a0843b57c35e046ecfcaedcbf4471386ec64a01577723c8717d98ebdc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 449ff84a202510087c5ab59a6f1809d1b59a1ec77f7e496fb37a6d93369bd5f1a1589fb102bc739763f3c1f3a74614558bb2c70d2ba17448f7df3398a2b1b498
|
7
|
+
data.tar.gz: 6e779574462dc657ce38422a094c556bcf36171698f8c02e165ce850e8aef0e79a7030e1c614332965efc4399c1fc7f8cbd33dc7570d7c92b8e8b5bb562f36a1
|
@@ -1,18 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Subroutine
|
2
4
|
module Association
|
3
|
-
|
4
5
|
def self.included(base)
|
5
6
|
base.send :extend, ::Subroutine::Association::ClassMethods
|
6
7
|
class << base
|
7
8
|
alias_method :field_without_associations, :field
|
8
9
|
alias_method :field, :field_with_associations
|
9
10
|
end
|
10
|
-
|
11
11
|
end
|
12
12
|
|
13
13
|
module ClassMethods
|
14
|
-
|
15
|
-
|
16
14
|
def field_with_associations(*args)
|
17
15
|
opts = args.extract_options!
|
18
16
|
if opts[:association]
|
@@ -47,9 +45,8 @@ module Subroutine
|
|
47
45
|
# - unscoped => set true if the record should be looked up via Type.unscoped
|
48
46
|
|
49
47
|
def association(field, options = {})
|
50
|
-
|
51
48
|
if options[:as] && options[:foreign_key]
|
52
|
-
raise ArgumentError
|
49
|
+
raise ArgumentError, ':as and :foreign_key options should be provided together to an association invocation'
|
53
50
|
end
|
54
51
|
|
55
52
|
class_name = options[:class_name]
|
@@ -61,13 +58,12 @@ module Subroutine
|
|
61
58
|
klass = class_name.to_s if class_name
|
62
59
|
|
63
60
|
foreign_key_method = (options[:foreign_key] || "#{field}_id").to_s
|
64
|
-
foreign_type_method = foreign_key_method.gsub(/_id$/,
|
65
|
-
|
61
|
+
foreign_type_method = foreign_key_method.gsub(/_id$/, '_type')
|
66
62
|
|
67
63
|
if poly
|
68
64
|
string foreign_type_method
|
69
65
|
else
|
70
|
-
class_eval <<-EV, __FILE__, __LINE__+1
|
66
|
+
class_eval <<-EV, __FILE__, __LINE__ + 1
|
71
67
|
def #{foreign_type_method}
|
72
68
|
#{as.to_s.camelize.inspect}
|
73
69
|
end
|
@@ -78,7 +74,7 @@ module Subroutine
|
|
78
74
|
|
79
75
|
field_without_associations as, options.merge(association: true)
|
80
76
|
|
81
|
-
class_eval <<-EV, __FILE__, __LINE__+1
|
77
|
+
class_eval <<-EV, __FILE__, __LINE__ + 1
|
82
78
|
def #{as}_with_association
|
83
79
|
return @#{as} if defined?(@#{as})
|
84
80
|
@#{as} = begin
|
@@ -89,7 +85,7 @@ module Subroutine
|
|
89
85
|
|
90
86
|
def #{as}_with_association=(r)
|
91
87
|
@#{as} = r
|
92
|
-
#{poly || klass ? "params['#{foreign_type_method}'] = r.nil? ? nil : #{klass.nil? ?
|
88
|
+
#{poly || klass ? "params['#{foreign_type_method}'] = r.nil? ? nil : #{klass.nil? ? 'r.class.name' : klass.to_s.inspect}" : ''}
|
93
89
|
params['#{foreign_key_method}'] = r.nil? ? nil : r.id
|
94
90
|
r
|
95
91
|
end
|
@@ -100,16 +96,16 @@ module Subroutine
|
|
100
96
|
|
101
97
|
alias_method :"#{as}_without_association=", :"#{as}="
|
102
98
|
alias_method :"#{as}=", :"#{as}_with_association="
|
103
|
-
|
104
99
|
end
|
105
100
|
end
|
106
101
|
|
107
102
|
def initialize(*args)
|
108
103
|
super(*args)
|
109
104
|
|
110
|
-
|
105
|
+
_fields.each_pair do |field, config|
|
111
106
|
next unless config[:association]
|
112
|
-
next unless @original_params.
|
107
|
+
next unless @original_params.key?(field)
|
108
|
+
|
113
109
|
send("#{field}=", @original_params[field]) # this gets the _id and _type into the params hash
|
114
110
|
end
|
115
111
|
end
|
@@ -127,6 +123,5 @@ module Subroutine
|
|
127
123
|
|
128
124
|
scope.find(_id)
|
129
125
|
end
|
130
|
-
|
131
126
|
end
|
132
127
|
end
|
data/lib/subroutine/auth.rb
CHANGED
@@ -1,23 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Subroutine
|
2
4
|
module Auth
|
3
|
-
|
4
5
|
class NotAuthorizedError < ::StandardError
|
5
|
-
|
6
6
|
def initialize(msg = nil)
|
7
|
-
msg = I18n.t("errors.#{msg}", default:
|
8
|
-
msg ||= I18n.t('errors.unauthorized', default:
|
7
|
+
msg = I18n.t("errors.#{msg}", default: 'Sorry, you are not authorized to perform this action.') if msg.is_a?(Symbol)
|
8
|
+
msg ||= I18n.t('errors.unauthorized', default: 'Sorry, you are not authorized to perform this action.')
|
9
9
|
super msg
|
10
10
|
end
|
11
11
|
|
12
12
|
def status
|
13
13
|
401
|
14
14
|
end
|
15
|
-
|
16
15
|
end
|
17
16
|
|
18
17
|
class AuthorizationNotDeclaredError < ::StandardError
|
19
18
|
def initialize(msg = nil)
|
20
|
-
super(msg ||
|
19
|
+
super(msg || 'Authorization management has not been declared on this class')
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
@@ -30,9 +29,7 @@ module Subroutine
|
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
|
-
|
34
32
|
module ClassMethods
|
35
|
-
|
36
33
|
def authorize(validation_name)
|
37
34
|
validate validation_name, unless: :skip_auth_checks?
|
38
35
|
end
|
@@ -66,7 +63,7 @@ module Subroutine
|
|
66
63
|
policy_name = opts[:policy] || :policy
|
67
64
|
|
68
65
|
if_conditionals = Array(opts[:if])
|
69
|
-
unless_conditionals = Array(
|
66
|
+
unless_conditionals = Array(opts[:unless])
|
70
67
|
|
71
68
|
validate unless: :skip_auth_checks? do
|
72
69
|
run_it = true
|
@@ -84,17 +81,16 @@ module Subroutine
|
|
84
81
|
|
85
82
|
next unless run_it
|
86
83
|
|
87
|
-
p =
|
88
|
-
if !p || meths.any?{|m| !(p.respond_to?("#{m}?") ? p.send("#{m}?") : p.send(m)) }
|
84
|
+
p = send(policy_name)
|
85
|
+
if !p || meths.any? { |m| !(p.respond_to?("#{m}?") ? p.send("#{m}?") : p.send(m)) }
|
89
86
|
unauthorized! opts[:error]
|
90
87
|
end
|
91
88
|
end
|
92
89
|
end
|
93
|
-
|
94
90
|
end
|
95
91
|
|
96
92
|
def initialize(*args)
|
97
|
-
raise Subroutine::Auth::AuthorizationNotDeclaredError
|
93
|
+
raise Subroutine::Auth::AuthorizationNotDeclaredError unless self.class.authorization_declared
|
98
94
|
|
99
95
|
super(args.extract_options!)
|
100
96
|
@skip_auth_checks = false
|
@@ -117,8 +113,7 @@ module Subroutine
|
|
117
113
|
|
118
114
|
def unauthorized!(reason = nil)
|
119
115
|
reason ||= :unauthorized
|
120
|
-
raise ::Subroutine::Auth::NotAuthorizedError
|
116
|
+
raise ::Subroutine::Auth::NotAuthorizedError, reason
|
121
117
|
end
|
122
|
-
|
123
118
|
end
|
124
119
|
end
|
data/lib/subroutine/failure.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module Subroutine
|
3
4
|
class Failure < StandardError
|
4
5
|
attr_reader :record
|
5
6
|
def initialize(record)
|
6
7
|
@record = record
|
7
|
-
errors = @record.errors.full_messages.join(
|
8
|
+
errors = @record.errors.full_messages.join(', ')
|
8
9
|
super(errors)
|
9
10
|
end
|
10
11
|
end
|
11
|
-
|
12
12
|
end
|
data/lib/subroutine/op.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/core_ext/hash/indifferent_access'
|
2
4
|
require 'active_support/core_ext/object/duplicable'
|
3
5
|
require 'active_support/core_ext/object/deep_dup'
|
@@ -86,9 +88,9 @@ module Subroutine
|
|
86
88
|
|
87
89
|
def inherited(child)
|
88
90
|
super
|
89
|
-
child._fields =
|
90
|
-
child._error_map =
|
91
|
-
child._error_ignores =
|
91
|
+
child._fields = _fields.dup
|
92
|
+
child._error_map = _error_map.dup
|
93
|
+
child._error_ignores = _error_ignores.dup
|
92
94
|
end
|
93
95
|
|
94
96
|
def submit!(*args)
|
@@ -121,7 +123,7 @@ module Subroutine
|
|
121
123
|
|
122
124
|
def #{field_name}=(v)
|
123
125
|
config = #{field_name}_config
|
124
|
-
v = ::Subroutine::TypeCaster.cast(v, config
|
126
|
+
v = ::Subroutine::TypeCaster.cast(v, config)
|
125
127
|
@params["#{field_name}"] = v
|
126
128
|
end
|
127
129
|
|
@@ -134,16 +136,13 @@ module Subroutine
|
|
134
136
|
end
|
135
137
|
|
136
138
|
EV
|
137
|
-
|
138
139
|
end
|
139
140
|
|
140
141
|
def _ignore_errors(field_name)
|
141
142
|
_error_ignores[field_name.to_sym] = true
|
142
143
|
end
|
143
|
-
|
144
144
|
end
|
145
145
|
|
146
|
-
|
147
146
|
class_attribute :_outputs
|
148
147
|
self._outputs = {}
|
149
148
|
|
@@ -159,9 +158,8 @@ module Subroutine
|
|
159
158
|
attr_reader :original_params
|
160
159
|
attr_reader :params, :defaults
|
161
160
|
|
162
|
-
|
163
161
|
def initialize(inputs = {})
|
164
|
-
@original_params
|
162
|
+
@original_params = inputs.with_indifferent_access
|
165
163
|
@params = sanitize_params(@original_params)
|
166
164
|
@defaults = sanitize_defaults
|
167
165
|
@outputs = {}
|
@@ -173,13 +171,13 @@ module Subroutine
|
|
173
171
|
|
174
172
|
def output(name, value)
|
175
173
|
unless _outputs.key?(name.to_sym)
|
176
|
-
raise ::Subroutine::UnknownOutputError
|
174
|
+
raise ::Subroutine::UnknownOutputError, name
|
177
175
|
end
|
176
|
+
|
178
177
|
@outputs[name.to_sym] = value
|
179
178
|
end
|
180
179
|
|
181
180
|
def submit!
|
182
|
-
|
183
181
|
begin
|
184
182
|
observe_submission do
|
185
183
|
validate_and_perform
|
@@ -197,13 +195,13 @@ module Subroutine
|
|
197
195
|
if errors.empty?
|
198
196
|
_outputs.each_pair do |name, config|
|
199
197
|
if config[:required] && !@outputs.key?(name)
|
200
|
-
raise ::Subroutine::OutputNotSetError
|
198
|
+
raise ::Subroutine::OutputNotSetError, name
|
201
199
|
end
|
202
200
|
end
|
203
201
|
|
204
202
|
true
|
205
203
|
else
|
206
|
-
raise ::Subroutine::Failure
|
204
|
+
raise ::Subroutine::Failure, self
|
207
205
|
end
|
208
206
|
end
|
209
207
|
|
@@ -239,10 +237,10 @@ module Subroutine
|
|
239
237
|
end
|
240
238
|
|
241
239
|
def validate_and_perform
|
242
|
-
bool = observe_validation{ valid? }
|
240
|
+
bool = observe_validation { valid? }
|
243
241
|
return false unless bool
|
244
242
|
|
245
|
-
observe_perform{ perform }
|
243
|
+
observe_perform { perform }
|
246
244
|
end
|
247
245
|
|
248
246
|
# implement this in your concrete class.
|
@@ -260,8 +258,7 @@ module Subroutine
|
|
260
258
|
def inherit_errors(error_object)
|
261
259
|
error_object = error_object.errors if error_object.respond_to?(:errors)
|
262
260
|
|
263
|
-
error_object.each do |k,v|
|
264
|
-
|
261
|
+
error_object.each do |k, v|
|
265
262
|
next if _error_ignores[k.to_sym]
|
266
263
|
|
267
264
|
if respond_to?(k)
|
@@ -269,7 +266,7 @@ module Subroutine
|
|
269
266
|
elsif _error_map[k.to_sym]
|
270
267
|
errors.add(_error_map[k.to_sym], v)
|
271
268
|
else
|
272
|
-
errors.add(:base, error_object.full_message(k,v))
|
269
|
+
errors.add(:base, error_object.full_message(k, v))
|
273
270
|
end
|
274
271
|
end
|
275
272
|
|
@@ -282,7 +279,8 @@ module Subroutine
|
|
282
279
|
out = {}.with_indifferent_access
|
283
280
|
_fields.each_pair do |field, config|
|
284
281
|
next unless inputs.key?(field)
|
285
|
-
|
282
|
+
|
283
|
+
out[field] = ::Subroutine::TypeCaster.cast(inputs[field], config)
|
286
284
|
end
|
287
285
|
|
288
286
|
out
|
@@ -293,6 +291,7 @@ module Subroutine
|
|
293
291
|
|
294
292
|
_fields.each_pair do |field, config|
|
295
293
|
next if config[:default].nil?
|
294
|
+
|
296
295
|
deflt = config[:default]
|
297
296
|
if deflt.respond_to?(:call)
|
298
297
|
deflt = deflt.call
|
@@ -301,7 +300,7 @@ module Subroutine
|
|
301
300
|
# the class global default value, and potentially modify it.
|
302
301
|
deflt = deflt.deep_dup # from active_support
|
303
302
|
end
|
304
|
-
defaults[field] = ::Subroutine::TypeCaster.cast(deflt, config
|
303
|
+
defaults[field] = ::Subroutine::TypeCaster.cast(deflt, config)
|
305
304
|
end
|
306
305
|
|
307
306
|
defaults
|
@@ -1,60 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'date'
|
2
4
|
require 'time'
|
3
5
|
require 'bigdecimal'
|
6
|
+
require 'securerandom'
|
4
7
|
require 'active_support/core_ext/object/blank'
|
5
8
|
require 'active_support/core_ext/object/try'
|
6
9
|
require 'active_support/core_ext/array/wrap'
|
7
10
|
|
8
11
|
module Subroutine
|
9
12
|
module TypeCaster
|
10
|
-
|
11
|
-
def casters
|
13
|
+
def self.casters
|
12
14
|
@casters ||= {}
|
13
15
|
end
|
14
|
-
module_function :casters
|
15
16
|
|
16
|
-
def register(*names, &block)
|
17
|
+
def self.register(*names, &block)
|
17
18
|
names.each do |n|
|
18
19
|
casters[n] = block
|
19
20
|
end
|
20
21
|
end
|
21
|
-
module_function :register
|
22
22
|
|
23
|
-
def cast(value,
|
23
|
+
def self.cast(value, options = {})
|
24
|
+
type = options[:type]
|
24
25
|
return value if value.nil? || type.nil?
|
25
26
|
|
26
27
|
caster = casters[type]
|
27
|
-
|
28
|
-
end
|
29
|
-
module_function :cast
|
28
|
+
return value unless caster
|
30
29
|
|
30
|
+
caster.call(value, options)
|
31
|
+
end
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
|
-
Subroutine::TypeCaster.register :number, :float do |value,
|
35
|
+
::Subroutine::TypeCaster.register :number, :float do |value, options = {}|
|
35
36
|
next nil if value.blank?
|
36
|
-
|
37
|
+
|
38
|
+
meth = (options[:methods] || []).detect { |m| value.respond_to?(m) }
|
37
39
|
meth ? value.send(meth) : value.to_f
|
38
40
|
end
|
39
41
|
|
40
|
-
Subroutine::TypeCaster.register :integer, :int, :epoch do |value|
|
41
|
-
Subroutine::TypeCaster.cast(value, :number, :to_i)
|
42
|
+
::Subroutine::TypeCaster.register :integer, :int, :epoch do |value, _options = {}|
|
43
|
+
::Subroutine::TypeCaster.cast(value, type: :number, methods: [:to_i])
|
42
44
|
end
|
43
45
|
|
44
|
-
Subroutine::TypeCaster.register :decimal, :big_decimal do |value|
|
45
|
-
Subroutine::TypeCaster.cast(value, :number, :to_d, :to_f)
|
46
|
+
::Subroutine::TypeCaster.register :decimal, :big_decimal do |value, _options = {}|
|
47
|
+
::Subroutine::TypeCaster.cast(value, type: :number, methods: [:to_d, :to_f])
|
46
48
|
end
|
47
49
|
|
48
|
-
Subroutine::TypeCaster.register :string, :text do |value|
|
50
|
+
::Subroutine::TypeCaster.register :string, :text do |value, _options = {}|
|
49
51
|
String(value)
|
50
52
|
end
|
51
53
|
|
52
|
-
Subroutine::TypeCaster.register :boolean, :bool do |value|
|
54
|
+
::Subroutine::TypeCaster.register :boolean, :bool do |value, _options = {}|
|
53
55
|
!!(String(value) =~ /^(yes|true|1|ok)$/)
|
54
56
|
end
|
55
57
|
|
56
|
-
Subroutine::TypeCaster.register :iso_date do |value|
|
58
|
+
::Subroutine::TypeCaster.register :iso_date do |value, _options = {}|
|
57
59
|
next nil unless value.present?
|
60
|
+
|
58
61
|
d = nil
|
59
62
|
d ||= value if value.is_a?(::Date)
|
60
63
|
d ||= value if value.try(:acts_like?, :date)
|
@@ -62,8 +65,9 @@ Subroutine::TypeCaster.register :iso_date do |value|
|
|
62
65
|
d.iso8601
|
63
66
|
end
|
64
67
|
|
65
|
-
Subroutine::TypeCaster.register :iso_time do |value|
|
68
|
+
::Subroutine::TypeCaster.register :iso_time do |value, _options = {}|
|
66
69
|
next nil unless value.present?
|
70
|
+
|
67
71
|
t = nil
|
68
72
|
t ||= value if value.is_a?(::Time)
|
69
73
|
t ||= value if value.try(:acts_like?, :time)
|
@@ -71,20 +75,24 @@ Subroutine::TypeCaster.register :iso_time do |value|
|
|
71
75
|
t.utc.iso8601
|
72
76
|
end
|
73
77
|
|
74
|
-
Subroutine::TypeCaster.register :date do |value|
|
78
|
+
::Subroutine::TypeCaster.register :date do |value, _options = {}|
|
75
79
|
next nil unless value.present?
|
80
|
+
|
76
81
|
::Date.parse(String(value))
|
77
82
|
end
|
78
83
|
|
79
|
-
Subroutine::TypeCaster.register :time, :timestamp, :datetime do |value|
|
84
|
+
::Subroutine::TypeCaster.register :time, :timestamp, :datetime do |value, _options = {}|
|
80
85
|
next nil unless value.present?
|
86
|
+
|
81
87
|
::Time.parse(String(value))
|
82
88
|
end
|
83
89
|
|
84
|
-
Subroutine::TypeCaster.register :hash, :object, :hashmap, :dict do |value|
|
90
|
+
::Subroutine::TypeCaster.register :hash, :object, :hashmap, :dict do |value, _options = {}|
|
85
91
|
if value.class.name == 'ActionController::Parameters'
|
86
92
|
value = value.to_hash
|
87
|
-
value.each_pair
|
93
|
+
value.each_pair do |k, v|
|
94
|
+
value[k] = ::Subroutine::TypeCaster.cast(v, type: :hash) if v.class.name == 'ActionController::Parameters'
|
95
|
+
end
|
88
96
|
next value
|
89
97
|
end
|
90
98
|
|
@@ -93,10 +101,28 @@ Subroutine::TypeCaster.register :hash, :object, :hashmap, :dict do |value|
|
|
93
101
|
next value.to_hash if value.respond_to?(:to_hash)
|
94
102
|
next value.to_h if value.respond_to?(:to_h)
|
95
103
|
next ::Hash[value.to_a] if value.respond_to?(:to_a)
|
104
|
+
|
96
105
|
{}
|
97
106
|
end
|
98
107
|
|
99
|
-
Subroutine::TypeCaster.register :array do |value|
|
108
|
+
::Subroutine::TypeCaster.register :array do |value, options = {}|
|
100
109
|
next [] if value.blank?
|
101
|
-
|
110
|
+
|
111
|
+
out = ::Array.wrap(value)
|
112
|
+
out = out.map { |v| ::Subroutine::TypeCaster.cast(v, type: options[:of]) } if options[:of]
|
113
|
+
out
|
114
|
+
end
|
115
|
+
|
116
|
+
::Subroutine::TypeCaster.register :file do |value, options = {}|
|
117
|
+
next nil if value.blank?
|
118
|
+
|
119
|
+
next value if defined?(::Tempfile) && value.is_a?(::Tempfile)
|
120
|
+
next value if value.is_a?(::File)
|
121
|
+
|
122
|
+
value = ::Base64.decode64(value) if options[:base64]
|
123
|
+
|
124
|
+
::Tempfile.new(SecureRandom.hex).tap do |f|
|
125
|
+
f.write(value)
|
126
|
+
f.rewind
|
127
|
+
end
|
102
128
|
end
|
data/lib/subroutine/version.rb
CHANGED
data/lib/subroutine.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
module Subroutine
|
4
6
|
class AuthTest < TestCase
|
5
|
-
|
6
7
|
def doug
|
7
|
-
@doug ||= ::User.new(id: 1, email_address:
|
8
|
+
@doug ||= ::User.new(id: 1, email_address: 'doug@example.com')
|
8
9
|
end
|
9
10
|
|
10
11
|
def fred
|
11
|
-
@fred ||= ::User.new(id: 2, email_address:
|
12
|
+
@fred ||= ::User.new(id: 2, email_address: 'fred@example.com')
|
12
13
|
end
|
13
14
|
|
14
15
|
def test_it_sets_accessors_on_init
|
15
16
|
op = SimpleAssociationOp.new user: doug
|
16
|
-
assert_equal
|
17
|
+
assert_equal 'User', op.user_type
|
17
18
|
assert_equal doug.id, op.user_id
|
18
19
|
end
|
19
20
|
|
@@ -23,7 +24,7 @@ module Subroutine
|
|
23
24
|
::User.expects(:all).returns(all_mock)
|
24
25
|
all_mock.expects(:find).with(1).returns(doug)
|
25
26
|
|
26
|
-
op = SimpleAssociationOp.new user_type:
|
27
|
+
op = SimpleAssociationOp.new user_type: 'User', user_id: doug.id
|
27
28
|
assert_equal doug, op.user
|
28
29
|
end
|
29
30
|
|
@@ -33,7 +34,7 @@ module Subroutine
|
|
33
34
|
::User.expects(:all).returns(all_mock)
|
34
35
|
all_mock.expects(:find).with(1).returns(doug)
|
35
36
|
|
36
|
-
op = SimpleAssociationOp.new user_type:
|
37
|
+
op = SimpleAssociationOp.new user_type: 'users', user_id: doug.id
|
37
38
|
assert_equal doug, op.user
|
38
39
|
end
|
39
40
|
|
@@ -45,7 +46,7 @@ module Subroutine
|
|
45
46
|
all_mock.expects(:unscoped).returns(unscoped_mock)
|
46
47
|
unscoped_mock.expects(:find).with(1).returns(doug)
|
47
48
|
|
48
|
-
op = UnscopedSimpleAssociationOp.new user_type:
|
49
|
+
op = UnscopedSimpleAssociationOp.new user_type: 'User', user_id: doug.id
|
49
50
|
assert_equal doug, op.user
|
50
51
|
end
|
51
52
|
|
@@ -55,13 +56,13 @@ module Subroutine
|
|
55
56
|
::AdminUser.expects(:all).returns(all_mock)
|
56
57
|
all_mock.expects(:find).with(1).returns(doug)
|
57
58
|
|
58
|
-
op = PolymorphicAssociationOp.new(admin_type:
|
59
|
+
op = PolymorphicAssociationOp.new(admin_type: 'AdminUser', admin_id: doug.id)
|
59
60
|
assert_equal doug, op.admin
|
60
61
|
end
|
61
62
|
|
62
63
|
def test_it_allows_the_class_to_be_set
|
63
64
|
op = ::AssociationWithClassOp.new(admin: doug)
|
64
|
-
assert_equal
|
65
|
+
assert_equal 'AdminUser', op.admin_type
|
65
66
|
end
|
66
67
|
|
67
68
|
def test_it_inherits_associations_via_inputs_from
|
@@ -70,9 +71,9 @@ module Subroutine
|
|
70
71
|
::User.expects(:all).returns(all_mock)
|
71
72
|
all_mock.expects(:find).with(1).returns(doug)
|
72
73
|
|
73
|
-
op = ::InheritedSimpleAssociation.new(user_type:
|
74
|
+
op = ::InheritedSimpleAssociation.new(user_type: 'User', user_id: doug.id)
|
74
75
|
assert_equal doug, op.user
|
75
|
-
assert_equal
|
76
|
+
assert_equal 'User', op.user_type
|
76
77
|
assert_equal doug.id, op.user_id
|
77
78
|
end
|
78
79
|
|
@@ -84,9 +85,9 @@ module Subroutine
|
|
84
85
|
all_mock.expects(:unscoped).returns(unscoped_mock)
|
85
86
|
unscoped_mock.expects(:find).with(1).returns(doug)
|
86
87
|
|
87
|
-
op = ::InheritedUnscopedAssociation.new(user_type:
|
88
|
+
op = ::InheritedUnscopedAssociation.new(user_type: 'User', user_id: doug.id)
|
88
89
|
assert_equal doug, op.user
|
89
|
-
assert_equal
|
90
|
+
assert_equal 'User', op.user_type
|
90
91
|
assert_equal doug.id, op.user_id
|
91
92
|
end
|
92
93
|
|
@@ -96,11 +97,10 @@ module Subroutine
|
|
96
97
|
::AdminUser.expects(:all).returns(all_mock)
|
97
98
|
all_mock.expects(:find).with(1).returns(doug)
|
98
99
|
|
99
|
-
op = ::InheritedPolymorphicAssociationOp.new(admin_type:
|
100
|
+
op = ::InheritedPolymorphicAssociationOp.new(admin_type: 'AdminUser', admin_id: doug.id)
|
100
101
|
assert_equal doug, op.admin
|
101
|
-
assert_equal
|
102
|
+
assert_equal 'AdminUser', op.admin_type
|
102
103
|
assert_equal doug.id, op.admin_id
|
103
104
|
end
|
104
|
-
|
105
105
|
end
|
106
106
|
end
|