BUGFIX: attribute protection
Fixes bug where documents recreated from the database were being stripped of their protected attributes when instantiated Signed-off-by: Marcos Tapajos <tapajos@Tapajos-MacBook.local>
This commit is contained in:
parent
5707d89290
commit
d41c7c96da
5 changed files with 84 additions and 17 deletions
|
@ -183,7 +183,7 @@ module CouchRest
|
|||
if @container_class.nil?
|
||||
results
|
||||
else
|
||||
results['rows'].collect { |row| @container_class.new(row['doc']) } unless results['rows'].nil?
|
||||
results['rows'].collect { |row| @container_class.create_from_database(row['doc']) } unless results['rows'].nil?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -51,11 +51,9 @@ module CouchRest
|
|||
# db<Database>:: optional option to pass a custom database to use
|
||||
def get(id, db = database)
|
||||
begin
|
||||
doc = db.get id
|
||||
get!(id, db)
|
||||
rescue
|
||||
nil
|
||||
else
|
||||
new(doc)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -72,11 +70,11 @@ module CouchRest
|
|||
# db<Database>:: optional option to pass a custom database to use
|
||||
def get!(id, db = database)
|
||||
doc = db.get id
|
||||
new(doc)
|
||||
create_from_database(doc)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -137,13 +137,13 @@ module CouchRest
|
|||
collection_proxy_for(design_doc, name, opts.merge({:include_docs => true}))
|
||||
else
|
||||
view = fetch_view db, name, opts.merge({:include_docs => true}), &block
|
||||
view['rows'].collect{|r|new(r['doc'])} if view['rows']
|
||||
view['rows'].collect{|r|create_from_database(r['doc'])} if view['rows']
|
||||
end
|
||||
rescue
|
||||
# fallback for old versions of couchdb that don't
|
||||
# have include_docs support
|
||||
view = fetch_view(db, name, opts, &block)
|
||||
view['rows'].collect{|r|new(db.get(r['id']))} if view['rows']
|
||||
view['rows'].collect{|r|create_from_database(db.get(r['id']))} if view['rows']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -170,4 +170,4 @@ module CouchRest
|
|||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,11 +38,20 @@ module CouchRest
|
|||
define_callbacks :save, "result == :halt"
|
||||
define_callbacks :update, "result == :halt"
|
||||
define_callbacks :destroy, "result == :halt"
|
||||
|
||||
# Creates a new instance, bypassing attribute protection
|
||||
#
|
||||
# ==== Returns
|
||||
# a document instance
|
||||
def self.create_from_database(passed_keys={})
|
||||
new(passed_keys, :directly_set_attributes => true)
|
||||
end
|
||||
|
||||
def initialize(passed_keys={})
|
||||
def initialize(passed_keys={}, options={})
|
||||
apply_defaults # defined in CouchRest::Mixins::Properties
|
||||
set_attributes(passed_keys) unless passed_keys.nil?
|
||||
super
|
||||
remove_protected_attributes(passed_keys) unless options[:directly_set_attributes]
|
||||
directly_set_attributes(passed_keys) unless passed_keys.nil?
|
||||
super(passed_keys)
|
||||
cast_keys # defined in CouchRest::Mixins::Properties
|
||||
unless self['_id'] && self['_rev']
|
||||
self['couchrest-type'] = self.class.to_s
|
||||
|
@ -281,14 +290,18 @@ module CouchRest
|
|||
raise NoMethodError, "#{attribute_name}= method not available, use property :#{attribute_name}" unless self.respond_to?("#{attribute_name}=")
|
||||
end
|
||||
end
|
||||
|
||||
def set_attributes(hash)
|
||||
attrs = remove_protected_attributes(hash)
|
||||
attrs.each do |attribute_name, attribute_value|
|
||||
|
||||
def directly_set_attributes(hash)
|
||||
hash.each do |attribute_name, attribute_value|
|
||||
if self.respond_to?("#{attribute_name}=")
|
||||
self.send("#{attribute_name}=", attrs.delete(attribute_name))
|
||||
self.send("#{attribute_name}=", hash.delete(attribute_name))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def set_attributes(hash)
|
||||
attrs = remove_protected_attributes(hash)
|
||||
directly_set_attributes(attrs)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue