From 5b84f47c51536f5d5a97279fe5c39f0b93b1ad59 Mon Sep 17 00:00:00 2001 From: hpoydar Date: Sun, 30 Aug 2009 10:00:42 -0400 Subject: [PATCH] Updated view code --- README.md | 4 +- lib/couch_rest_rails/database.rb | 4 +- lib/couch_rest_rails/fixtures.rb | 4 +- lib/couch_rest_rails/tests.rb | 10 +-- lib/couch_rest_rails/views.rb | 75 +++++++++++-------- spec/lib/couch_rest_rails/tests_spec.rb | 5 +- spec/lib/couch_rest_rails/views_spec.rb | 6 +- .../views/bar/views/{ => default}/all/map.js | 0 .../views/foo/views/{ => default}/all/map.js | 0 .../views/foo/views/{ => default}/tags/map.js | 0 .../foo/views/{ => default}/tags/reduce.js | 0 11 files changed, 59 insertions(+), 49 deletions(-) rename spec/mock/views/bar/views/{ => default}/all/map.js (100%) rename spec/mock/views/foo/views/{ => default}/all/map.js (100%) rename spec/mock/views/foo/views/{ => default}/tags/map.js (100%) rename spec/mock/views/foo/views/{ => default}/tags/reduce.js (100%) diff --git a/README.md b/README.md index cb45c0c..cd20a7a 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ This plugin assumes some knowledge of CouchDB and its important differences from Install with the native Rails plugin installation script: - script/plugin install git://github.com/hpoydar/couchrest_rails.git + script/plugin install git://github.com/hpoydar/couchrest-rails.git Or simply add to vendor/plugins and generate the files you need: @@ -140,7 +140,7 @@ The Lucene design documents are stored alongside the views: db/couch//lucene |-- - |-- + |-- You can also customize this path: diff --git a/lib/couch_rest_rails/database.rb b/lib/couch_rest_rails/database.rb index 1f4d12c..6935296 100644 --- a/lib/couch_rest_rails/database.rb +++ b/lib/couch_rest_rails/database.rb @@ -3,7 +3,7 @@ module Database extend self - def create(database_name = '*') + def create(database_name = '*', opts = {}) CouchRestRails.process_database_method(database_name) do |db, response| @@ -43,7 +43,7 @@ def create(database_name = '*') end - def delete(database_name = '*') + def delete(database_name = '*', opts = {}) CouchRestRails.process_database_method(database_name) do |db, response| diff --git a/lib/couch_rest_rails/fixtures.rb b/lib/couch_rest_rails/fixtures.rb index 78d0888..a8aef41 100644 --- a/lib/couch_rest_rails/fixtures.rb +++ b/lib/couch_rest_rails/fixtures.rb @@ -13,7 +13,7 @@ def blurbs res << "Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus. Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non lacus vitae ipsum viverra pretium. Phasellus massa. Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius nibh ut lacus. Curabitur fringilla. Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat urna, ut fermentum neque mi egestas dolor." end - def load(database_name = '*') + def load(database_name = '*', opts = {}) CouchRestRails.process_database_method(database_name) do |db, response| @@ -45,7 +45,7 @@ def load(database_name = '*') end - def dump(database_name = '*') + def dump(database_name = '*', opts = {}) CouchRestRails.process_database_method(database_name) do |db, response| diff --git a/lib/couch_rest_rails/tests.rb b/lib/couch_rest_rails/tests.rb index e5e54dd..761a8ce 100644 --- a/lib/couch_rest_rails/tests.rb +++ b/lib/couch_rest_rails/tests.rb @@ -5,13 +5,13 @@ module Tests mattr_accessor :fixtures_loaded self.fixtures_loaded = Set.new - def setup(database = "*") + def setup(database = "*", opts = {}) ENV['RAILS_ENV'] = CouchRestRails.test_environment unless fixtures_loaded.include?(database) - CouchRestRails::Database.delete(database) - CouchRestRails::Database.create(database) - CouchRestRails::Fixtures.load(database) - CouchRestRails::Views.push(database) + CouchRestRails::Database.delete(database, opts) + CouchRestRails::Database.create(database, opts) + CouchRestRails::Fixtures.load(database, opts) + CouchRestRails::Views.push(database, opts) fixtures_loaded << database end end diff --git a/lib/couch_rest_rails/views.rb b/lib/couch_rest_rails/views.rb index 3162ad6..7eb021d 100644 --- a/lib/couch_rest_rails/views.rb +++ b/lib/couch_rest_rails/views.rb @@ -3,13 +3,19 @@ module Views extend self # Push views to couchdb - def push(database_name = '*', view_name = '*') + def push(database_name = '*', opts = {}) CouchRestRails.process_database_method(database_name) do |db, response| full_db_name = [COUCHDB_CONFIG[:db_prefix], File.basename(db), COUCHDB_CONFIG[:db_suffix]].join full_db_path = [COUCHDB_CONFIG[:host_path], '/', full_db_name].join + # Default to push all views for the given database + view_name = opts[:view_name] || '*' + + # Default to load views from all design documents + design_doc_name = opts[:design_doc_name] || '*' + # Check for CouchDB database if !COUCHDB_SERVER.databases.include?(full_db_name) response << "Database #{db} (#{full_db_name}) does not exist" @@ -22,46 +28,49 @@ def push(database_name = '*', view_name = '*') next end - # Assemble views for each design doc + # Assemble views for each design document db_conn = CouchRest.database(full_db_path) - views = {} - Dir.glob(File.join(RAILS_ROOT, CouchRestRails.views_path, db, "views", view_name)).each do |doc| + + Dir.glob(File.join(RAILS_ROOT, CouchRestRails.views_path, db, "views", design_doc_name)).each do |doc| + + views = {} + couchdb_design_doc = db_conn.get("_design/#{File.basename(doc)}") rescue nil + Dir.glob(File.join(doc, view_name)).each do |view| - # Load views from filesystem - view = assemble_view(doc) - if view.empty? - response << "No view files were found in #{CouchRestRails.views_path}/#{db}/views/#{File.basename(doc)}" - next - else - views[File.basename(doc)] = view - end + # Load view from filesystem + map_reduce = assemble_view(view) + if map_reduce.empty? + response << "No view files were found in #{CouchRestRails.views_path}/#{db}/views/#{File.basename(doc)}/#{File.basename(view)}" + next + else + views[File.basename(view)] = map_reduce + end - # Load views from Couch - couchdb_views = db_conn.get("_design/#{design_doc_name}") rescue nil + # Warn if overwriting views on Couch + if couchdb_design_doc && couchdb_design_doc['views'] && couchdb_design_doc['views'][File.basename(view)] + response << "Overwriting existing view '#{File.basename(view)}' in _design/#{File.basename(doc)}" + end - # Warn if overwriting views on Couch - # merge! - end - end - # Do the save + # Merge with existing views + views = couchdb_design_doc['views'].merge!(views) unless couchdb_design_doc.nil? - # begin - # existing_doc = db_conn.get("_design/#{design_doc_name}") - # if existing_doc - # response << "WARINING: overwriting _design/#{design_doc_name}" - # db_conn.delete_doc(existing_doc) - # end - # rescue - # end + # Save or update + if couchdb_design_doc.nil? + couchdb_design_doc = { + "_id" => "_design/#{File.basename(doc)}", + 'language' => 'javascript', + 'views' => views + } + else + couchdb_design_doc['views'] = views + end + db_conn.save_doc(couchdb_design_doc) - # db_conn.save_doc({ - # "_id" => "_design/#{full_db_name}", - # 'language' => 'javascript', - # :views => views - # }) - # response << "Pushed views to #{full_db_name}/_design/#{full_db_name}" + response << "Pushed views to #{full_db_name}/_design/#{File.basename(doc)}: #{views.keys.join(', ')}" + + end end diff --git a/spec/lib/couch_rest_rails/tests_spec.rb b/spec/lib/couch_rest_rails/tests_spec.rb index 52fa8f1..91a8858 100644 --- a/spec/lib/couch_rest_rails/tests_spec.rb +++ b/spec/lib/couch_rest_rails/tests_spec.rb @@ -26,7 +26,7 @@ CouchRestRails::Tests.setup('foo') db.documents['rows'].size.should == 11 # Includes design docs - db.view("#{@foo_db_name}/all")['rows'].size.should == 10 + db.view("default/all")['rows'].size.should == 10 end it 'should delete, add, push views and load fixtures for all databases if none are specified' do @@ -39,9 +39,10 @@ CouchRestRails::Fixtures.load('bar') (dbf.documents['rows'].size + dbb.documents['rows'].size).should == 15 + CouchRestRails::Tests.reset_fixtures CouchRestRails::Tests.setup (dbf.documents['rows'].size + dbb.documents['rows'].size).should == 17 # Includes design docs - (dbf.view("#{@foo_db_name}/all")['rows'].size + dbb.view("#{@bar_db_name}/all")['rows'].size).should == 15 + (dbf.view("default/all")['rows'].size + dbb.view("default/all")['rows'].size).should == 15 end end diff --git a/spec/lib/couch_rest_rails/views_spec.rb b/spec/lib/couch_rest_rails/views_spec.rb index 3715cea..702c00d 100644 --- a/spec/lib/couch_rest_rails/views_spec.rb +++ b/spec/lib/couch_rest_rails/views_spec.rb @@ -19,20 +19,20 @@ res = CouchRestRails::Fixtures.load('foo') res = CouchRestRails::Views.push('foo') db = CouchRest.database(@foo_db_url) - db.view("#{@foo_db_name}/all")['rows'].size.should == 10 + db.view("default/all")['rows'].size.should == 10 end it "should replace existing views but issue a warning" do CouchRestRails::Tests.setup('foo') res = CouchRestRails::Views.push('foo') - res.should =~ /overwriting/ + res.should =~ /Overwriting/ end it "should push the views in CouchRestRails.views_path to a design document for all databases if * is passed" do CouchRestRails::Tests.setup dbf = CouchRest.database(@foo_db_url) dbb = CouchRest.database(@bar_db_url) - (dbf.view("#{@foo_db_name}/all")['rows'].size + dbb.view("#{@bar_db_name}/all")['rows'].size).should == 15 + (dbf.view("default/all")['rows'].size + dbb.view("default/all")['rows'].size).should == 15 end end diff --git a/spec/mock/views/bar/views/all/map.js b/spec/mock/views/bar/views/default/all/map.js similarity index 100% rename from spec/mock/views/bar/views/all/map.js rename to spec/mock/views/bar/views/default/all/map.js diff --git a/spec/mock/views/foo/views/all/map.js b/spec/mock/views/foo/views/default/all/map.js similarity index 100% rename from spec/mock/views/foo/views/all/map.js rename to spec/mock/views/foo/views/default/all/map.js diff --git a/spec/mock/views/foo/views/tags/map.js b/spec/mock/views/foo/views/default/tags/map.js similarity index 100% rename from spec/mock/views/foo/views/tags/map.js rename to spec/mock/views/foo/views/default/tags/map.js diff --git a/spec/mock/views/foo/views/tags/reduce.js b/spec/mock/views/foo/views/default/tags/reduce.js similarity index 100% rename from spec/mock/views/foo/views/tags/reduce.js rename to spec/mock/views/foo/views/default/tags/reduce.js