diff --git a/test/dummy/app/models/dummy_model.rb b/test/dummy/app/models/dummy_model.rb new file mode 100644 index 0000000..2f9e159 --- /dev/null +++ b/test/dummy/app/models/dummy_model.rb @@ -0,0 +1,2 @@ +class DummyModel < ApplicationRecord +end diff --git a/test/dummy/app/models/inherited_model.rb b/test/dummy/app/models/inherited_model.rb new file mode 100644 index 0000000..77ad16f --- /dev/null +++ b/test/dummy/app/models/inherited_model.rb @@ -0,0 +1,2 @@ +class InheritedModel < DummyModel +end diff --git a/test/dummy/app/models/missing_model.rb b/test/dummy/app/models/missing_model.rb new file mode 100644 index 0000000..4ed0710 --- /dev/null +++ b/test/dummy/app/models/missing_model.rb @@ -0,0 +1,2 @@ +class MissingModel < ApplicationRecord +end diff --git a/test/dummy/db/migrate/20231222124254_create_dummy_models.rb b/test/dummy/db/migrate/20231222124254_create_dummy_models.rb new file mode 100644 index 0000000..8551f0c --- /dev/null +++ b/test/dummy/db/migrate/20231222124254_create_dummy_models.rb @@ -0,0 +1,8 @@ +class CreateDummyModels < ActiveRecord::Migration[7.1] + def change + create_table :dummy_models do |t| + + t.timestamps + end + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb new file mode 100644 index 0000000..1e5713d --- /dev/null +++ b/test/dummy/db/schema.rb @@ -0,0 +1,19 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.1].define(version: 2023_12_22_124254) do + create_table "dummy_models", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/rails_model_load_hook_test.rb b/test/rails_model_load_hook_test.rb index d78331d..7c0d5ff 100644 --- a/test/rails_model_load_hook_test.rb +++ b/test/rails_model_load_hook_test.rb @@ -1,7 +1,57 @@ require "test_helper" +require 'minitest/autorun' -class RailsModelLoadHookTest < ActiveSupport::TestCase - test "it has a version number" do - assert RailsModelLoadHook::VERSION - end +describe RailsModelLoadHook do + let(:loaded) { [] } + let(:on_load) { -> { loaded << _1 } } + + before do + on_load = self.on_load + + ActiveSupport.on_load :model_class do + on_load[self] + end + end + + module SharedExamples + refine Minitest::Spec.singleton_class do + def passing + it 'passes the hook' do + _(loaded).must_include model + end + end + + def skipping + it 'skips the hook' do + _(loaded).wont_include model + end + end + end + end + + using SharedExamples + + describe 'with existing models' do + let(:model) { DummyModel.tap { _1.new } } + + passing + end + + describe 'with inherited models' do + let(:model) { InheritedModel.tap { _1.new } } + + passing + end + + describe 'with ApplicationRecord' do + let(:model) { ApplicationRecord } + + skipping + end + + describe 'with models lacking DB tables' do + let(:model) { MissingModel } + + skipping + end end