Skip to content

Commit

Permalink
Updated view code
Browse files Browse the repository at this point in the history
  • Loading branch information
henrypoydar committed Aug 30, 2009
1 parent a247177 commit 5b84f47
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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:

Expand Down Expand Up @@ -140,7 +140,7 @@ The Lucene design documents are stored alongside the views:

db/couch/<database_name>/lucene
|-- <design_document_name>
|-- <lucene_search>
|-- <lucene_search>

You can also customize this path:

Expand Down
4 changes: 2 additions & 2 deletions lib/couch_rest_rails/database.rb
Expand Up @@ -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|

Expand Down Expand Up @@ -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|

Expand Down
4 changes: 2 additions & 2 deletions lib/couch_rest_rails/fixtures.rb
Expand Up @@ -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|

Expand Down Expand Up @@ -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|

Expand Down
10 changes: 5 additions & 5 deletions lib/couch_rest_rails/tests.rb
Expand Up @@ -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
Expand Down
75 changes: 42 additions & 33 deletions lib/couch_rest_rails/views.rb
Expand Up @@ -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"
Expand All @@ -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

Expand Down
5 changes: 3 additions & 2 deletions spec/lib/couch_rest_rails/tests_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/couch_rest_rails/views_spec.rb
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5b84f47

Please sign in to comment.