Adding support for setting types with hash

improve_associations
Sam Lown 2010-05-13 00:17:30 +02:00
parent b0d2258bd3
commit 89c45ebb87
4 changed files with 34 additions and 5 deletions

View File

@ -11,6 +11,7 @@
* Added support for instantiation of documents read from database as couchrest-type provided (Sam Lown)
* Improved attachment handling for detecting file type (Sam Lown)
* Removing some monkey patches and relying on active_support for constantize and humanize (Sam Lown)
* Added support for setting type directly on property (Sam Lown)
== 1.0.2

View File

@ -82,10 +82,18 @@ module CouchRest
module ClassMethods
def property(name, options={})
def property(name, *options)
opts = { }
type = options.shift
if type.class != Hash
opts[:type] = type
opts.merge!(options.shift || {})
else
opts.update(type)
end
existing_property = self.properties.find{|p| p.name == name.to_s}
if existing_property.nil? || (existing_property.default != options[:default])
define_property(name, options)
if existing_property.nil? || (existing_property.default != opts[:default])
define_property(name, opts)
end
end

View File

@ -18,6 +18,14 @@ describe "ExtendedDocument" do
property :name
timestamps!
end
class WithSimplePropertyType < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
property :name, String
property :preset, String, :default => 'none'
property :tags, [String]
timestamps!
end
class WithCallBacks < CouchRest::ExtendedDocument
include ::CouchRest::Validation
@ -277,6 +285,18 @@ describe "ExtendedDocument" do
obj.read_only_with_default.should == 'generic'
end
end
describe "simplified way of setting property types" do
it "should set defaults" do
obj = WithSimplePropertyType.new
obj.preset.should eql('none')
end
it "should handle arrays" do
obj = WithSimplePropertyType.new(:tags => ['spec'])
obj.tags.should == ['spec']
end
end
describe "a doc with template values (CR::Model spec)" do
before(:all) do

View File

@ -15,8 +15,8 @@ class Cat < CouchRest::ExtendedDocument
use_database DB
property :name, :accessible => true
property :toys, :cast_as => [CatToy], :default => [], :accessible => true
property :favorite_toy, :cast_as => CatToy, :accessible => true
property :toys, [CatToy], :default => [], :accessible => true
property :favorite_toy, CatToy, :accessible => true
property :number
end