Skip to content

Commit

Permalink
Remove remnants of resetting tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jagthedrummer committed Oct 23, 2023
1 parent be779b0 commit 45de06a
Showing 1 changed file with 2 additions and 53 deletions.
55 changes: 2 additions & 53 deletions bullet_train-api/lib/bullet_train/api/example_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@

module FactoryBot
module ExampleBot
attr_accessor :tables_to_reset

def example(model, **)
@tables_to_reset = [model.to_s.pluralize]

object = FactoryBot.build(factory(model), **)

object
ensure
reset_tables!
FactoryBot.build(factory(model), **)
end

def example_list(model, quantity, **)
@tables_to_reset = [model.to_s.pluralize]

objects = FactoryBot.build_list(factory(model), quantity, **)

objects
ensure
reset_tables!
FactoryBot.build_list(factory(model), quantity, **)
end

REST_METHODS = %i[get_examples get_example post_example post_parameters put_example put_parameters patch_example patch_parameters]
Expand All @@ -39,43 +25,6 @@ def factory(model)
factories.include?("#{model}_example") ? "#{model}_example".to_sym : model
end

def reset_tables!
# This is only available for postgres
return unless @tables_to_reset.present?

return unless ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)

@tables_to_reset.each do |name|
ActiveRecord::Base.connection.reset_pk_sequence!(name) if ActiveRecord::Base.connection.table_exists?(name)
end
end

def deep_clone(instance)
clone = instance.clone

instance.class.reflections.each do |name, reflection|
if reflection.macro == :has_many
associations = instance.send(name).map { |association| association.clone }
clone.send("#{name}=", associations)
@tables_to_reset << name
elsif %i[belongs_to has_one].include?(reflection.macro)
# Calling e.g. address.team= throws an error,
# if address has_one team through person
# and person has_one team through company
# and company belongs_to team
# so the resulting error will be caught here and a warning logged.
begin
clone.send("#{name}=", instance.send(name).clone)
@tables_to_reset << name.pluralize
rescue ActiveRecord::HasOneThroughNestedAssociationsAreReadonly
Rails.logger.warn("ExampleBot.deep_clone ignored setting #{name} of #{instance}")
end
end
end

clone
end

include ::Api::OpenApiHelper
def _path_examples(method, model, **options)
version = options.delete(:version) || "v1"
Expand Down

0 comments on commit 45de06a

Please sign in to comment.