1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/spec/support/unit/helpers/model_builder.rb
Pedro Paiva 8dea981df0
Add Ruby 3.0 support (#1427)
* Fix warning: Using the last argument as keyword parameters is deprecated

* Add ruby 3.0 support

* Update listen to support ruby 3.0

* Prevent CI testing Ruby 3.0 with Rails 5.x

* Fix action/cache key

* Update nokogiri

* Use Ruby 3.0.1 for development

* Remove webdrivers gem
2021-04-09 22:56:49 -03:00

115 lines
3 KiB
Ruby

require_relative 'class_builder'
module UnitTests
module ModelBuilder
def create_table(*args, &block)
ModelBuilder.create_table(*args, &block)
end
def define_model(*args, &block)
ModelBuilder.define_model(*args, &block)
end
def define_model_instance(*args, &block)
define_model(*args, &block).new
end
def define_model_class(*args, &block)
ModelBuilder.define_model_class(*args, &block)
end
def define_active_model_class(*args, &block)
ModelBuilder.define_active_model_class(*args, &block)
end
class << self
def configure_example_group(example_group)
example_group.include(self)
example_group.after do
ModelBuilder.reset
end
end
def reset
clear_column_caches
drop_created_tables
created_tables.clear
defined_models.clear
end
def create_table(table_name, options = {}, &block)
connection =
options.delete(:connection) || DevelopmentRecord.connection
begin
connection.execute("DROP TABLE IF EXISTS #{table_name}")
connection.create_table(table_name, **options, &block)
created_tables << table_name
connection
rescue StandardError => e
connection.execute("DROP TABLE IF EXISTS #{table_name}")
raise e
end
end
def define_model_class(class_name, parent_class: DevelopmentRecord, &block)
ClassBuilder.define_class(class_name, parent_class, &block)
end
def define_active_model_class(class_name, options = {}, &block)
attribute_names = options.delete(:accessors) { [] }
UnitTests::ModelCreationStrategies::ActiveModel.call(
class_name,
attribute_names,
options,
&block
)
end
def define_model(name, columns = {}, options = {}, &block)
model = UnitTests::ModelCreationStrategies::ActiveRecord.call(
name,
columns,
options,
&block
)
model.reset_column_information
defined_models << model
model
end
private
def clear_column_caches
# Rails 4.x
if ::ActiveRecord::Base.connection.respond_to?(:schema_cache)
DevelopmentRecord.connection.schema_cache.clear!
ProductionRecord.connection.schema_cache.clear!
# Rails 3.1 - 4.0
elsif ::ActiveRecord::Base.connection_pool.respond_to?(:clear_cache!)
DevelopmentRecord.connection_pool.clear_cache!
ProductionRecord.connection_pool.clear_cache!
end
end
def drop_created_tables
created_tables.each do |table_name|
DevelopmentRecord.connection.
execute("DROP TABLE IF EXISTS #{table_name}")
ProductionRecord.connection.
execute("DROP TABLE IF EXISTS #{table_name}")
end
end
def created_tables
@_created_tables ||= []
end
def defined_models
@_defined_models ||= []
end
end
end
end