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
Public Class Methods
Source
# File lib/with_model/model.rb, line 21 def initialize(name, superclass: ActiveRecord::Base) @name = name.to_sym @model_block = nil @table_block = nil @table_options = {} @superclass = superclass end
@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.
Public Instance Methods
Source
# File lib/with_model/model.rb, line 29 def create table.create @model = Class.new(@superclass) do extend WithModel::Methods end stubber.stub_const @model setup_model end
Source
# File lib/with_model/model.rb, line 38 def destroy stubber.unstub_const cleanup_descendants_tracking reset_dependencies_cache table.destroy WithModel::DescendantsTracker.clear([@model]) @model = nil end
Private Instance Methods
Source
# File lib/with_model/model.rb, line 59 def cleanup_descendants_tracking ActiveSupport::DescendantsTracker.clear([@model]) \ unless ActiveSupport::DescendantsTracker.clear_disabled end
Source
# File lib/with_model/model.rb, line 49 def const_name @name.to_s.camelize.to_sym end
Source
# File lib/with_model/model.rb, line 64 def reset_dependencies_cache return unless defined?(ActiveSupport::Dependencies::Reference) ActiveSupport::Dependencies::Reference.clear! end
Source
# File lib/with_model/model.rb, line 53 def setup_model @model.table_name = table_name @model.class_eval(&@model_block) if @model_block @model.reset_column_information end
Source
# File lib/with_model/model.rb, line 70 def stubber @stubber ||= ConstantStubber.new const_name end
Source
# File lib/with_model/model.rb, line 74 def table @table ||= Table.new table_name, @table_options, connection: @superclass.connection, &@table_block end
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