class WithModel::Model

In general, direct use of this class should be avoided. Instead use either the {WithModel high-level API} or {WithModel::Model::DSL low-level API}.

Attributes

model_block[W]
table_block[W]
table_options[W]

Public Class Methods

new(name, superclass: ActiveRecord::Base) click to toggle source

@param [Symbol] name The constant name to assign the model class to. @param [Class] superclass The superclass for the created class. Should

have `ActiveRecord::Base` as an ancestor.
# File lib/with_model/model.rb, line 19
def initialize(name, superclass: ActiveRecord::Base)
  @name = name.to_sym
  @model_block = nil
  @table_block = nil
  @table_options = {}
  @superclass = superclass
end

Public Instance Methods

create() click to toggle source
# File lib/with_model/model.rb, line 27
def create
  table.create
  @model = Class.new(@superclass) do
    extend WithModel::Methods
  end
  stubber.stub_const @model
  setup_model
end
destroy() click to toggle source
# File lib/with_model/model.rb, line 36
def destroy
  stubber.unstub_const
  cleanup_descendants_tracking
  reset_dependencies_cache
  table.destroy
  @model = nil
end

Private Instance Methods

cleanup_descendants_tracking() click to toggle source
# File lib/with_model/model.rb, line 56
def cleanup_descendants_tracking
  if defined?(ActiveSupport::DescendantsTracker)
    ActiveSupport::DescendantsTracker.class_variable_get(:@@direct_descendants).delete(ActiveRecord::Base)
  elsif @model.superclass.respond_to?(:direct_descendants)
    @model.superclass.direct_descendants.delete(@model)
  end
end
const_name() click to toggle source
# File lib/with_model/model.rb, line 46
def const_name
  @name.to_s.camelize.to_sym
end
reset_dependencies_cache() click to toggle source
# File lib/with_model/model.rb, line 64
def reset_dependencies_cache
  return unless defined?(ActiveSupport::Dependencies::Reference)

  ActiveSupport::Dependencies::Reference.clear!
end
setup_model() click to toggle source
# File lib/with_model/model.rb, line 50
def setup_model
  @model.table_name = table_name
  @model.class_eval(&@model_block) if @model_block
  @model.reset_column_information
end
stubber() click to toggle source
# File lib/with_model/model.rb, line 70
def stubber
  @stubber ||= ConstantStubber.new const_name
end
table() click to toggle source
# File lib/with_model/model.rb, line 74
def table
  @table ||= Table.new table_name, @table_options, &@table_block
end
table_name() click to toggle source
# File lib/with_model/model.rb, line 78
def table_name
  uid = "#{$PID}_#{Thread.current.object_id}"
  "with_model_#{@name.to_s.tableize}_#{uid}"
end