Allow mixing of protected and accessible properties.

Any unspecified properties are now assumed to be protected by default

Signed-off-by: Marcos Tapajós <tapajos@gmail.com>
This commit is contained in:
Will Leinweber 2010-08-11 17:41:32 -05:00 committed by Marcos Tapajós
parent bf22222fd9
commit 49c9656fe3
4 changed files with 67 additions and 55 deletions

3
.gitignore vendored
View file

@ -2,3 +2,6 @@
html/*
pkg
*.swp
Gemfile*
.rvmrc
.bundle

View file

@ -4,6 +4,7 @@
* Minor enhancements
* Raise error on adding objects to "collection_of" without an id
* Allow mixing of protected and accessible properties. Any unspecified properties are now assumed to be protected by default
== CouchRest Model 1.0.0.beta7

View file

@ -1,7 +1,7 @@
module CouchRest
module Model
module AttributeProtection
# Attribute protection from mass assignment to CouchRest properties
# Attribute protection from mass assignment to CouchRest::Model properties
#
# Protected methods will be removed from
# * new
@ -10,15 +10,21 @@ module CouchRest
# * attributes=
#
# There are two modes of protection
# 1) Declare accessible poperties, assume all the rest are protected
# property :name, :accessible => true
# property :admin # this will be automatically protected
# 1) Declare accessible poperties, and assume all unspecified properties are protected
# property :name, :accessible => true
# property :admin # this will be automatically protected
#
# 2) Declare protected properties, assume all the rest are accessible
# property :name # this will not be protected
# 2) Declare protected properties, and assume all unspecified properties are accessible
# property :name # this will not be protected
# property :admin, :protected => true
#
# Note: you cannot set both flags in a single class
# 3) Mix and match, and assume all unspecified properties are protected.
# property :name, :accessible => true
# property :admin, :protected => true
# property :phone # this will be automatically protected
#
# Note: the timestamps! method protectes the created_at and updated_at properties
def self.included(base)
base.extend(ClassMethods)
@ -56,18 +62,13 @@ module CouchRest
private
def properties_to_remove_from_mass_assignment
has_protected = !protected_properties.empty?
has_accessible = !accessible_properties.empty?
to_remove = protected_properties
if !has_protected && !has_accessible
[]
elsif has_protected && !has_accessible
protected_properties
elsif has_accessible && !has_protected
properties.reject { |prop| prop.options[:accessible] }
else
raise "Set either :accessible or :protected for #{self.class}, but not both"
unless accessible_properties.empty?
to_remove += properties.reject { |prop| prop.options[:accessible] }
end
to_remove
end
end
end

View file

@ -94,15 +94,22 @@ describe "Model Attributes" do
end
end
describe "protected flag" do
class WithBoth < CouchRest::Model::Base
describe "Model Base", "mixing protected and accessible flags" do
class WithBothAndUnspecified < CouchRest::Model::Base
use_database TEST_SERVER.default_database
property :name, :accessible => true
property :admin, :default => false, :protected => true
property :phone, :default => 'unset phone number'
end
it "should raise an error when both are set" do
lambda { WithBoth.new }.should raise_error
it { expect { WithBothAndUnspecified.new }.to_not raise_error }
it 'should assume that any unspecified property is protected by default' do
user = WithBothAndUnspecified.new(:name => 'will', :admin => true, :phone => '555-1234')
user.name.should == 'will'
user.admin.should == false
user.phone.should == 'unset phone number'
end
end