Skip to content

Commit

Permalink
Raise when multiple included blocks are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
md5 committed May 16, 2013
1 parent f964783 commit 2d7a86e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,9 @@
* Raise an error when multiple `included` blocks are defined for a Concern.
The old behavior would silently discard previously defined blocks, running
only the last one.

*Mike Dillon*

* Replace `multi_json` with `json`.

Since Rails requires Ruby 1.9 and since Ruby 1.9 includes `json` in the standard library,
Expand Down
8 changes: 8 additions & 0 deletions activesupport/lib/active_support/concern.rb
Expand Up @@ -98,6 +98,12 @@ module ActiveSupport
# include Bar # works, Bar takes care now of its dependencies
# end
module Concern
class MultipleIncludedBlocks < StandardError #:nodoc:
def initialize
super "Cannot define multiple 'included' blocks for a Concern"
end
end

def self.extended(base) #:nodoc:
base.instance_variable_set("@_dependencies", [])
end
Expand All @@ -117,6 +123,8 @@ def append_features(base)

def included(base = nil, &block)
if base.nil?
raise MultipleIncludedBlocks if instance_variable_defined?("@_included_block")

@_included_block = block
else
super
Expand Down
14 changes: 14 additions & 0 deletions activesupport/test/concern_test.rb
Expand Up @@ -91,4 +91,18 @@ def test_dependencies_with_multiple_modules
@klass.send(:include, Foo)
assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..2]
end

def test_raise_on_multiple_included_calls
assert_raises(ActiveSupport::Concern::MultipleIncludedBlocks) do
Module.new do
extend ActiveSupport::Concern

included do
end

included do
end
end
end
end
end

0 comments on commit 2d7a86e

Please sign in to comment.