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.



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

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.



2
3
4
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 2

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 2

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



2
3
4
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 2

def namespace
  @namespace
end

#viewmodelObject (readonly)

Returns the value of attribute viewmodel.



2
3
4
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 2

def viewmodel
  @viewmodel
end

Instance Method Details

#teardownObject



61
62
63
64
65
66
67
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 61

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
  ActiveSupport::Dependencies::Reference.clear!
end