Class: ViewModel::TestHelpers::ARVMBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/test_helpers/arvm_builder.rb

Defined Under Namespace

Classes: Spec

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, model_base: ApplicationRecord, viewmodel_base: ViewModelBase, namespace: Object, spec: nil, &block) ⇒ ARVMBuilder

Returns a new instance of ARVMBuilder.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 36

def initialize(name, model_base: ApplicationRecord, viewmodel_base: ViewModelBase, namespace: Object, spec: nil, &block)
  @model_base = model_base
  @viewmodel_base = viewmodel_base
  @namespace = namespace
  @name = name.to_s.camelize
  @no_viewmodel = false

  if spec
    define_schema(&spec.schema)
    define_model(&spec.model)
    define_viewmodel(&spec.viewmodel)
  else
    instance_eval(&block)
  end

  raise 'Model not created in ARVMBuilder'     unless model
  raise 'Schema not created in ARVMBuilder'    unless model.table_exists?
  raise 'ViewModel not created in ARVMBuilder' unless viewmodel || @no_viewmodel

  # Force the realization of the view model into the library's lookup
  # table. If this doesn't happen the library may have conflicting entries in
  # the deferred table, and will allow viewmodels to leak between tests.
  unless @no_viewmodel || !(@viewmodel < ViewModel::Record)
    resolved = ViewModel::Registry.for_view_name(viewmodel.view_name)
    raise 'Failed to register expected new class!' unless resolved == @viewmodel
  end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 4

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 4

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



4
5
6
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 4

def namespace
  @namespace
end

#viewmodelObject (readonly)

Returns the value of attribute viewmodel.



4
5
6
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 4

def viewmodel
  @viewmodel
end

Instance Method Details

#teardownObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 64

def teardown
  ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{name.underscore.pluralize} CASCADE")
  namespace.send(:remove_const, name)
  namespace.send(:remove_const, viewmodel_name) if viewmodel

  # prevent cached old class from being used to resolve associations
  if ActiveSupport::VERSION::MAJOR < 7
    ActiveSupport::Dependencies::Reference.clear!
  end
end