factory_burgers 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/factory_burgers/builder.rb +31 -8
- data/lib/factory_burgers/errors.rb +7 -0
- data/lib/factory_burgers/middleware/build.rb +9 -19
- data/lib/factory_burgers/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f976d301bd92dab1483eae6443b7aff5a8f96ff417a24a79bb9a03568c79028
|
4
|
+
data.tar.gz: 6a5718377d57f7c9fe72ab91cab29a7bc156e348ffe56cfd3401b7e4c87fd0c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bf91ca8dcb5eeb240ba57f3c1e7a1e00e6b0c74e1ba8af1127f9fea4774f96b9edfadbbdcec1424df66b4bdac1194db0a1a1825410baee8e460c7d6dd7cb1e8
|
7
|
+
data.tar.gz: 3d063a6dcb7fc933f712225b36c3992250688ad48fbe2a1ad67e504ef9b9a7381369f88193769f93323fe4eafbd280dd4953a630ce5af1c67cd548cabf6f5c39
|
@@ -1,26 +1,44 @@
|
|
1
1
|
module FactoryBurgers
|
2
2
|
# Build resources from specified factories, traits, and attributes
|
3
3
|
class Builder
|
4
|
+
attr_reader :owner
|
5
|
+
|
6
|
+
# `owner` is an optional resource that we can attach new resources to
|
7
|
+
# E.g. a `User` for whom we wish to build a `Post`.
|
8
|
+
def initialize(owner = nil)
|
9
|
+
@owner = owner
|
10
|
+
end
|
11
|
+
|
4
12
|
# TODO: clean up method signature
|
5
|
-
def build(factory, traits, attributes,
|
13
|
+
def build(factory, traits, attributes, as: nil) # rubocop:disable Naming/MethodParameterName; as is a good name here
|
6
14
|
resource = insistently do
|
7
15
|
FactoryBot.create(factory, *traits, attributes)
|
8
16
|
end
|
9
|
-
|
17
|
+
update_assocation_to_owner(as, resource) if owner && as
|
10
18
|
|
11
19
|
return resource
|
12
20
|
end
|
13
21
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
22
|
+
private
|
23
|
+
|
24
|
+
def update_assocation_to_owner(association_name, resource)
|
25
|
+
reflection = get_owner_association(association_name)
|
26
|
+
case reflection
|
27
|
+
when ActiveRecord::Reflection::BelongsToReflection, ActiveRecord::Reflection::HasOneReflection
|
28
|
+
# This will update the foreign key on `resource` for has_one.
|
29
|
+
owner.update!(association_name => resource)
|
30
|
+
when ActiveRecord::Reflection::HasManyReflection
|
31
|
+
# This will update the foreign key on `resource`.
|
32
|
+
owner.public_send(association_name) << resource
|
19
33
|
else
|
20
|
-
|
34
|
+
invalid_association(reflection)
|
21
35
|
end
|
22
36
|
end
|
23
37
|
|
38
|
+
def get_owner_association(name)
|
39
|
+
owner.class.reflections[name.to_s] or raise "Could not find association named #{name}"
|
40
|
+
end
|
41
|
+
|
24
42
|
# Brute force through sequences (e.g. account name) to overcome uniqueness validations
|
25
43
|
def insistently(tries = 30)
|
26
44
|
tries.times do |attempt|
|
@@ -29,5 +47,10 @@ module FactoryBurgers
|
|
29
47
|
raise if attempt >= tries - 1
|
30
48
|
end
|
31
49
|
end
|
50
|
+
|
51
|
+
def invalid_association(reflection)
|
52
|
+
msg = "#{reflection.class} association #{reflection.name} on #{reflection.active_record.name} is not supported."
|
53
|
+
raise FactoryBurgers::Errors::InvalidAssociationError, msg
|
54
|
+
end
|
32
55
|
end
|
33
56
|
end
|
@@ -24,8 +24,8 @@ module FactoryBurgers
|
|
24
24
|
factory = params.fetch("factory")
|
25
25
|
traits = params["traits"]&.keys
|
26
26
|
attributes = attribute_overrides(params["attributes"])
|
27
|
-
owner = get_resource_owner(params[
|
28
|
-
return FactoryBurgers::Builder.new.build(factory, traits, attributes,
|
27
|
+
owner = get_resource_owner(params["owner_type"], params["owner_id"])
|
28
|
+
return FactoryBurgers::Builder.new(owner).build(factory, traits, attributes, as: params["owner_association"])
|
29
29
|
end
|
30
30
|
|
31
31
|
def request(env)
|
@@ -43,15 +43,13 @@ module FactoryBurgers
|
|
43
43
|
return attribute_items.map { |attr| [attr["name"], attr["value"]] }.to_h
|
44
44
|
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
return nil if owner_type.blank? || owner_id.blank? || owner_association.blank?
|
46
|
+
def get_resource_owner(owner_type, owner_id)
|
47
|
+
return nil if owner_type.blank? || owner_id.blank?
|
49
48
|
|
50
49
|
klass = owner_type.constantize
|
51
|
-
|
52
|
-
invalid_association(klass) if !valid_owner?(klass, owner_association)
|
50
|
+
invalid_resource(klass) if !valid_resource?(klass)
|
53
51
|
|
54
|
-
return klass.
|
52
|
+
return klass.find(owner_id)
|
55
53
|
end
|
56
54
|
|
57
55
|
def log_error(error)
|
@@ -64,20 +62,12 @@ module FactoryBurgers
|
|
64
62
|
|
65
63
|
private
|
66
64
|
|
67
|
-
def
|
65
|
+
def valid_resource?(klass)
|
68
66
|
klass < ActiveRecord::Base
|
69
67
|
end
|
70
68
|
|
71
|
-
def
|
72
|
-
raise "#{klass.name} is not a
|
73
|
-
end
|
74
|
-
|
75
|
-
def valid_association?(klass, assoc_name)
|
76
|
-
klass.reflections.include?(assoc_name)
|
77
|
-
end
|
78
|
-
|
79
|
-
def invalid_association(klass, association)
|
80
|
-
raise "#{association} is not an association for #{klass.name}!"
|
69
|
+
def invalid_resource(klass)
|
70
|
+
raise FactoryBurgers::Errors::InvalidRequestError, "#{klass.name} is not a valid resource."
|
81
71
|
end
|
82
72
|
end
|
83
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_burgers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Schwartz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: factory_bot
|
@@ -122,7 +122,8 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
-
description:
|
125
|
+
description: factory_burgers provides a UI and some tooling to allow manual testing
|
126
|
+
the abilit to use all of your factories.
|
126
127
|
email: ozydingo@gmail.com
|
127
128
|
executables: []
|
128
129
|
extensions: []
|
@@ -244,6 +245,7 @@ files:
|
|
244
245
|
- lib/factory_burgers/app.rb
|
245
246
|
- lib/factory_burgers/builder.rb
|
246
247
|
- lib/factory_burgers/cheating.rb
|
248
|
+
- lib/factory_burgers/errors.rb
|
247
249
|
- lib/factory_burgers/factory_bot_adapter.rb
|
248
250
|
- lib/factory_burgers/init.rb
|
249
251
|
- lib/factory_burgers/introspection.rb
|
@@ -280,5 +282,5 @@ requirements: []
|
|
280
282
|
rubygems_version: 3.0.3
|
281
283
|
signing_key:
|
282
284
|
specification_version: 4
|
283
|
-
summary:
|
285
|
+
summary: Bring the power of thoughtbot/factory_bot to manual testing
|
284
286
|
test_files: []
|