Module: WithModel

Defined in:
lib/with_model.rb,
lib/with_model/model.rb,
lib/with_model/table.rb,
lib/with_model/methods.rb,
lib/with_model/version.rb,
lib/with_model/model/dsl.rb,
lib/with_model/null_table.rb,
lib/with_model/constant_stubber.rb,
lib/with_model/invalid_superclass.rb,
lib/with_model/missing_superclass.rb,
lib/with_model/descendants_tracker.rb

Defined Under Namespace

Modules: DescendantsTracker, Methods Classes: InvalidSuperclass, MiniTestLifeCycle, MissingSuperclass, Model, NullTable, Table

Constant Summary collapse

VERSION =
"2.3.0"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.runnerObject



39
40
41
# File 'lib/with_model.rb', line 39

def self.runner
  @runner ||= :rspec
end

Class Method Details

.deprecatorObject

The deprecator used for with_model's own deprecation warnings. Callers can silence it (WithModel.deprecator.silenced = true) or escalate it (behavior = :raise) while migrating, and Rails applications can register it in Rails.application.deprecators.



47
48
49
# File 'lib/with_model.rb', line 47

def self.deprecator
  @deprecator ||= ActiveSupport::Deprecation.new("3.0", "with_model")
end

.warn_omitted_table(name, callstack) ⇒ Object

Warns once per call site, at definition time, rather than once per example. The horizon is stated in the message because Deprecation#warn does not interpolate the deprecator's deprecation_horizon.

The callstack has to be handed in. ActiveSupport::Deprecation skips Rails' own frames and the standard library when working out where a warning came from, but with_model's frames look like anyone else's to it, so left to itself it reports this file for every omission in a suite.

Parameters:

  • callstack

    Frames to blame, beginning with the caller to report.



87
88
89
90
91
92
93
94
95
# File 'lib/with_model.rb', line 87

def self.warn_omitted_table(name, callstack)
  deprecator.warn(
    "with_model #{name.inspect} was called without a `table`, which creates a table with only " \
    "an id column. In with_model 3.0 no table will be created. Call `table` (with no " \
    "arguments or an empty block) to keep a table, or `table(false)` to inherit the " \
    "superclass's table (single table inheritance).",
    callstack
  )
end

Instance Method Details

#with_model(name, scope: nil, **options, &block) ⇒ Object

Parameters:

  • name (Symbol)

    The constant name to assign the model class to.

  • scope (defaults to: nil)

    Passed to before/after in the test context. RSpec only.

  • options
  • block

    Yielded an instance of WithModel::Model::DSL.



55
56
57
58
59
60
61
62
63
64
# File 'lib/with_model.rb', line 55

def with_model(name, scope: nil, **options, &block)
  runner = options.delete(:runner)
  model = Model.new name, **options
  dsl = Model::DSL.new model
  dsl.instance_exec(&block) if block
  # caller_locations(1) is this method's caller: the `with_model` line itself.
  WithModel.warn_omitted_table(name, caller_locations(1)) unless model.table_specified?

  setup_object(model, scope: scope, runner: runner)
end

#with_table(name, scope: nil, **options, &block) ⇒ Object

Parameters:



70
71
72
73
74
75
# File 'lib/with_model.rb', line 70

def with_table(name, scope: nil, **options, &block)
  runner = options.delete(:runner)
  table = Table.new name, options, &block

  setup_object(table, scope: scope, runner: runner)
end