Upgrading to use CouchRest 1.1.0.pre3 and new Hash-less design
This commit is contained in:
parent
6e025bb256
commit
7c7ee2c2b1
19 changed files with 197 additions and 137 deletions
|
@ -1,6 +1,6 @@
|
|||
module CouchRest
|
||||
module Model
|
||||
class Base < Document
|
||||
class Base < CouchRest::Document
|
||||
|
||||
extend ActiveModel::Naming
|
||||
|
||||
|
@ -51,14 +51,15 @@ module CouchRest
|
|||
#
|
||||
# If a block is provided the new model will be passed into the
|
||||
# block so that it can be populated.
|
||||
def initialize(doc = {}, options = {})
|
||||
doc = prepare_all_attributes(doc, options)
|
||||
# set the instances database, if provided
|
||||
def initialize(attributes = {}, options = {})
|
||||
super()
|
||||
prepare_all_attributes(attributes, options)
|
||||
# set the instance's database, if provided
|
||||
self.database = options[:database] unless options[:database].nil?
|
||||
super(doc)
|
||||
unless self['_id'] && self['_rev']
|
||||
self[self.model_type_key] = self.class.to_s
|
||||
end
|
||||
|
||||
yield self if block_given?
|
||||
|
||||
after_initialize if respond_to?(:after_initialize)
|
||||
|
@ -79,16 +80,6 @@ module CouchRest
|
|||
super
|
||||
end
|
||||
|
||||
## Compatibility with ActiveSupport and older frameworks
|
||||
|
||||
# Hack so that CouchRest::Document, which descends from Hash,
|
||||
# doesn't appear to Rails routing as a Hash of options
|
||||
def is_a?(klass)
|
||||
return false if klass == Hash
|
||||
super
|
||||
end
|
||||
alias :kind_of? :is_a?
|
||||
|
||||
def persisted?
|
||||
!new?
|
||||
end
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
module CouchRest
|
||||
module Model
|
||||
module DocumentQueries
|
||||
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
|
||||
|
||||
# Load all documents that have the model_type_key's field equal to the
|
||||
# name of the current class. Take the standard set of
|
||||
# CouchRest::Database#view options.
|
||||
|
@ -73,7 +70,7 @@ module CouchRest
|
|||
end
|
||||
end
|
||||
alias :find :get
|
||||
|
||||
|
||||
# Load a document from the database by id
|
||||
# An exception will be raised if the document isn't found
|
||||
#
|
||||
|
|
|
@ -106,14 +106,17 @@ module CouchRest
|
|||
|
||||
module ClassMethods
|
||||
|
||||
# Creates a new instance, bypassing attribute protection
|
||||
# Creates a new instance, bypassing attribute protection and
|
||||
# uses the type field to determine which model to use to instanatiate
|
||||
# the new object.
|
||||
#
|
||||
# ==== Returns
|
||||
# a document instance
|
||||
#
|
||||
def build_from_database(doc = {})
|
||||
base = (doc[model_type_key].blank? || doc[model_type_key] == self.to_s) ? self : doc[model_type_key].constantize
|
||||
base.new(doc, :directly_set_attributes => true)
|
||||
def build_from_database(doc = {}, options = {}, &block)
|
||||
src = doc[model_type_key]
|
||||
base = (src.blank? || src == self.to_s) ? self : src.constantize
|
||||
base.new(doc, options.merge(:directly_set_attributes => true), &block)
|
||||
end
|
||||
|
||||
# Defines an instance and save it directly to the database
|
||||
|
|
|
@ -80,17 +80,18 @@ module CouchRest
|
|||
self.disable_dirty = dirty
|
||||
end
|
||||
|
||||
def prepare_all_attributes(doc = {}, options = {})
|
||||
def prepare_all_attributes(attrs = {}, options = {})
|
||||
self.disable_dirty = !!options[:directly_set_attributes]
|
||||
apply_all_property_defaults
|
||||
if options[:directly_set_attributes]
|
||||
directly_set_read_only_attributes(doc)
|
||||
directly_set_read_only_attributes(attrs)
|
||||
directly_set_attributes(attrs, true)
|
||||
else
|
||||
doc = remove_protected_attributes(doc)
|
||||
attrs = remove_protected_attributes(attrs)
|
||||
directly_set_attributes(attrs)
|
||||
end
|
||||
res = doc.nil? ? doc : directly_set_attributes(doc)
|
||||
self.disable_dirty = false
|
||||
res
|
||||
self
|
||||
end
|
||||
|
||||
def find_property!(property)
|
||||
|
@ -101,16 +102,13 @@ module CouchRest
|
|||
|
||||
# Set all the attributes and return a hash with the attributes
|
||||
# that have not been accepted.
|
||||
def directly_set_attributes(hash)
|
||||
hash.reject do |attribute_name, attribute_value|
|
||||
if self.respond_to?("#{attribute_name}=")
|
||||
self.send("#{attribute_name}=", attribute_value)
|
||||
true
|
||||
elsif mass_assign_any_attribute # config option
|
||||
self[attribute_name] = attribute_value
|
||||
true
|
||||
else
|
||||
false
|
||||
def directly_set_attributes(hash, mass_assign = false)
|
||||
return if hash.nil?
|
||||
hash.reject do |key, value|
|
||||
if self.respond_to?("#{key}=")
|
||||
self.send("#{key}=", value)
|
||||
elsif mass_assign || mass_assign_any_attribute
|
||||
self[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -26,7 +26,7 @@ module CouchRest::Model
|
|||
if type.is_a?(Array)
|
||||
if value.nil?
|
||||
value = []
|
||||
elsif [Hash, HashWithIndifferentAccess].include?(value.class)
|
||||
elsif value.is_a?(Hash)
|
||||
# Assume provided as a Hash where key is index!
|
||||
data = value
|
||||
value = [ ]
|
||||
|
@ -39,7 +39,7 @@ module CouchRest::Model
|
|||
arr = value.collect { |data| cast_value(parent, data) }
|
||||
# allow casted_by calls to be passed up chain by wrapping in CastedArray
|
||||
CastedArray.new(arr, self, parent)
|
||||
elsif (type == Object || type == Hash) && (value.class == Hash)
|
||||
elsif (type == Object || type == Hash) && (value.is_a?(Hash))
|
||||
# allow casted_by calls to be passed up chain by wrapping in CastedHash
|
||||
CastedHash[value, self, parent]
|
||||
elsif !value.nil?
|
||||
|
|
|
@ -73,12 +73,12 @@ module CouchRest
|
|||
end
|
||||
|
||||
# Base
|
||||
def new(*args)
|
||||
proxy_update(model.new(*args))
|
||||
def new(attrs = {}, options = {}, &block)
|
||||
proxy_block_update(:new, attrs, options, &block)
|
||||
end
|
||||
|
||||
def build_from_database(doc = {})
|
||||
proxy_update(model.build_from_database(doc))
|
||||
def build_from_database(attrs = {}, options = {}, &block)
|
||||
proxy_block_update(:build_from_database, attrs, options, &block)
|
||||
end
|
||||
|
||||
def method_missing(m, *args, &block)
|
||||
|
@ -170,6 +170,13 @@ module CouchRest
|
|||
end
|
||||
end
|
||||
|
||||
def proxy_block_update(method, *args, &block)
|
||||
model.send(method, *args) do |doc|
|
||||
proxy_update(doc)
|
||||
yield doc if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,7 +18,7 @@ CouchRest::Design.class_eval do
|
|||
flatten =
|
||||
lambda {|r|
|
||||
(recurse = lambda {|v|
|
||||
if v.is_a?(Hash)
|
||||
if v.is_a?(Hash) || v.is_a?(CouchRest::Document)
|
||||
v.to_a.map{|v| recurse.call(v)}.flatten
|
||||
elsif v.is_a?(Array)
|
||||
v.flatten.map{|v| recurse.call(v)}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue