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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67f3705a0aa0daae828a023c64d2076905e6a55ecc976819b2c1782fd654f7bd
4
- data.tar.gz: 41f2fb80a4430ddb0fe469f6c43edc41cdd8f479b8214be080461411c860740f
3
+ metadata.gz: 4f976d301bd92dab1483eae6443b7aff5a8f96ff417a24a79bb9a03568c79028
4
+ data.tar.gz: 6a5718377d57f7c9fe72ab91cab29a7bc156e348ffe56cfd3401b7e4c87fd0c5
5
5
  SHA512:
6
- metadata.gz: a543ecdb55543d128659f8c999ecf4d90cf1a35dd0a616a11160723236effa2c259c502b33e65f23e11e575d17a3b3954163fa1bde9bb55b107af9c0d6cfecec
7
- data.tar.gz: eaaf8613824e8f425fe09a5bc21a4ae74cb5678853634819380e5edbd44d6ece1c730e4e0afa63a24653a848f54f96f22fe0bb01712990f9eb8984257756343c
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, owner)
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
- update_owner_resource(owner, params[:owner_association], resource) if owner
17
+ update_assocation_to_owner(as, resource) if owner && as
10
18
 
11
19
  return resource
12
20
  end
13
21
 
14
- def update_owner_resource(owner, name, resource)
15
- reflection = owner.class.reflections[name] or raise "Could not find reflection named #{name}"
16
- if reflection.is_a?(ActiveRecord::Reflection::HasManyReflection)
17
- owner.update!(name => owner.public_send(name) + [resource])
18
- resource.reload
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
- owner.update!(name => resource)
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
@@ -0,0 +1,7 @@
1
+ module FactoryBurgers
2
+ module Errors
3
+ FactoryBurgerError = Class.new(StandardError)
4
+ InvalidAssociationError = Class.new(FactoryBurgerError)
5
+ InvalidRequestError = Class.new(FactoryBurgerError)
6
+ end
7
+ 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[:owner_type], params[:owner_id], params[:owner_association])
28
- return FactoryBurgers::Builder.new.build(factory, traits, attributes, owner)
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
- # TODO: make params explicit
47
- def get_resource_owner(owner_type, owner_id, owner_association)
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
- invalid_build_class(klass) if !valid_build_class?(klass)
52
- invalid_association(klass) if !valid_owner?(klass, owner_association)
50
+ invalid_resource(klass) if !valid_resource?(klass)
53
51
 
54
- return klass.constantize.find(owner_id)
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 valid_build_class?(klass)
65
+ def valid_resource?(klass)
68
66
  klass < ActiveRecord::Base
69
67
  end
70
68
 
71
- def invalid_build_class(klass)
72
- raise "#{klass.name} is not a thing I can build."
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
@@ -1,3 +1,3 @@
1
1
  module FactoryBurgers
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.1.5'.freeze
3
3
  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
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-23 00:00:00.000000000 Z
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: UI for thoughtbot/factory_bot
285
+ summary: Bring the power of thoughtbot/factory_bot to manual testing
284
286
  test_files: []