Skip to content

Commit

Permalink
Merge pull request #1 from lowjoel/master
Browse files Browse the repository at this point in the history
Define new hooks for ActiveRecord::Reflection::AssociationReflection
  • Loading branch information
ronen committed Apr 1, 2015
2 parents 03bc7a4 + 19a2d67 commit aad19ea
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
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

0 comments on commit aad19ea

Please sign in to comment.