Permalink
Browse files

Cleaned up protected attributes plugin and added tests for embedded d…

…ocs.
  • Loading branch information...
1 parent e5571ab commit 845e6021fa7237e26af159c2e4b7c061033096f4 @jnunemaker jnunemaker committed Jan 28, 2010
View
2 lib/mongo_mapper/document.rb
@@ -7,8 +7,8 @@ def self.included(model)
include InstanceMethods
extend ClassMethods
extend Finders
-
extend Plugins
+
plugin Plugins::Associations
plugin Plugins::Clone
plugin Plugins::Descendants
View
2 lib/mongo_mapper/embedded_document.rb
@@ -6,8 +6,8 @@ def self.included(model)
model.class_eval do
include InstanceMethods
extend ClassMethods
-
extend Plugins
+
plugin Plugins::Associations
plugin Plugins::Clone
plugin Plugins::Descendants
View
28 lib/mongo_mapper/plugins/protected.rb
@@ -2,29 +2,33 @@ module MongoMapper
module Plugins
module Protected
module ClassMethods
- def attr_protected( *attrs )
- self.write_inheritable_attribute( :attr_protected, Set.new(attrs) + (protected_attributes || []) )
+ def attr_protected(*attrs)
+ self.write_inheritable_attribute(:attr_protected, Set.new(attrs) + (protected_attributes || []))
end
+
def protected_attributes
- self.read_inheritable_attribute( :attr_protected )
+ self.read_inheritable_attribute(:attr_protected)
end
end
module InstanceMethods
- def update_attributes( attrs = {} )
- super filter_protected_attrs(attrs)
+ def update_attributes(attrs={})
+ super(filter_protected_attrs(attrs))
end
- def update_attributes!( attrs = {} )
- super filter_protected_attrs(attrs)
+ def update_attributes!(attrs={})
+ super(filter_protected_attrs(attrs))
end
- protected
- def filter_protected_attrs( attrs )
- prot_attrs = self.class.protected_attributes
- return attrs unless prot_attrs
- attrs.dup.delete_if { |key, val| prot_attrs.include?(key) }
+ def protected_attributes
+ self.class.protected_attributes
end
+
+ protected
+ def filter_protected_attrs(attrs)
+ return attrs if protected_attributes.blank?
+ attrs.dup.delete_if { |key, val| protected_attributes.include?(key) }
+ end
end
end
end
View
68 test/functional/test_protected.rb
@@ -4,38 +4,80 @@ class ProtectedTest < Test::Unit::TestCase
context 'A document with protected attributes' do
setup do
@doc_class = Doc do
- set_collection_name 'users'
key :name, String
- key :admin, Boolean
+ key :admin, Boolean, :default => false
attr_protected :admin
end
- @doc = @doc_class.create( :name => 'Steve Sloan', :admin => false )
- @doc.admin.should == false
+ @doc = @doc_class.create(:name => 'Steve Sloan')
end
- should 'have protected attributes' do
- @doc_class.protected_attributes.to_a.should == [:admin]
- @doc.class.protected_attributes.to_a.should == [:admin]
+ should 'have protected attributes class method' do
+ @doc_class.protected_attributes.should == [:admin].to_set
+ end
+
+ should "have protected attributes instance method" do
+ @doc.protected_attributes.should equal(@doc_class.protected_attributes)
end
should 'assign protected attribute through accessor' do
@doc.admin = true
- @doc.save!
- @doc.admin.should == true
+ @doc.admin.should be_true
end
should 'ignore protected attribute on #update_attributes' do
- @doc.update_attributes( :name => 'Ren Hoek', :admin => true )
+ @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
@doc.name.should == 'Ren Hoek'
- @doc.admin.should == false
+ @doc.admin.should be_false
end
should 'ignore protected attribute on #update_attributes!' do
- @doc.update_attributes!( :name => 'Stimpson J. Cat', :admin => true )
+ @doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
@doc.name.should == 'Stimpson J. Cat'
- @doc.admin.should == false
+ @doc.admin.should be_false
+ end
+ end
+
+ context 'An embedded document with protected attributes' do
+ setup do
+ @doc_class = Doc('Project')
+ @edoc_class = EDoc('Person') do
+ key :name, String
+ key :admin, Boolean, :default => false
+
+ attr_protected :admin
+ end
+ @doc_class.many :people, :class => @edoc_class
+
+ @doc = @doc_class.create(:title => 'MongoMapper')
+ @edoc = @edoc_class.new(:name => 'Steve Sloan')
+ @doc.people << @edoc
+ end
+
+ should 'have protected attributes class method' do
+ @edoc_class.protected_attributes.should == [:admin].to_set
+ end
+
+ should "have protected attributes instance method" do
+ @edoc.protected_attributes.should equal(@edoc_class.protected_attributes)
+ end
+
+ should 'assign protected attribute through accessor' do
+ @edoc.admin = true
+ @edoc.admin.should be_true
+ end
+
+ should 'ignore protected attribute on #update_attributes' do
+ @edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
+ @edoc.name.should == 'Ren Hoek'
+ @edoc.admin.should be_false
+ end
+
+ should 'ignore protected attribute on #update_attributes!' do
+ @edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
+ @edoc.name.should == 'Stimpson J. Cat'
+ @edoc.admin.should be_false
end
end
end
View
25 test/test_helper.rb
@@ -16,45 +16,40 @@
class Test::Unit::TestCase
include CustomMatchers
-
- cattr_accessor :mm_document_count
- self.mm_document_count = 0
-
+
def Doc(name=nil, &block)
- Test::Unit::TestCase.mm_document_count += 1
-
klass = Class.new do
include MongoMapper::Document
set_collection_name "test#{rand(20)}"
-
+
if name
class_eval "def self.name; '#{name}' end"
class_eval "def self.to_s; '#{name}' end"
end
end
+
klass.class_eval(&block) if block_given?
klass.collection.remove
klass
end
-
+
def EDoc(name=nil, &block)
- Class.new do
+ klass = Class.new do
include MongoMapper::EmbeddedDocument
-
+
if name
class_eval "def self.name; '#{name}' end"
class_eval "def self.to_s; '#{name}' end"
end
-
- class_eval(&block) if block_given?
end
+
+ klass.class_eval(&block) if block_given?
+ klass
end
end
test_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp')
FileUtils.mkdir_p(test_dir) unless File.exist?(test_dir)
-MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, {
- :logger => Logger.new(test_dir + '/test.log')
-})
+MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, {:logger => Logger.new(test_dir + '/test.log')})
MongoMapper.database = 'test'

0 comments on commit 845e602

Please sign in to comment.