Skip to content

Commit f929659

Browse files
committed
Fixing model_type_value issue
1 parent 18559ee commit f929659

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

history.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CouchRest Model Change History
22

3+
## 2.0.2 - pending
4+
5+
* Fix for model_type_value, not being used correctly from database.
6+
37
## 2.0.1 - 2013-12-03
48

59
* nil keys in view requests are now sent to server, avoiding returning first document issue. (Thanks to @svoboda-jan for pointer.)

lib/couchrest/model/persistence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ module ClassMethods
120120
#
121121
def build_from_database(doc = {}, options = {}, &block)
122122
src = doc[model_type_key]
123-
base = (src.blank? || src == self.to_s) ? self : src.constantize
123+
base = (src.blank? || src == model_type_value) ? self : src.constantize
124124
base.new(doc, options.merge(:directly_set_attributes => true), &block)
125125
end
126126

spec/unit/persistence_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
doc.class.should eql(Article)
2020
end
2121

22-
it "should instantialize document of different type" do
22+
it "should instantiate document of different type" do
2323
doc = Article.build_from_database({'_id' => 'testitem2', '_rev' => 123, Article.model_type_key => 'WithTemplateAndUniqueID', 'name' => 'my test'})
2424
doc.class.should eql(WithTemplateAndUniqueID)
2525
end
2626

2727
end
2828

29+
2930
describe "basic saving and retrieving" do
3031
it "should work fine" do
3132
@obj.name = "should be easily saved and retrieved"
@@ -482,6 +483,32 @@
482483
it "should always return string value of class" do
483484
Article.model_type_value.should eql('Article')
484485
end
486+
487+
describe "usage" do
488+
let :klass do
489+
Class.new(CouchRest::Model::Base) do
490+
property :name, String
491+
def self.model_type_value
492+
'something_else'
493+
end
494+
end
495+
end
496+
it "should use the model type value if overridden" do
497+
obj = klass.build_from_database(
498+
'_id' => '1234', 'type' => 'something_else', 'name' => 'Test'
499+
)
500+
obj['type'].should eql('something_else')
501+
obj.name.should eql('Test')
502+
end
503+
it "should fail if different model type value provided" do
504+
expect {
505+
obj = klass.build_from_database(
506+
'_id' => '1234', 'type' => 'something', 'name' => 'Test'
507+
)
508+
}.to raise_error(NameError)
509+
end
510+
511+
end
485512
end
486513

487514
end

0 commit comments

Comments
 (0)