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:
parent
bf22222fd9
commit
49c9656fe3
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,3 +2,6 @@
|
||||||
html/*
|
html/*
|
||||||
pkg
|
pkg
|
||||||
*.swp
|
*.swp
|
||||||
|
Gemfile*
|
||||||
|
.rvmrc
|
||||||
|
.bundle
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
* Minor enhancements
|
* Minor enhancements
|
||||||
* Raise error on adding objects to "collection_of" without an id
|
* 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
|
== CouchRest Model 1.0.0.beta7
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module CouchRest
|
module CouchRest
|
||||||
module Model
|
module Model
|
||||||
module AttributeProtection
|
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
|
# Protected methods will be removed from
|
||||||
# * new
|
# * new
|
||||||
|
@ -10,15 +10,21 @@ module CouchRest
|
||||||
# * attributes=
|
# * attributes=
|
||||||
#
|
#
|
||||||
# There are two modes of protection
|
# There are two modes of protection
|
||||||
# 1) Declare accessible poperties, assume all the rest are protected
|
# 1) Declare accessible poperties, and assume all unspecified properties are protected
|
||||||
# property :name, :accessible => true
|
# property :name, :accessible => true
|
||||||
# property :admin # this will be automatically protected
|
# property :admin # this will be automatically protected
|
||||||
#
|
#
|
||||||
# 2) Declare protected properties, assume all the rest are accessible
|
# 2) Declare protected properties, and assume all unspecified properties are accessible
|
||||||
# property :name # this will not be protected
|
# property :name # this will not be protected
|
||||||
# property :admin, :protected => true
|
# 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)
|
def self.included(base)
|
||||||
base.extend(ClassMethods)
|
base.extend(ClassMethods)
|
||||||
|
@ -56,18 +62,13 @@ module CouchRest
|
||||||
private
|
private
|
||||||
|
|
||||||
def properties_to_remove_from_mass_assignment
|
def properties_to_remove_from_mass_assignment
|
||||||
has_protected = !protected_properties.empty?
|
to_remove = protected_properties
|
||||||
has_accessible = !accessible_properties.empty?
|
|
||||||
|
|
||||||
if !has_protected && !has_accessible
|
unless accessible_properties.empty?
|
||||||
[]
|
to_remove += properties.reject { |prop| prop.options[: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"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
to_remove
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -94,15 +94,22 @@ describe "Model Attributes" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "protected flag" do
|
describe "Model Base", "mixing protected and accessible flags" do
|
||||||
class WithBoth < CouchRest::Model::Base
|
class WithBothAndUnspecified < CouchRest::Model::Base
|
||||||
use_database TEST_SERVER.default_database
|
use_database TEST_SERVER.default_database
|
||||||
property :name, :accessible => true
|
property :name, :accessible => true
|
||||||
property :admin, :default => false, :protected => true
|
property :admin, :default => false, :protected => true
|
||||||
|
property :phone, :default => 'unset phone number'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise an error when both are set" do
|
it { expect { WithBothAndUnspecified.new }.to_not raise_error }
|
||||||
lambda { WithBoth.new }.should 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue