class ViewModel::TestHelpers::ARVMBuilder

Constants

Spec

Building an ARVM requires three blocks, to define schema, model and viewmodel. Support providing these either in an spec argument or as a dsl-style builder.

Attributes

model[R]
name[R]
namespace[R]
viewmodel[R]

Public Class Methods

new(name, model_base: ApplicationRecord, viewmodel_base: ViewModelBase, namespace: Object, spec: nil, &block) click to toggle source
# 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

Public Instance Methods

teardown() click to toggle source
# 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
  ActiveSupport::Dependencies::Reference.clear!
end

Private Instance Methods

define_model(&block) click to toggle source
# File lib/view_model/test_helpers/arvm_builder.rb, line 87
def define_model(&block)
  model_name = name
  model_namespace = namespace
  @model = Class.new(@model_base) do |_c|
    raise "Model already defined: #{model_name}" if model_namespace.const_defined?(model_name, false)

    model_namespace.const_set(model_name, self)
    class_eval(&block)
    reset_column_information
  end
  @model
end
define_schema(&block) click to toggle source
# File lib/view_model/test_helpers/arvm_builder.rb, line 78
def define_schema(&block)
  table_name = name.underscore.pluralize
  ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{table_name} CASCADE")
  ActiveRecord::Schema.define do
    self.verbose = false
    create_table(table_name, &block)
  end
end
define_viewmodel(&block) click to toggle source
# File lib/view_model/test_helpers/arvm_builder.rb, line 100
def define_viewmodel(&block)
  vm_name = viewmodel_name
  vm_namespace = namespace
  @viewmodel = Class.new(@viewmodel_base) do |_c|
    raise "Viewmodel alreay defined: #{vm_name}" if vm_namespace.const_defined?(vm_name, false)

    vm_namespace.const_set(vm_name, self)
    class_eval(&block)
  end
  raise 'help help' if @viewmodel.name.nil?

  @viewmodel
end
no_viewmodel() click to toggle source
# File lib/view_model/test_helpers/arvm_builder.rb, line 114
def no_viewmodel
  @no_viewmodel = true
end
viewmodel_name() click to toggle source
# File lib/view_model/test_helpers/arvm_builder.rb, line 74
def viewmodel_name
  self.name + 'View'
end