Permalink
Browse files

Deprecate plugin structure in favor of using ActiveSupport::Concern

  • Loading branch information...
1 parent 1342424 commit 7c69e08756b2223db171d7562cf87fac44e2a085 @bkeepers bkeepers committed Sep 23, 2010
Showing with 161 additions and 59 deletions.
  1. +8 −3 lib/mongo_mapper/plugins.rb
  2. +2 −0 lib/mongo_mapper/plugins/accessible.rb
  3. +11 −11 lib/mongo_mapper/plugins/active_model.rb
  4. +2 −0 lib/mongo_mapper/plugins/associations.rb
  5. +2 −0 lib/mongo_mapper/plugins/caching.rb
  6. +2 −0 lib/mongo_mapper/plugins/callbacks.rb
  7. +2 −0 lib/mongo_mapper/plugins/clone.rb
  8. +2 −0 lib/mongo_mapper/plugins/dirty.rb
  9. +2 −0 lib/mongo_mapper/plugins/document.rb
  10. +2 −0 lib/mongo_mapper/plugins/dynamic_querying.rb
  11. +7 −7 lib/mongo_mapper/plugins/embedded_callbacks.rb
  12. +4 −4 lib/mongo_mapper/plugins/embedded_document.rb
  13. +2 −0 lib/mongo_mapper/plugins/equality.rb
  14. +4 −2 lib/mongo_mapper/plugins/identity_map.rb
  15. +2 −0 lib/mongo_mapper/plugins/indexes.rb
  16. +2 −0 lib/mongo_mapper/plugins/inspect.rb
  17. +4 −2 lib/mongo_mapper/plugins/keys.rb
  18. +2 −0 lib/mongo_mapper/plugins/logger.rb
  19. +2 −0 lib/mongo_mapper/plugins/modifiers.rb
  20. +2 −0 lib/mongo_mapper/plugins/pagination.rb
  21. +2 −0 lib/mongo_mapper/plugins/persistence.rb
  22. +2 −0 lib/mongo_mapper/plugins/protected.rb
  23. +2 −0 lib/mongo_mapper/plugins/querying.rb
  24. +2 −0 lib/mongo_mapper/plugins/rails.rb
  25. +2 −0 lib/mongo_mapper/plugins/safe.rb
  26. +2 −0 lib/mongo_mapper/plugins/sci.rb
  27. +2 −0 lib/mongo_mapper/plugins/scopes.rb
  28. +4 −2 lib/mongo_mapper/plugins/serialization.rb
  29. +2 −0 lib/mongo_mapper/plugins/timestamps.rb
  30. +2 −0 lib/mongo_mapper/plugins/userstamps.rb
  31. +4 −4 lib/mongo_mapper/plugins/validations.rb
  32. +1 −1 test/unit/test_dynamic_finder.rb
  33. +1 −1 test/unit/test_key.rb
  34. +0 −2 test/unit/test_keys.rb
  35. +67 −20 test/unit/test_plugins.rb
View
11 lib/mongo_mapper/plugins.rb
@@ -6,9 +6,14 @@ def plugins
end
def plugin(mod)
- extend mod::ClassMethods if mod.const_defined?(:ClassMethods)
- include mod::InstanceMethods if mod.const_defined?(:InstanceMethods)
- mod.configure(self) if mod.respond_to?(:configure)
+ if ActiveSupport::Concern === mod
+ include mod
+ else
+ warn "[DEPRECATED] Plugins must extend ActiveSupport::Concern"
+ extend mod::ClassMethods if mod.const_defined?(:ClassMethods)
+ include mod::InstanceMethods if mod.const_defined?(:InstanceMethods)
+ mod.configure(self) if mod.respond_to?(:configure)
+ end
plugins << mod
end
end
View
2 lib/mongo_mapper/plugins/accessible.rb
@@ -1,6 +1,8 @@
module MongoMapper
module Plugins
module Accessible
+ extend ActiveSupport::Concern
+
module ClassMethods
def attr_accessible(*attrs)
raise AccessibleOrProtected.new(name) if try(:protected_attributes?)
View
22 lib/mongo_mapper/plugins/active_model.rb
@@ -2,17 +2,17 @@
module MongoMapper
module Plugins
module ActiveModel
- def self.configure(model)
- model.class_eval do
- include ::ActiveModel::Naming
- include ::ActiveModel::Conversion
- include ::ActiveModel::Serialization
- include ::ActiveModel::Serializers::Xml
- include ::ActiveModel::Serializers::JSON
- include ::ActiveSupport::DescendantsTracker
-
- extend ::ActiveModel::Translation
- end
+ extend ActiveSupport::Concern
+
+ included do
+ include ::ActiveModel::Naming
+ include ::ActiveModel::Conversion
+ include ::ActiveModel::Serialization
+ include ::ActiveModel::Serializers::Xml
+ include ::ActiveModel::Serializers::JSON
+ include ::ActiveSupport::DescendantsTracker
+
+ extend ::ActiveModel::Translation
end
end
end
View
2 lib/mongo_mapper/plugins/associations.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Associations
+ extend ActiveSupport::Concern
+
module ClassMethods
def inherited(subclass)
subclass.associations = associations.dup
View
2 lib/mongo_mapper/plugins/caching.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Caching
+ extend ActiveSupport::Concern
+
module InstanceMethods
def cache_key(*suffixes)
cache_key = case
View
2 lib/mongo_mapper/plugins/callbacks.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Callbacks
+ extend ActiveSupport::Concern
+
module InstanceMethods
def initialize(attrs = {})
super.tap { run_callbacks(:initialize) }
View
2 lib/mongo_mapper/plugins/clone.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Clone
+ extend ActiveSupport::Concern
+
module InstanceMethods
def initialize_copy(other)
@_new = true
View
2 lib/mongo_mapper/plugins/dirty.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Dirty
+ extend ActiveSupport::Concern
+
module InstanceMethods
def method_missing(method, *args, &block)
if method.to_s =~ /(_changed\?|_change|_will_change!|_was)$/
View
2 lib/mongo_mapper/plugins/document.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Document
+ extend ActiveSupport::Concern
+
module ClassMethods
def embeddable?
false
View
2 lib/mongo_mapper/plugins/dynamic_querying.rb
@@ -4,6 +4,8 @@
module MongoMapper
module Plugins
module DynamicQuerying
+ extend ActiveSupport::Concern
+
module ClassMethods
def dynamic_find(finder, args)
attributes = {}
View
14 lib/mongo_mapper/plugins/embedded_callbacks.rb
@@ -2,14 +2,14 @@
module MongoMapper
module Plugins
module EmbeddedCallbacks
- def self.configure(model)
- model.class_eval do
- extend ::ActiveModel::Callbacks
- include ::ActiveModel::Validations::Callbacks
+ extend ActiveSupport::Concern
- define_model_callbacks :validation, :save, :create, :update, :destroy, :only => [ :before, :after ]
- define_model_callbacks :initialize, :find, :only => :after
- end
+ included do
+ extend ::ActiveModel::Callbacks
+ include ::ActiveModel::Validations::Callbacks
+
+ define_model_callbacks :validation, :save, :create, :update, :destroy, :only => [ :before, :after ]
+ define_model_callbacks :initialize, :find, :only => :after
end
module InstanceMethods
View
8 lib/mongo_mapper/plugins/embedded_document.rb
@@ -2,10 +2,10 @@
module MongoMapper
module Plugins
module EmbeddedDocument
- def self.configure(model)
- model.class_eval do
- attr_accessor :_parent_document
- end
+ extend ActiveSupport::Concern
+
+ included do
+ attr_accessor :_parent_document
end
module ClassMethods
View
2 lib/mongo_mapper/plugins/equality.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Equality
+ extend ActiveSupport::Concern
+
module InstanceMethods
def eql?(other)
other.is_a?(self.class) && _id == other._id
View
6 lib/mongo_mapper/plugins/identity_map.rb
@@ -4,6 +4,8 @@
module MongoMapper
module Plugins
module IdentityMap
+ extend ActiveSupport::Concern
+
def self.models
@models ||= Set.new
end
@@ -12,8 +14,8 @@ def self.clear
models.each { |m| m.identity_map.clear }
end
- def self.configure(model)
- IdentityMap.models << model
+ included do
+ IdentityMap.models << self
end
module ClassMethods
View
2 lib/mongo_mapper/plugins/indexes.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Indexes
+ extend ActiveSupport::Concern
+
module ClassMethods
def ensure_index(spec, options={})
collection.create_index(spec, options)
View
2 lib/mongo_mapper/plugins/inspect.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Inspect
+ extend ActiveSupport::Concern
+
module InstanceMethods
def inspect
attributes_as_nice_string = key_names.collect do |name|
View
6 lib/mongo_mapper/plugins/keys.rb
@@ -4,8 +4,10 @@
module MongoMapper
module Plugins
module Keys
- def self.configure(model)
- model.key :_id, ObjectId
+ extend ActiveSupport::Concern
+
+ included do
+ key :_id, ObjectId
end
module ClassMethods
View
2 lib/mongo_mapper/plugins/logger.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Logger
+ extend ActiveSupport::Concern
+
module ClassMethods
def logger
MongoMapper.logger
View
2 lib/mongo_mapper/plugins/modifiers.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Modifiers
+ extend ActiveSupport::Concern
+
module ClassMethods
def increment(*args)
modifier_update('$inc', args)
View
2 lib/mongo_mapper/plugins/pagination.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Pagination
+ extend ActiveSupport::Concern
+
module ClassMethods
def per_page; 25 end
View
2 lib/mongo_mapper/plugins/persistence.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Persistence
+ extend ActiveSupport::Concern
+
module ClassMethods
def connection(mongo_connection=nil)
assert_supported
View
2 lib/mongo_mapper/plugins/protected.rb
@@ -4,6 +4,8 @@
module MongoMapper
module Plugins
module Protected
+ extend ActiveSupport::Concern
+
module ClassMethods
def attr_protected(*attrs)
raise AccessibleOrProtected.new(name) if try(:accessible_attributes?)
View
2 lib/mongo_mapper/plugins/querying.rb
@@ -5,6 +5,8 @@
module MongoMapper
module Plugins
module Querying
+ extend ActiveSupport::Concern
+
module ClassMethods
include PluckyMethods
View
2 lib/mongo_mapper/plugins/rails.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Rails
+ extend ActiveSupport::Concern
+
module InstanceMethods
def to_param
id.to_s if persisted?
View
2 lib/mongo_mapper/plugins/safe.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Safe
+ extend ActiveSupport::Concern
+
module ClassMethods
def inherited(subclass)
super
View
2 lib/mongo_mapper/plugins/sci.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Sci
+ extend ActiveSupport::Concern
+
module ClassMethods
def inherited(subclass)
key :_type, String unless key?(:_type)
View
2 lib/mongo_mapper/plugins/scopes.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Scopes
+ extend ActiveSupport::Concern
+
module ClassMethods
def scope(name, scope_options={})
scopes[name] = lambda do |*args|
View
6 lib/mongo_mapper/plugins/serialization.rb
@@ -4,8 +4,10 @@
module MongoMapper
module Plugins
module Serialization
- def self.configure(model)
- model.class_eval { cattr_accessor :include_root_in_json, :instance_writer => true }
+ extend ActiveSupport::Concern
+
+ included do
+ cattr_accessor :include_root_in_json, :instance_writer => true
end
module InstanceMethods
View
2 lib/mongo_mapper/plugins/timestamps.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Timestamps
+ extend ActiveSupport::Concern
+
module ClassMethods
def timestamps!
key :created_at, Time
View
2 lib/mongo_mapper/plugins/userstamps.rb
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Userstamps
+ extend ActiveSupport::Concern
+
module ClassMethods
def userstamps!
key :creator_id, ObjectId
View
8 lib/mongo_mapper/plugins/validations.rb
@@ -2,10 +2,10 @@
module MongoMapper
module Plugins
module Validations
- def self.configure(model)
- model.class_eval do
- include ::ActiveModel::Validations
- end
+ extend ActiveSupport::Concern
+
+ included do
+ include ::ActiveModel::Validations
end
module ClassMethods
View
2 test/unit/test_dynamic_finder.rb
@@ -1,7 +1,7 @@
require 'test_helper'
class DynamicFinderTest < Test::Unit::TestCase
- include MongoMapper::Plugins::DynamicQuerying
+ DynamicFinder = MongoMapper::Plugins::DynamicQuerying::DynamicFinder
should "initialize with method" do
finder = DynamicFinder.new(:foobar)
View
2 test/unit/test_key.rb
@@ -12,7 +12,7 @@ def self.from_mongo(value)
end
class KeyTest < Test::Unit::TestCase
- include MongoMapper::Plugins::Keys
+ Key = MongoMapper::Plugins::Keys::Key
context "Initializing a new key" do
should "allow setting the name" do
View
2 test/unit/test_keys.rb
@@ -2,8 +2,6 @@
require 'models'
class KeyTest < Test::Unit::TestCase
- include MongoMapper::Plugins::Keys
-
context ".new with no id and _id of type integer" do
should "not error" do
lambda {
View
87 test/unit/test_plugins.rb
@@ -1,29 +1,35 @@
require 'test_helper'
-module MyPlugin
- def self.configure(model)
- model.class_eval { attr_accessor :from_configure }
+class PluginsTest < Test::Unit::TestCase
+ should "default plugins to empty array" do
+ Class.new { extend MongoMapper::Plugins }.plugins.should == []
end
- module ClassMethods
- def class_foo
- 'class_foo'
- end
- end
+ context "ActiveSupport::Concern" do
+ module MyConcern
+ extend ActiveSupport::Concern
+
+ included do
+ attr_accessor :from_concern
+ end
- module InstanceMethods
- def instance_foo
- 'instance_foo'
+ module ClassMethods
+ def class_foo
+ 'class_foo'
+ end
+ end
+
+ module InstanceMethods
+ def instance_foo
+ 'instance_foo'
+ end
+ end
end
- end
-end
-class PluginsTest < Test::Unit::TestCase
- context "plugin" do
setup do
@document = Class.new do
extend MongoMapper::Plugins
- plugin MyPlugin
+ plugin MyConcern
end
end
@@ -36,15 +42,56 @@ class PluginsTest < Test::Unit::TestCase
end
should "pass model to configure" do
- @document.new.should respond_to(:from_configure)
+ @document.new.should respond_to(:from_concern)
+ end
+
+ should "add plugin to plugins" do
+ @document.plugins.should include(MyConcern)
+ end
+ end
+
+ context "deprecated plugin" do
+ module DeprecatedPlugin
+ def self.configure(model)
+ model.class_eval { attr_accessor :from_configure }
+ end
+
+ module ClassMethods
+ def class_foo
+ 'class_foo'
+ end
+ end
+
+ module InstanceMethods
+ def instance_foo
+ 'instance_foo'
+ end
+ end
+ end
+
+ setup do
+ silence_stderr do
+ @document = Class.new do
+ extend MongoMapper::Plugins
+ plugin DeprecatedPlugin
+ end
+ end
+ end
+
+ should "include instance methods" do
+ @document.new.instance_foo.should == 'instance_foo'
end
- should "default plugins to empty array" do
- Class.new { extend MongoMapper::Plugins }.plugins.should == []
+ should "extend class methods" do
+ @document.class_foo.should == 'class_foo'
+ end
+
+ should "pass model to configure" do
+ @document.new.should respond_to(:from_configure)
end
should "add plugin to plugins" do
- @document.plugins.should include(MyPlugin)
+ @document.plugins.should include(DeprecatedPlugin)
end
end
end

0 comments on commit 7c69e08

Please sign in to comment.