diff --git a/README.md b/README.md index 7fb6684..160193f 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,21 @@ Stacks for class methods on ActiveRecord models. The base implementation performs the reset. +* `Model::Association::Declaration` + + Wrapper around the `Model.has_many`, `Model.has_and_belongs_to_many`, `Model.has_one`, and + `Model.belongs_to` methods + + Env Field | Description | Initialized + --- | --- | --- + `:model` | The model Class being defined | *context* + `:name` | The name of the association being defined. | *arg* + `:scope` | The scope lambda associated with the association | *arg* + `:options` | Options associated with the association. | *arg* + `:extension` | Extensions to the association to be made. | *arg* + + The base implementation creates the association. + ### Migration Stacks for operations that change the schema. In some cases the operation immediately modifies the database schema, in others the operation defines ActiveRecord objects (e.g., column definitions in a create_table definition) and the actual modification of the database schema will happen some time later. diff --git a/lib/schema_plus/core/active_record/base.rb b/lib/schema_plus/core/active_record/base.rb index 470b50a..f636b8e 100644 --- a/lib/schema_plus/core/active_record/base.rb +++ b/lib/schema_plus/core/active_record/base.rb @@ -15,6 +15,34 @@ def reset_column_information super end end + + def has_many(name, scope = nil, options = {}, &extension) + SchemaMonkey::Middleware::Model::Association::Declaration.start(model: self, name: name, scope: scope, options: options, extension: extension) do + |env| + super(env.name, env.scope, env.options, &env.extension) + end + end + + def has_one(name, scope = nil, options = {}, &extension) + SchemaMonkey::Middleware::Model::Association::Declaration.start(model: self, name: name, scope: scope, options: options, extension: extension) do + |env| + super(env.name, env.scope, env.options, &env.extension) + end + end + + def has_and_belongs_to_many(name, scope = nil, options = {}, &extension) + SchemaMonkey::Middleware::Model::Association::Declaration.start(model: self, name: name, scope: scope, options: options, extension: extension) do + |env| + super(env.name, env.scope, env.options, &env.extension) + end + end + + def belongs_to(name, scope = nil, options = {}, &extension) + SchemaMonkey::Middleware::Model::Association::Declaration.start(model: self, name: name, scope: scope, options: options, extension: extension) do + |env| + super(env.name, env.scope, env.options, &env.extension) + end + end end end end diff --git a/lib/schema_plus/core/middleware.rb b/lib/schema_plus/core/middleware.rb index 8f8cd96..9f9a804 100644 --- a/lib/schema_plus/core/middleware.rb +++ b/lib/schema_plus/core/middleware.rb @@ -80,6 +80,11 @@ module Columns module ResetColumnInformation ENV = [:model] end + module Association + module Declaration + ENV = [:model, :name, :scope, :options, :extension] + end + end end end end diff --git a/spec/middleware_spec.rb b/spec/middleware_spec.rb index a1a0051..0a85082 100644 --- a/spec/middleware_spec.rb +++ b/spec/middleware_spec.rb @@ -104,6 +104,16 @@ def change Then { expect_middleware { Thing.reset_column_information } } end + context TestReporter::Middleware::Model::Association::Declaration do + Then do + class Thingamajig < ActiveRecord::Base; end + expect_middleware { Thingamajig.has_many :things, class_name: Thing.name } + expect_middleware { Thingamajig.has_one :thing, class_name: Thing.name } + expect_middleware { Thingamajig.belongs_to :another_thing, class_name: Thing.name } + expect_middleware { Thingamajig.has_and_belongs_to_many :other_things, class_name: Thing.name } + end + end + end context SchemaMonkey::Middleware::Dumper do diff --git a/spec/support/test_reporter.rb b/spec/support/test_reporter.rb index e680409..50575a7 100644 --- a/spec/support/test_reporter.rb +++ b/spec/support/test_reporter.rb @@ -46,6 +46,9 @@ module Table ; include Notify ; end module Model module Columns ; include Notify ; end module ResetColumnInformation ; include Notify ; end + module Association + module Declaration ; include Notify ; end + end end module Dumper