2009-02-03 04:21:32 +01:00
|
|
|
module CouchRest
|
2010-06-20 22:01:11 +02:00
|
|
|
module Model
|
2009-02-03 04:21:32 +01:00
|
|
|
module Views
|
2010-06-20 22:01:11 +02:00
|
|
|
extend ActiveSupport::Concern
|
2009-02-03 04:21:32 +01:00
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
# Define a CouchDB view. The name of the view will be the concatenation
|
|
|
|
# of <tt>by</tt> and the keys joined by <tt>_and_</tt>
|
|
|
|
#
|
|
|
|
# ==== Example views:
|
|
|
|
#
|
|
|
|
# class Post
|
|
|
|
# # view with default options
|
|
|
|
# # query with Post.by_date
|
|
|
|
# view_by :date, :descending => true
|
|
|
|
#
|
|
|
|
# # view with compound sort-keys
|
|
|
|
# # query with Post.by_user_id_and_date
|
|
|
|
# view_by :user_id, :date
|
|
|
|
#
|
|
|
|
# # view with custom map/reduce functions
|
|
|
|
# # query with Post.by_tags :reduce => true
|
|
|
|
# view_by :tags,
|
|
|
|
# :map =>
|
|
|
|
# "function(doc) {
|
2010-09-17 23:00:55 +02:00
|
|
|
# if (doc['model'] == 'Post' && doc.tags) {
|
2009-02-03 04:21:32 +01:00
|
|
|
# doc.tags.forEach(function(tag){
|
|
|
|
# emit(doc.tag, 1);
|
|
|
|
# });
|
|
|
|
# }
|
|
|
|
# }",
|
|
|
|
# :reduce =>
|
|
|
|
# "function(keys, values, rereduce) {
|
|
|
|
# return sum(values);
|
|
|
|
# }"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# <tt>view_by :date</tt> will create a view defined by this Javascript
|
|
|
|
# function:
|
|
|
|
#
|
|
|
|
# function(doc) {
|
2010-09-17 23:00:55 +02:00
|
|
|
# if (doc['model'] == 'Post' && doc.date) {
|
2009-02-03 04:21:32 +01:00
|
|
|
# emit(doc.date, null);
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# It can be queried by calling <tt>Post.by_date</tt> which accepts all
|
|
|
|
# valid options for CouchRest::Database#view. In addition, calling with
|
|
|
|
# the <tt>:raw => true</tt> option will return the view rows
|
|
|
|
# themselves. By default <tt>Post.by_date</tt> will return the
|
|
|
|
# documents included in the generated view.
|
|
|
|
#
|
2009-03-27 12:27:37 +01:00
|
|
|
# Calling with :database => [instance of CouchRest::Database] will
|
|
|
|
# send the query to a specific database, otherwise it will go to
|
|
|
|
# the model's default database (use_database)
|
|
|
|
#
|
2009-02-03 04:21:32 +01:00
|
|
|
# CouchRest::Database#view options can be applied at view definition
|
|
|
|
# time as defaults, and they will be curried and used at view query
|
|
|
|
# time. Or they can be overridden at query time.
|
|
|
|
#
|
|
|
|
# Custom views can be queried with <tt>:reduce => true</tt> to return
|
|
|
|
# reduce results. The default for custom views is to query with
|
|
|
|
# <tt>:reduce => false</tt>.
|
|
|
|
#
|
|
|
|
# Views are generated (on a per-model basis) lazily on first-access.
|
|
|
|
# This means that if you are deploying changes to a view, the views for
|
|
|
|
# that model won't be available until generation is complete. This can
|
|
|
|
# take some time with large databases. Strategies are in the works.
|
|
|
|
#
|
2009-03-27 12:27:37 +01:00
|
|
|
# To understand the capabilities of this view system more completely,
|
2009-02-03 04:21:32 +01:00
|
|
|
# it is recommended that you read the RSpec file at
|
2009-07-15 08:48:06 +02:00
|
|
|
# <tt>spec/couchrest/more/extended_doc_spec.rb</tt>.
|
2009-02-03 04:21:32 +01:00
|
|
|
|
|
|
|
def view_by(*keys)
|
|
|
|
opts = keys.pop if keys.last.is_a?(Hash)
|
|
|
|
opts ||= {}
|
|
|
|
ducktype = opts.delete(:ducktype)
|
|
|
|
unless ducktype || opts[:map]
|
|
|
|
opts[:guards] ||= []
|
2010-09-17 23:00:55 +02:00
|
|
|
opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')"
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
keys.push opts
|
2010-04-06 19:51:17 +02:00
|
|
|
design_doc.view_by(*keys)
|
|
|
|
req_design_doc_refresh
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
|
2010-04-16 02:51:59 +02:00
|
|
|
# returns stored defaults if there is a view named this in the design doc
|
2009-02-03 04:21:32 +01:00
|
|
|
def has_view?(view)
|
|
|
|
view = view.to_s
|
|
|
|
design_doc && design_doc['views'] && design_doc['views'][view]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Dispatches to any named view.
|
2009-02-17 09:36:11 +01:00
|
|
|
def view(name, query={}, &block)
|
2010-06-21 16:43:33 +02:00
|
|
|
query = query.dup # Modifications made on copy!
|
2009-07-21 23:23:12 +02:00
|
|
|
db = query.delete(:database) || database
|
2010-04-06 19:51:17 +02:00
|
|
|
refresh_design_doc(db)
|
2009-02-03 04:21:32 +01:00
|
|
|
query[:raw] = true if query[:reduce]
|
|
|
|
raw = query.delete(:raw)
|
2009-03-27 12:27:37 +01:00
|
|
|
fetch_view_with_docs(db, name, query, raw, &block)
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
|
2010-06-18 20:07:34 +02:00
|
|
|
# Find the first entry in the view. If the second parameter is a string
|
|
|
|
# it will be used as the key for the request, for example:
|
2010-06-18 19:00:20 +02:00
|
|
|
#
|
2010-06-18 20:07:34 +02:00
|
|
|
# Course.first_from_view('by_teacher', 'Fred')
|
2010-06-18 19:00:20 +02:00
|
|
|
#
|
2010-06-18 20:07:34 +02:00
|
|
|
# More advanced requests can be performed by providing a hash:
|
|
|
|
#
|
|
|
|
# Course.first_from_view('by_teacher', :startkey => 'bbb', :endkey => 'eee')
|
|
|
|
#
|
|
|
|
def first_from_view(name, *args)
|
|
|
|
query = {:limit => 1}
|
|
|
|
case args.first
|
|
|
|
when String, Array
|
|
|
|
query.update(args[1]) unless args[1].nil?
|
|
|
|
query[:key] = args.first
|
|
|
|
when Hash
|
|
|
|
query.update(args.first)
|
|
|
|
end
|
2010-06-18 19:00:20 +02:00
|
|
|
view(name, query).first
|
|
|
|
end
|
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
private
|
|
|
|
|
2009-03-27 12:27:37 +01:00
|
|
|
def fetch_view_with_docs(db, name, opts, raw=false, &block)
|
2009-03-04 01:57:59 +01:00
|
|
|
if raw || (opts.has_key?(:include_docs) && opts[:include_docs] == false)
|
2009-03-27 12:27:37 +01:00
|
|
|
fetch_view(db, name, opts, &block)
|
2009-02-03 04:21:32 +01:00
|
|
|
else
|
2010-06-25 20:02:18 +02:00
|
|
|
if block.nil?
|
|
|
|
collection_proxy_for(design_doc, name, opts.merge({:database => db, :include_docs => true}))
|
|
|
|
else
|
|
|
|
view = fetch_view db, name, opts.merge({:include_docs => true}), &block
|
|
|
|
view['rows'].collect{|r|create_from_database(r['doc'])} if view['rows']
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-27 12:27:37 +01:00
|
|
|
def fetch_view(db, view_name, opts, &block)
|
|
|
|
raise "A view needs a database to operate on (specify :database option, or use_database in the #{self.class} class)" unless db
|
2009-02-03 04:21:32 +01:00
|
|
|
retryable = true
|
|
|
|
begin
|
2009-03-27 12:27:37 +01:00
|
|
|
design_doc.view_on(db, view_name, opts, &block)
|
|
|
|
# the design doc may not have been saved yet on this database
|
2010-05-20 20:49:53 +02:00
|
|
|
rescue RestClient::ResourceNotFound => e
|
2009-02-03 04:21:32 +01:00
|
|
|
if retryable
|
2010-04-06 19:51:17 +02:00
|
|
|
save_design_doc(db)
|
2009-02-03 04:21:32 +01:00
|
|
|
retryable = false
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end # module ClassMethods
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2009-12-04 08:58:07 +01:00
|
|
|
end
|