fixed a major bug with inheritance and the class database setup. (plus some validation bugs)

This commit is contained in:
Matt Aimonetti 2009-02-10 16:10:35 -08:00
parent e448112ff6
commit c0abafd1e0
6 changed files with 16 additions and 24 deletions

View file

@ -4,18 +4,18 @@ module CouchRest
class Document < Response class Document < Response
include CouchRest::Mixins::Attachments include CouchRest::Mixins::Attachments
# def self.inherited(subklass)
# subklass.send(:class_inheritable_accessor, :database)
# end
class_inheritable_accessor :database
attr_accessor :database attr_accessor :database
@@database = nil
# override the CouchRest::Model-wide default_database # override the CouchRest::Model-wide default_database
# This is not a thread safe operation, do not change the model # This is not a thread safe operation, do not change the model
# database at runtime. # database at runtime.
def self.use_database(db) def self.use_database(db)
@@database = db self.database = db
end
def self.database
@@database
end end
def id def id

View file

@ -118,7 +118,7 @@ module CouchRest
# Overridden to set the unique ID. # Overridden to set the unique ID.
# Returns a boolean value # Returns a boolean value
def save_without_callbacks(bulk = false) def save_without_callbacks(bulk = false)
raise ArgumentError, "a document requires a database to be saved to" unless database raise ArgumentError, "a document requires a database to be saved to (The document or the #{self.class} default database were not set)" unless database
set_unique_id if new_document? && self.respond_to?(:set_unique_id) set_unique_id if new_document? && self.respond_to?(:set_unique_id)
result = database.save_doc(self, bulk) result = database.save_doc(self, bulk)
result["ok"] == true result["ok"] == true

View file

@ -6,7 +6,7 @@ module CouchRest
class Property class Property
# flag letting us know if we already checked the autovalidation settings # flag letting us know if we already checked the autovalidation settings
attr_accessor :autovalidation_check attr_accessor :autovalidation_check
@@autovalidation_check = false @autovalidation_check = false
end end
module Validation module Validation
@ -71,7 +71,7 @@ module CouchRest
# validator to be created for the property. integer_only # validator to be created for the property. integer_only
# is set to true # is set to true
# #
# BigDecimal or Float type # Float type
# Using a Integer type causes a validates_is_number # Using a Integer type causes a validates_is_number
# validator to be created for the property. integer_only # validator to be created for the property. integer_only
# is set to false, and precision/scale match the property # is set to false, and precision/scale match the property
@ -91,13 +91,14 @@ module CouchRest
# It is just shortcut if only one validation option is set # It is just shortcut if only one validation option is set
# #
def auto_generate_validations(property) def auto_generate_validations(property)
return unless (property.autovalidation_check && self.auto_validation && (property.options && property.options.has_key?(:auto_validation) && property.options[:auto_validation])) return unless ((property.autovalidation_check != true) && self.auto_validation)
return if (property.options && (property.options.has_key?(:auto_validation) && !property.options[:auto_validation]) || property.read_only)
# value is set by the storage system # value is set by the storage system
opts = {} opts = {}
opts[:context] = property.options[:validates] if property.options.has_key?(:validates) opts[:context] = property.options[:validates] if property.options.has_key?(:validates)
# presence # presence
unless opts[:allow_nil] if opts[:allow_nil] == false
# validates_present property.name, opts # validates_present property.name, opts
validates_present property.name, options_with_message(opts, property, :presence) validates_present property.name, options_with_message(opts, property, :presence)
end end

View file

@ -12,7 +12,6 @@ class DummyModel < CouchRest::ExtendedDocument
property :casted_attribute, :cast_as => 'WithCastedModelMixin' property :casted_attribute, :cast_as => 'WithCastedModelMixin'
end end
describe CouchRest::CastedModel do describe CouchRest::CastedModel do
describe "A non hash class including CastedModel" do describe "A non hash class including CastedModel" do
@ -59,7 +58,7 @@ describe CouchRest::CastedModel do
describe "saved document with casted models" do describe "saved document with casted models" do
before(:each) do before(:each) do
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever'}) @obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
@obj.save @obj.save.should be_true
end end
it "should be able to load with the casted models" do it "should be able to load with the casted models" do

View file

@ -37,10 +37,7 @@ describe "ExtendedDocument properties" do
it "should be auto timestamped" do it "should be auto timestamped" do
@card.created_at.should be_nil @card.created_at.should be_nil
@card.updated_at.should be_nil @card.updated_at.should be_nil
# :emo: hack for autospec @card.save.should be_true
Card.use_database(TEST_SERVER.default_database) if @card.database.nil?
@card.save #.should be_true
p @card.errors
@card.created_at.should_not be_nil @card.created_at.should_not be_nil
@card.updated_at.should_not be_nil @card.updated_at.should_not be_nil
end end
@ -73,9 +70,6 @@ describe "ExtendedDocument properties" do
it "should let you set an error message" do it "should let you set an error message" do
@invoice.location = nil @invoice.location = nil
@invoice.valid? @invoice.valid?
# require 'ruby-debug'
# debugger
# p @invoice.class.validators.map{|v| v.message}.inspect
@invoice.errors.on(:location).should == ["Hey stupid!, you forgot the location"] @invoice.errors.on(:location).should == ["Hey stupid!, you forgot the location"]
end end
@ -108,7 +102,7 @@ describe "ExtendedDocument properties" do
@service.name = nil @service.name = nil
@service.should_not be_valid @service.should_not be_valid
@service.errors.should_not be_nil @service.errors.should_not be_nil
@service.errors.on(:name).first.should == "Name must not be blank" @service.errors.on(:name).first.should == "Name must be between 4 and 19 characters long"
end end
it "should autovalidate the correct length" do it "should autovalidate the correct length" do

View file

@ -1,7 +1,7 @@
class Service < CouchRest::ExtendedDocument class Service < CouchRest::ExtendedDocument
# Include the validation module to get access to the validation methods # Include the validation module to get access to the validation methods
include CouchRest::Validation include CouchRest::Validation
auto_validate!
# Set the default database to use # Set the default database to use
use_database TEST_SERVER.default_database use_database TEST_SERVER.default_database
@ -9,6 +9,4 @@ class Service < CouchRest::ExtendedDocument
property :name, :length => 4...20 property :name, :length => 4...20
property :price, :type => Integer property :price, :type => Integer
auto_validate!
end end