Adding support for mass_assign_any_attribute config option and refactoring non-api methods into private areas of modules
This commit is contained in:
parent
97347e70e3
commit
1d1d815435
38
README.md
38
README.md
|
@ -72,18 +72,16 @@ but no guarantees!
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
A property is the definition of an attribute, it describes what the attribute is called, how it should
|
A property is the definition of an attribute, it describes what the attribute is called, how it should
|
||||||
be type casted, if at all, and other options such as the default value. These replace your typical
|
be type casted and other options such as the default value. These replace your typical
|
||||||
`add_column` methods typically found in migrations.
|
`add_column` methods typically found in relational database migrations.
|
||||||
|
|
||||||
By default only attributes with a property definition will be stored in CouchRest Model, as opposed
|
Attributes with a property definition will have setter and getter methods defined for them. Any other attibute
|
||||||
to a normal CouchRest Document which will store everything. This however can be disabled using the
|
you'd like to set can be done using the regular CouchRest Document, in the same way you'd update a Hash.
|
||||||
`allow_dynamic_properties` configuration option either for all of CouchRest Model, or for specific
|
|
||||||
models. See the configuration section for more details.
|
|
||||||
|
|
||||||
In its simplest form, a property
|
Properties allow for type casting. Simply provide a Class along with the property definition and CouchRest Model
|
||||||
will only create a getter and setter passing all attribute data directly to the database. Assuming the attribute
|
will convert any value provided to the property into a new instance of the Class.
|
||||||
provided responds to `to_json`, there will not be any problems saving it, but when loading the
|
|
||||||
data back it will either be a string, number, array, or hash:
|
Here are a few examples of the way properties are used:
|
||||||
|
|
||||||
class Cat < CouchRest::Model::Base
|
class Cat < CouchRest::Model::Base
|
||||||
property :name
|
property :name
|
||||||
|
@ -151,6 +149,20 @@ attribute using the `write_attribute` method:
|
||||||
@cat.fall_off_balcony!
|
@cat.fall_off_balcony!
|
||||||
@cat.lives # Now 8!
|
@cat.lives # Now 8!
|
||||||
|
|
||||||
|
Mass assigning attributes is also possible in a similar fashion to ActiveRecord:
|
||||||
|
|
||||||
|
@cat.attributes = {:name => "Felix"}
|
||||||
|
@cat.save
|
||||||
|
|
||||||
|
Is the same as:
|
||||||
|
|
||||||
|
@cat.update_attributes(:name => "Felix")
|
||||||
|
|
||||||
|
Attributes without a property definition however will not be updated this way, this is useful to
|
||||||
|
provent useless data being passed from an HTML form for example. However, if you would like truely
|
||||||
|
dynamic attributes, the `mass_assign_any_attribute` configuration option when set to true will
|
||||||
|
store everything you put into the `Base#attributes=` method.
|
||||||
|
|
||||||
|
|
||||||
## Property Arrays
|
## Property Arrays
|
||||||
|
|
||||||
|
@ -300,19 +312,19 @@ base or for a specific model of your chosing. To configure globally, provide som
|
||||||
following in your projects loading code:
|
following in your projects loading code:
|
||||||
|
|
||||||
CouchRestModel::Model::Base.configure do |config|
|
CouchRestModel::Model::Base.configure do |config|
|
||||||
config.allow_dynamic_properties = true
|
config.mass_assign_any_attribute = true
|
||||||
config.model_type_key = 'couchrest-type'
|
config.model_type_key = 'couchrest-type'
|
||||||
end
|
end
|
||||||
|
|
||||||
To set for a specific model:
|
To set for a specific model:
|
||||||
|
|
||||||
class Cat < CouchRest::Model::Base
|
class Cat < CouchRest::Model::Base
|
||||||
allow_dynamic_properties true
|
mass_assign_any_attribute true
|
||||||
end
|
end
|
||||||
|
|
||||||
Options currently avilable are:
|
Options currently avilable are:
|
||||||
|
|
||||||
* `allow_dynamic_properties` - false by default, when true properties do not need to be defined to be stored, although they will have no accessors.
|
* `mass_assign_any_attribute` - false by default, when true any attribute may be updated via the update_attributes or attributes= methods.
|
||||||
* `model_type_key` - 'model' by default, useful for migrating from an older CouchRest ExtendedDocument when the default used to be 'couchrest-type'.
|
* `model_type_key` - 'model' by default, useful for migrating from an older CouchRest ExtendedDocument when the default used to be 'couchrest-type'.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
* Major enhancements
|
* Major enhancements
|
||||||
* IMPORTANT: Model's class name key changed from 'couchrest-type' to 'model'
|
* IMPORTANT: Model's class name key changed from 'couchrest-type' to 'model'
|
||||||
* Support for configuration module and "model_type_key" option for overriding model's type key
|
* Support for configuration module and "model_type_key" option for overriding model's type key
|
||||||
|
* Added "mass_assign_any_attribute" configuration option to allow setting anything via the attribute= method.
|
||||||
|
|
||||||
* Minor enhancements
|
* Minor enhancements
|
||||||
* Fixing find("") issue (thanks epochwolf)
|
* Fixing find("") issue (thanks epochwolf)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
module CouchRest
|
module CouchRest
|
||||||
module Model
|
module Model
|
||||||
module AttributeProtection
|
module AttributeProtection
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
# Attribute protection from mass assignment to CouchRest::Model properties
|
# Attribute protection from mass assignment to CouchRest::Model properties
|
||||||
#
|
#
|
||||||
# Protected methods will be removed from
|
# Protected methods will be removed from
|
||||||
|
|
|
@ -14,7 +14,6 @@ module CouchRest
|
||||||
include CouchRest::Model::ClassProxy
|
include CouchRest::Model::ClassProxy
|
||||||
include CouchRest::Model::Collection
|
include CouchRest::Model::Collection
|
||||||
include CouchRest::Model::AttributeProtection
|
include CouchRest::Model::AttributeProtection
|
||||||
include CouchRest::Model::Attributes
|
|
||||||
include CouchRest::Model::Associations
|
include CouchRest::Model::Associations
|
||||||
include CouchRest::Model::Validations
|
include CouchRest::Model::Validations
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,9 @@ module CouchRest::Model
|
||||||
|
|
||||||
included do
|
included do
|
||||||
include CouchRest::Model::Configuration
|
include CouchRest::Model::Configuration
|
||||||
include CouchRest::Model::AttributeProtection
|
|
||||||
include CouchRest::Model::Attributes
|
|
||||||
include CouchRest::Model::Callbacks
|
include CouchRest::Model::Callbacks
|
||||||
include CouchRest::Model::Properties
|
include CouchRest::Model::Properties
|
||||||
|
include CouchRest::Model::AttributeProtection
|
||||||
include CouchRest::Model::Associations
|
include CouchRest::Model::Associations
|
||||||
include CouchRest::Model::Validations
|
include CouchRest::Model::Validations
|
||||||
attr_accessor :casted_by
|
attr_accessor :casted_by
|
||||||
|
|
|
@ -9,11 +9,11 @@ module CouchRest
|
||||||
|
|
||||||
included do
|
included do
|
||||||
add_config :model_type_key
|
add_config :model_type_key
|
||||||
add_config :allow_dynamic_properties
|
add_config :mass_assign_any_attribute
|
||||||
|
|
||||||
configure do |config|
|
configure do |config|
|
||||||
config.model_type_key = 'model'
|
config.model_type_key = 'model'
|
||||||
config.allow_dynamic_properties = false
|
config.mass_assign_any_attribute = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,16 +2,12 @@
|
||||||
module CouchRest
|
module CouchRest
|
||||||
module Model
|
module Model
|
||||||
module Properties
|
module Properties
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
class IncludeError < StandardError; end
|
included do
|
||||||
|
extlib_inheritable_accessor(:properties) unless self.respond_to?(:properties)
|
||||||
def self.included(base)
|
self.properties ||= []
|
||||||
base.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=))
|
||||||
extlib_inheritable_accessor(:properties) unless self.respond_to?(:properties)
|
|
||||||
self.properties ||= []
|
|
||||||
EOS
|
|
||||||
base.extend(ClassMethods)
|
|
||||||
raise CouchRest::Mixins::Properties::IncludeError, "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (base.new.respond_to?(:[]) && base.new.respond_to?(:[]=))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the Class properties
|
# Returns the Class properties
|
||||||
|
@ -22,16 +18,36 @@ module CouchRest
|
||||||
self.class.properties
|
self.class.properties
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Read the casted value of an attribute defined with a property.
|
||||||
|
#
|
||||||
|
# ==== Returns
|
||||||
|
# Object:: the casted attibutes value.
|
||||||
def read_attribute(property)
|
def read_attribute(property)
|
||||||
prop = find_property!(property)
|
self[find_property!(property).to_s]
|
||||||
self[prop.to_s]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Store a casted value in the current instance of an attribute defined
|
||||||
|
# with a property.
|
||||||
def write_attribute(property, value)
|
def write_attribute(property, value)
|
||||||
prop = find_property!(property)
|
prop = find_property!(property)
|
||||||
self[prop.to_s] = prop.is_a?(String) ? value : prop.cast(self, value)
|
self[prop.to_s] = prop.is_a?(String) ? value : prop.cast(self, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Takes a hash as argument, and applies the values by using writer methods
|
||||||
|
# for each key. It doesn't save the document at the end. Raises a NoMethodError if the corresponding methods are
|
||||||
|
# missing. In case of error, no attributes are changed.
|
||||||
|
def update_attributes_without_saving(hash)
|
||||||
|
# Remove any protected and update all the rest. Any attributes
|
||||||
|
# which do not have a property will simply be ignored.
|
||||||
|
attrs = remove_protected_attributes(hash)
|
||||||
|
directly_set_attributes(attrs)
|
||||||
|
end
|
||||||
|
alias :attributes= :update_attributes_without_saving
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
# The following methods should be accessable by the Model::Base Class, but not by anything else!
|
||||||
|
|
||||||
def apply_all_property_defaults
|
def apply_all_property_defaults
|
||||||
return if self.respond_to?(:new?) && (new? == false)
|
return if self.respond_to?(:new?) && (new? == false)
|
||||||
# TODO: cache the default object
|
# TODO: cache the default object
|
||||||
|
@ -40,14 +56,48 @@ module CouchRest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def prepare_all_attributes(doc = {}, options = {})
|
||||||
|
apply_all_property_defaults
|
||||||
|
if options[:directly_set_attributes]
|
||||||
|
directly_set_read_only_attributes(doc)
|
||||||
|
else
|
||||||
|
remove_protected_attributes(doc)
|
||||||
|
end
|
||||||
|
directly_set_attributes(doc) unless doc.nil?
|
||||||
|
end
|
||||||
|
|
||||||
def find_property!(property)
|
def find_property!(property)
|
||||||
prop = property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
|
prop = property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
|
||||||
raise ArgumentError, "Missing property definition for #{property.to_s}" unless allow_dynamic_properties or !prop.nil?
|
raise ArgumentError, "Missing property definition for #{property.to_s}" if prop.nil?
|
||||||
prop || property
|
prop
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def directly_set_attributes(hash)
|
||||||
|
hash.each do |attribute_name, attribute_value|
|
||||||
|
if self.respond_to?("#{attribute_name}=")
|
||||||
|
self.send("#{attribute_name}=", hash.delete(attribute_name))
|
||||||
|
elsif mass_assign_any_attribute # config option
|
||||||
|
self[attribute_name] = attribute_value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def directly_set_read_only_attributes(hash)
|
||||||
|
property_list = self.properties.map{|p| p.name}
|
||||||
|
hash.each do |attribute_name, attribute_value|
|
||||||
|
next if self.respond_to?("#{attribute_name}=")
|
||||||
|
if property_list.include?(attribute_name)
|
||||||
|
write_attribute(attribute_name, hash.delete(attribute_name))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_attributes(hash)
|
||||||
|
attrs = remove_protected_attributes(hash)
|
||||||
|
directly_set_attributes(attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
||||||
def property(name, *options, &block)
|
def property(name, *options, &block)
|
||||||
|
|
|
@ -46,7 +46,6 @@ require "couchrest/model/extended_attachments"
|
||||||
require "couchrest/model/class_proxy"
|
require "couchrest/model/class_proxy"
|
||||||
require "couchrest/model/collection"
|
require "couchrest/model/collection"
|
||||||
require "couchrest/model/attribute_protection"
|
require "couchrest/model/attribute_protection"
|
||||||
require "couchrest/model/attributes"
|
|
||||||
require "couchrest/model/associations"
|
require "couchrest/model/associations"
|
||||||
require "couchrest/model/configuration"
|
require "couchrest/model/configuration"
|
||||||
|
|
||||||
|
|
|
@ -73,9 +73,7 @@ describe CouchRest::Model::Base do
|
||||||
|
|
||||||
it "should be possible to override on class using configure method" do
|
it "should be possible to override on class using configure method" do
|
||||||
Cat.instance_eval do
|
Cat.instance_eval do
|
||||||
configure do |config|
|
model_type_key 'cat-type'
|
||||||
config.model_type_key = 'cat-type'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
CouchRest::Model::Base.model_type_key.should eql(@default_model_key)
|
CouchRest::Model::Base.model_type_key.should eql(@default_model_key)
|
||||||
Cat.model_type_key.should eql('cat-type')
|
Cat.model_type_key.should eql('cat-type')
|
||||||
|
|
|
@ -88,12 +88,6 @@ describe "Model properties" do
|
||||||
expect { @card.write_attribute(:this_property_should_not_exist, 823) }.to raise_error(ArgumentError)
|
expect { @card.write_attribute(:this_property_should_not_exist, 823) }.to raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not raise an error if the property does not exist and dynamic properties are allowed' do
|
|
||||||
@card.class.allow_dynamic_properties = true
|
|
||||||
expect { @card.write_attribute(:this_property_should_not_exist, 823) }.to_not raise_error(ArgumentError)
|
|
||||||
@card.class.allow_dynamic_properties = false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
it "should let you use write_attribute on readonly properties" do
|
it "should let you use write_attribute on readonly properties" do
|
||||||
lambda {
|
lambda {
|
||||||
|
@ -116,6 +110,34 @@ describe "Model properties" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "mass updating attributes without property" do
|
||||||
|
|
||||||
|
describe "when mass_assign_any_attribute false" do
|
||||||
|
|
||||||
|
it "should not allow them to be set" do
|
||||||
|
@card.attributes = {:test => 'fooobar'}
|
||||||
|
@card['test'].should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "when mass_assign_any_attribute true" do
|
||||||
|
before(:each) do
|
||||||
|
# dup Card class so that no other tests are effected
|
||||||
|
card_class = Card.dup
|
||||||
|
card_class.class_eval do
|
||||||
|
mass_assign_any_attribute true
|
||||||
|
end
|
||||||
|
@card = card_class.new(:first_name => 'Sam')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should allow them to be updated' do
|
||||||
|
@card.attributes = {:test => 'fooobar'}
|
||||||
|
@card['test'].should eql('fooobar')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
describe "mass assignment protection" do
|
describe "mass assignment protection" do
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue