Permalink
Browse files

Added protected attribute plugin.

  • Loading branch information...
1 parent 767049c commit e5571aba4e64c0c2896b383d104fd349c340cf9b @CodeMonkeySteve CodeMonkeySteve committed with jnunemaker Jan 13, 2010
View
@@ -117,6 +117,7 @@ def self.normalize_object_id(value)
require 'mongo_mapper/plugins/keys'
require 'mongo_mapper/plugins/logger'
require 'mongo_mapper/plugins/pagination'
+require 'mongo_mapper/plugins/protected'
require 'mongo_mapper/plugins/rails'
require 'mongo_mapper/plugins/serialization'
require 'mongo_mapper/plugins/validations'
@@ -18,6 +18,7 @@ def self.included(model)
plugin Plugins::Dirty # for now dirty needs to be after keys
plugin Plugins::Logger
plugin Plugins::Pagination
+ plugin Plugins::Protected
plugin Plugins::Rails
plugin Plugins::Serialization
plugin Plugins::Validations
@@ -15,6 +15,7 @@ def self.included(model)
plugin Plugins::Inspect
plugin Plugins::Keys
plugin Plugins::Logger
+ plugin Plugins::Protected
plugin Plugins::Rails
plugin Plugins::Serialization
plugin Plugins::Validations
@@ -0,0 +1,31 @@
+module MongoMapper
+ module Plugins
+ module Protected
+ module ClassMethods
+ 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 )
+ end
+ end
+
+ module InstanceMethods
+ def update_attributes( attrs = {} )
+ super filter_protected_attrs(attrs)
+ end
+
+ 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) }
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,41 @@
+require 'test_helper'
+
+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
+
+ attr_protected :admin
+ end
+
+ @doc = @doc_class.create( :name => 'Steve Sloan', :admin => false )
+ @doc.admin.should == false
+ end
+
+ should 'have protected attributes' do
+ @doc_class.protected_attributes.to_a.should == [:admin]
+ @doc.class.protected_attributes.to_a.should == [:admin]
+ end
+
+ should 'assign protected attribute through accessor' do
+ @doc.admin = true
+ @doc.save!
+ @doc.admin.should == true
+ end
+
+ should 'ignore protected attribute on #update_attributes' do
+ @doc.update_attributes( :name => 'Ren Hoek', :admin => true )
+ @doc.name.should == 'Ren Hoek'
+ @doc.admin.should == false
+ end
+
+ should 'ignore protected attribute on #update_attributes!' do
+ @doc.update_attributes!( :name => 'Stimpson J. Cat', :admin => true )
+ @doc.name.should == 'Stimpson J. Cat'
+ @doc.admin.should == false
+ end
+ end
+end
View
@@ -31,9 +31,8 @@ def Doc(name=nil, &block)
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.collection.remove
klass
end

0 comments on commit e5571ab

Please sign in to comment.