Permalink
Browse files

Deprecate plugin structure in favor of using ActiveSupport::Concern

  • Loading branch information...
bkeepers committed Sep 23, 2010
1 parent 1342424 commit 7c69e08756b2223db171d7562cf87fac44e2a085
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
@@ -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
@@ -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?)
@@ -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
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Associations
extend ActiveSupport::Concern
module ClassMethods
def inherited(subclass)
subclass.associations = associations.dup
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Caching
extend ActiveSupport::Concern
module InstanceMethods
def cache_key(*suffixes)
cache_key = case
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Callbacks
extend ActiveSupport::Concern
module InstanceMethods
def initialize(attrs = {})
super.tap { run_callbacks(:initialize) }
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Clone
extend ActiveSupport::Concern
module InstanceMethods
def initialize_copy(other)
@_new = true
@@ -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)$/
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Document
extend ActiveSupport::Concern
module ClassMethods
def embeddable?
false
@@ -4,6 +4,8 @@
module MongoMapper
module Plugins
module DynamicQuerying
extend ActiveSupport::Concern
module ClassMethods
def dynamic_find(finder, args)
attributes = {}
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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|
@@ -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
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Logger
extend ActiveSupport::Concern
module ClassMethods
def logger
MongoMapper.logger
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Modifiers
extend ActiveSupport::Concern
module ClassMethods
def increment(*args)
modifier_update('$inc', args)
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Pagination
extend ActiveSupport::Concern
module ClassMethods
def per_page; 25 end
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Persistence
extend ActiveSupport::Concern
module ClassMethods
def connection(mongo_connection=nil)
assert_supported
@@ -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?)
@@ -5,6 +5,8 @@
module MongoMapper
module Plugins
module Querying
extend ActiveSupport::Concern
module ClassMethods
include PluckyMethods
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Rails
extend ActiveSupport::Concern
module InstanceMethods
def to_param
id.to_s if persisted?
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Safe
extend ActiveSupport::Concern
module ClassMethods
def inherited(subclass)
super
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Sci
extend ActiveSupport::Concern
module ClassMethods
def inherited(subclass)
key :_type, String unless key?(:_type)
@@ -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|
@@ -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
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Timestamps
extend ActiveSupport::Concern
module ClassMethods
def timestamps!
key :created_at, Time
@@ -2,6 +2,8 @@
module MongoMapper
module Plugins
module Userstamps
extend ActiveSupport::Concern
module ClassMethods
def userstamps!
key :creator_id, ObjectId
@@ -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
@@ -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
@@ -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,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 {
Oops, something went wrong.

0 comments on commit 7c69e08

Please sign in to comment.