Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define new hooks for ActiveRecord::Reflection::AssociationReflection #1

Merged
merged 3 commits into from
Apr 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions lib/schema_plus/core/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/schema_plus/core/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/support/test_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down