Skip to content

Commit

Permalink
use JSON.create_id so we can change the attribute that stores the mod…
Browse files Browse the repository at this point in the history
…el name in the document
  • Loading branch information
Lennart Melzer authored and langalex committed Nov 3, 2009
1 parent 434caaa commit f949948
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
@@ -1,5 +1,8 @@
## Changes

### 0.2.15
* ability to change the name of the attribute that stores the ruby class in the documents by setting JSON.create_id (lennart)

### 0.2.13

* support adding errors in before_validation callbacks (mattmatt)
Expand Down
2 changes: 1 addition & 1 deletion couch_potato.gemspec
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Alexander Lang"]
s.date = %q{2009-10-30}
s.date = %q{2009-10-31}
s.description = %q{Ruby persistence layer for CouchDB}
s.email = %q{alex@upstream-berlin.com}
s.extra_rdoc_files = [
Expand Down
2 changes: 1 addition & 1 deletion couch_potato_js_runner.js
Expand Up @@ -59,7 +59,7 @@ function print_r(x, max, sep, l) {
};


var doc = {"tags":["person","male"],"name":"horst"};
var doc = {"name":"horst","tags":["person","male"]};
var map = function(doc) {emit(doc.name, doc.tags.length);};
var result = [];
var emit = function(key, value) {
Expand Down
4 changes: 2 additions & 2 deletions lib/couch_potato/view/model_view_spec.rb
Expand Up @@ -18,7 +18,7 @@ def view_parameters

def map_function
"function(doc) {
if(doc.ruby_class && doc.ruby_class == '#{@klass.name}') {
if(doc.#{JSON.create_id} && doc.#{JSON.create_id} == '#{@klass.name}') {
emit(#{formatted_key(key)}, null);
}
}"
Expand Down Expand Up @@ -58,4 +58,4 @@ def formatted_key(key)

end
end
end
end
4 changes: 2 additions & 2 deletions lib/couch_potato/view/properties_view_spec.rb
Expand Up @@ -7,7 +7,7 @@ module View
class PropertiesViewSpec < ModelViewSpec
def map_function
"function(doc) {
if(doc.ruby_class && doc.ruby_class == '#{@klass.name}') {
if(doc.#{JSON.create_id} && doc.#{JSON.create_id} == '#{@klass.name}') {
emit(#{formatted_key(key)}, #{properties_for_map(properties)});
}
}"
Expand Down Expand Up @@ -36,4 +36,4 @@ def properties_for_map(properties)

end
end
end
end
2 changes: 1 addition & 1 deletion spec/create_spec.rb
Expand Up @@ -8,7 +8,7 @@
it "should store the class" do
@comment = Comment.new :title => 'my_title'
CouchPotato.database.save_document! @comment
CouchPotato.couchrest_database.get(@comment.id).ruby_class.should == 'Comment'
CouchPotato.couchrest_database.get(@comment.id).send(JSON.create_id).should == 'Comment'
end
end
describe "fails" do
Expand Down
16 changes: 8 additions & 8 deletions spec/custom_view_spec.rb
Expand Up @@ -39,7 +39,7 @@ class CustomBuild < Build
CouchPotato.database.view Build.timeline(:key => 1)
end

it "should not return documents that don't have a matching ruby_class" do
it "should not return documents that don't have a matching JSON.create_id" do
CouchPotato.couchrest_database.save_doc({:time => 'x'})
CouchPotato.database.view(Build.timeline).should == []
end
Expand All @@ -55,24 +55,24 @@ class CustomBuild < Build

describe "properties defined" do
it "should assign the configured properties" do
CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', :ruby_class => 'Build')
CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', JSON.create_id.to_sym => 'Build')
CouchPotato.database.view(Build.minimal_timeline).first.state.should == 'success'
end

it "should not assign the properties not configured" do
CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', :ruby_class => 'Build')
CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', JSON.create_id.to_sym => 'Build')
CouchPotato.database.view(Build.minimal_timeline).first.time.should be_nil
end

it "should assign the id even if it is not configured" do
id = CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', :ruby_class => 'Build')['id']
id = CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', JSON.create_id.to_sym => 'Build')['id']
CouchPotato.database.view(Build.minimal_timeline).first._id.should == id
end
end

describe "no properties defined" do
it "should assign all properties to the objects by default" do
id = CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01', :ruby_class => 'Build'})['id']
id = CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01', JSON.create_id.to_sym => 'Build'})['id']
result = CouchPotato.database.view(Build.timeline).first
result.state.should == 'success'
result.time.should == '2008-01-01'
Expand Down Expand Up @@ -112,8 +112,8 @@ class CustomBuild < Build
CouchPotato.database.view(Build.custom_timeline_returns_docs).map(&:state).should == ['success']
end

it "should still return instance of class if document included 'ruby_class'" do
CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01', :ruby_class => "Build"})
it "should still return instance of class if document included JSON.create_id" do
CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01', JSON.create_id.to_sym => "Build"})
view_data = CouchPotato.database.view(Build.custom_timeline_returns_docs)
view_data.map(&:class).should == [Build]
view_data.map(&:state).should == ['success']
Expand Down Expand Up @@ -163,4 +163,4 @@ class CustomBuild < Build
CouchPotato.database.view(CustomBuild.timeline).first.should be_kind_of(CustomBuild)
end
end
end
end
3 changes: 1 addition & 2 deletions spec/spec_helper.rb
Expand Up @@ -24,8 +24,7 @@ class Comment
end

def recreate_db
CouchPotato.couchrest_database.delete! rescue nil
CouchPotato.couchrest_database.server.create_db CouchPotato::Config.database_name
CouchPotato.couchrest_database.recreate!
end
recreate_db

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/attributes_spec.rb
Expand Up @@ -25,12 +25,12 @@ class Plant
# useful when loading models from custom views
describe "accessing ghost attributes" do
it "should allow me to access attributes that are in the couchdb document but not defined as a property" do
plant = Plant.json_create({"ruby_class" => "Plant", "color" => "red", "leaf_count" => 1})
plant = Plant.json_create({JSON.create_id => "Plant", "color" => "red", "leaf_count" => 1})
plant.color.should == 'red'
end

it "should raise a no method error when trying to read attributes that are not in the document" do
plant = Plant.json_create({"ruby_class" => "Plant", "leaf_count" => 1})
plant = Plant.json_create({JSON.create_id => "Plant", "leaf_count" => 1})
lambda do
plant.length
end.should raise_error(NoMethodError)
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/database_spec.rb
Expand Up @@ -55,13 +55,13 @@ class Child
it "should set itself on the model" do
user = mock 'user'
DbTestUser.stub!(:new).and_return(user)
db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => DbTestUser.json_create({'ruby_class' => 'DbTestUser'})))
db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => DbTestUser.json_create({JSON.create_id => 'DbTestUser'})))
user.should_receive(:database=).with(db)
db.load '1'
end

it "should load namespaced models" do
db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => Parent::Child.json_create({'ruby_class' => 'Parent::Child'})))
db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => Parent::Child.json_create({JSON.create_id => 'Parent::Child'})))
db.load('1').class.should == Parent::Child
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/dirty_attributes_spec.rb
Expand Up @@ -86,7 +86,7 @@ class Plate

describe "object loaded from database" do
before(:each) do
couchrest_db = stub('database', :get => Plate.json_create({'_id' => '1', '_rev' => '2', 'food' => 'sushi', 'ruby_class' => 'Plate'}), :info => nil)
couchrest_db = stub('database', :get => Plate.json_create({'_id' => '1', '_rev' => '2', 'food' => 'sushi', JSON.create_id => 'Plate'}), :info => nil)
@plate = CouchPotato::Database.new(couchrest_db).load_document '1'
end

Expand All @@ -104,7 +104,7 @@ class Plate
end

it "should return true if array attribute changed" do
couchrest_db = stub('database', :get => Plate.json_create({'_id' => '1', '_rev' => '2', 'food' => ['sushi'], 'ruby_class' => 'Plate'}), :info => nil)
couchrest_db = stub('database', :get => Plate.json_create({'_id' => '1', '_rev' => '2', 'food' => ['sushi'], JSON.create_id => 'Plate'}), :info => nil)
plate = CouchPotato::Database.new(couchrest_db).load_document '1'
plate.food << 'burger'
plate.should be_food_changed
Expand All @@ -119,7 +119,7 @@ class Plate

describe "after save" do
it "should reset all attributes to not dirty" do
couchrest_db = stub('database', :get => Plate.json_create({'_id' => '1', '_rev' => '2', 'food' => 'sushi', 'ruby_class' => 'Plate'}), :info => nil, :save_doc => {})
couchrest_db = stub('database', :get => Plate.json_create({'_id' => '1', '_rev' => '2', 'food' => 'sushi', JSON.create_id => 'Plate'}), :info => nil, :save_doc => {})
db = CouchPotato::Database.new(couchrest_db)
@plate = db.load_document '1'
@plate.food = 'burger'
Expand Down

0 comments on commit f949948

Please sign in to comment.