2010-05-10 21:19:24 +02:00
|
|
|
require File.expand_path("../../spec_helper", __FILE__)
|
2010-06-20 22:01:11 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'cat')
|
2010-04-16 02:51:59 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'person')
|
2009-03-20 02:53:17 +01:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'card')
|
2009-03-26 05:04:32 +01:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'course')
|
2009-03-20 02:53:17 +01:00
|
|
|
|
|
|
|
# add a default value
|
|
|
|
Card.property :bg_color, :default => '#ccc'
|
|
|
|
|
|
|
|
class BusinessCard < Card
|
|
|
|
property :extension_code
|
|
|
|
property :job_title
|
2010-06-20 22:01:11 +02:00
|
|
|
|
|
|
|
validates_presence_of :extension_code
|
|
|
|
validates_presence_of :job_title
|
2009-03-20 02:53:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
class DesignBusinessCard < BusinessCard
|
|
|
|
property :bg_color, :default => '#eee'
|
|
|
|
end
|
|
|
|
|
2009-03-26 05:52:03 +01:00
|
|
|
class OnlineCourse < Course
|
|
|
|
property :url
|
|
|
|
view_by :url
|
|
|
|
end
|
2009-03-26 05:04:32 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
class Animal < CouchRest::Model::Base
|
2009-03-26 05:04:32 +01:00
|
|
|
use_database TEST_SERVER.default_database
|
|
|
|
property :name
|
|
|
|
view_by :name
|
|
|
|
end
|
|
|
|
|
|
|
|
class Dog < Animal; end
|
2009-03-20 02:53:17 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
describe "Subclassing a Model" do
|
2009-03-20 02:53:17 +01:00
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@card = BusinessCard.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shouldn't messup the parent's properties" do
|
|
|
|
Card.properties.should_not == BusinessCard.properties
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should share the same db default" do
|
|
|
|
@card.database.uri.should == Card.database.uri
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have kept the validation details" do
|
|
|
|
@card.should_not be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have added the new validation details" do
|
2010-06-20 22:01:11 +02:00
|
|
|
validated_fields = @card.class.validators.map{|v| v.attributes}.flatten
|
|
|
|
validated_fields.should include(:extension_code)
|
2009-03-20 02:53:17 +01:00
|
|
|
validated_fields.should include(:job_title)
|
|
|
|
end
|
|
|
|
|
2009-03-25 06:18:28 +01:00
|
|
|
it "should not add to the parent's validations" do
|
2010-06-20 22:01:11 +02:00
|
|
|
validated_fields = Card.validators.map{|v| v.attributes}.flatten
|
2009-03-25 06:18:28 +01:00
|
|
|
validated_fields.should_not include(:extension_code)
|
|
|
|
validated_fields.should_not include(:job_title)
|
|
|
|
end
|
|
|
|
|
2009-03-20 02:53:17 +01:00
|
|
|
it "should inherit default property values" do
|
|
|
|
@card.bg_color.should == '#ccc'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to overwrite a default property" do
|
|
|
|
DesignBusinessCard.new.bg_color.should == '#eee'
|
|
|
|
end
|
|
|
|
|
2009-03-26 05:04:32 +01:00
|
|
|
it "should have a design doc slug based on the subclass name" do
|
|
|
|
Course.refresh_design_doc
|
|
|
|
OnlineCourse.design_doc_slug.should =~ /^OnlineCourse/
|
|
|
|
end
|
|
|
|
|
2009-03-26 05:52:03 +01:00
|
|
|
it "should have its own design_doc_fresh" do
|
2009-03-26 05:04:32 +01:00
|
|
|
Animal.refresh_design_doc
|
2010-04-06 19:51:17 +02:00
|
|
|
Dog.send(:design_doc_fresh, Dog.database).should_not == true
|
2009-03-26 05:04:32 +01:00
|
|
|
Dog.refresh_design_doc
|
2010-04-06 19:51:17 +02:00
|
|
|
Dog.send(:design_doc_fresh, Dog.database).should == true
|
2009-03-26 05:04:32 +01:00
|
|
|
end
|
|
|
|
|
2009-03-26 05:52:03 +01:00
|
|
|
it "should not add views to the parent's design_doc" do
|
|
|
|
Course.design_doc['views'].keys.should_not include('by_url')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not add the parent's views to its design doc" do
|
|
|
|
Course.refresh_design_doc
|
|
|
|
OnlineCourse.refresh_design_doc
|
|
|
|
OnlineCourse.design_doc['views'].keys.should_not include('by_title')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have an all view with a guard clause for couchrest-type == subclass name in the map function" do
|
|
|
|
OnlineCourse.design_doc['views']['all']['map'].should =~ /if \(doc\['couchrest-type'\] == 'OnlineCourse'\)/
|
|
|
|
end
|
2009-03-20 02:53:17 +01:00
|
|
|
end
|
|
|
|
|