Browse files

correct usage of ActiveSupport::Concern

You should not use include inside an included block as this prevents
developers from extending any of the mongoid api in the correct way.
Module inclusion should build up the inheritance chain with each include,
so that the last included module is first in the chain. Including in an
included block kills this by insuring your module is included last.

Check out the source for ActiveSupport::Concern to see what I mean.
  • Loading branch information...
1 parent ee64f1d commit 6854503b3892f608ad92b37f0e99eddc9f2a2d09 @adzap adzap committed Nov 4, 2010
Showing with 35 additions and 33 deletions.
  1. +2 −2 lib/mongoid/associations.rb
  2. +31 −29 lib/mongoid/components.rb
  3. +2 −2 lib/mongoid/document.rb
View
4 lib/mongoid/associations.rb
@@ -14,9 +14,9 @@
module Mongoid # :nodoc:
module Associations #:nodoc:
extend ActiveSupport::Concern
- included do
- include ForeignKey
+ include ForeignKey
+ included do
cattr_accessor :embedded
self.embedded = false
View
60 lib/mongoid/components.rb
@@ -2,39 +2,41 @@
module Mongoid #:nodoc
module Components #:nodoc
extend ActiveSupport::Concern
+
+ # All modules that a +Document+ is composed of are defined in this
+ # module, to keep the document class from getting too cluttered.
included do
- # All modules that a +Document+ is composed of are defined in this
- # module, to keep the document class from getting too cluttered.
- include ActiveModel::Conversion
- include ActiveModel::Naming
- include ActiveModel::Serialization
- include ActiveModel::MassAssignmentSecurity
- include ActiveModel::Serializers::JSON
- include ActiveModel::Serializers::Xml
- include Mongoid::Associations
- include Mongoid::Atomicity
- include Mongoid::Attributes
- include Mongoid::Collections
- include Mongoid::Dirty
- include Mongoid::Extras
- include Mongoid::Fields
- include Mongoid::Hierarchy
- include Mongoid::Indexes
- include Mongoid::JSON
- include Mongoid::Keys
- include Mongoid::Matchers
- include Mongoid::Memoization
- include Mongoid::Modifiers
- include Mongoid::MultiParameterAttributes
- include Mongoid::Paths
- include Mongoid::Persistence
- include Mongoid::Safety
- include Mongoid::State
- include Mongoid::Validations
- include Mongoid::Callbacks
extend ActiveModel::Translation
extend Mongoid::Finders
extend Mongoid::NamedScope
end
+
+ include ActiveModel::Conversion
+ include ActiveModel::Naming
+ include ActiveModel::Serialization
+ include ActiveModel::MassAssignmentSecurity
+ include ActiveModel::Serializers::JSON
+ include ActiveModel::Serializers::Xml
+ include Mongoid::Associations
+ include Mongoid::Atomicity
+ include Mongoid::Attributes
+ include Mongoid::Collections
+ include Mongoid::Dirty
+ include Mongoid::Extras
+ include Mongoid::Fields
+ include Mongoid::Hierarchy
+ include Mongoid::Indexes
+ include Mongoid::JSON
+ include Mongoid::Keys
+ include Mongoid::Matchers
+ include Mongoid::Memoization
+ include Mongoid::Modifiers
+ include Mongoid::MultiParameterAttributes
+ include Mongoid::Paths
+ include Mongoid::Persistence
+ include Mongoid::Safety
+ include Mongoid::State
+ include Mongoid::Validations
+ include Mongoid::Callbacks
end
end
View
4 lib/mongoid/document.rb
@@ -2,9 +2,9 @@
module Mongoid #:nodoc:
module Document
extend ActiveSupport::Concern
- included do
- include Mongoid::Components
+ include Mongoid::Components
+ included do
attr_accessor :association_name
attr_reader :new_record
end

0 comments on commit 6854503

Please sign in to comment.