Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ jobs:
- ruby: "jruby"
gemfile: "active_support_8_0"
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up CouchDB
uses: cobot/couchdb-action@v4
uses: cobot/couchdb-action@v5
with:
couchdb version: "2.3.1"
couchdb-version: "2.3.1"
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run tests
run: bundle exec rake spec_ci
env:
DATABASE: http://admin:admin@localhost:5984/couch_potato_test
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changes

# 1.19.0 / rspec-matchers 4.2.0

- add `single_design_document` config option
- remove support for lists and lib

# 1.18.0

- add testing Rails 7.2/8 on CI
Expand Down
60 changes: 8 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ Another switch allows you to store each CouchDB view in its own design document.
CouchPotato::Config.split_design_documents_per_view = true
```

With the following switch, couch potato only creates a single design document for all views:

```ruby
CouchPotato::Config.single_design_document = true
```

If you are using more than one database from your app, you can create aliases:

```ruby
Expand All @@ -95,6 +101,7 @@ Create a `config/couchdb.yml`:
default: &default
split_design_documents_per_view: true # optional, default is false
digest_view_names: true # optional, default is false
single_design_document: true # optional, default is false
default_language: :erlang # optional, default is javascript
database_host: "http://127.0.0.1:5984"

Expand Down Expand Up @@ -299,7 +306,7 @@ user.valid? # => false
user.errors[:name] # => ['can't be blank']
```

#### Finding stuff / views / lists
#### Finding stuff / views

In order to find data in your CouchDB you have to create a [view](http://books.couchdb.org/relax/design-documents/views) first. Couch Potato offers you to create and manage those views for you. All you have to do is declare them in your classes:

Expand Down Expand Up @@ -403,14 +410,6 @@ class User
end
```

commonJS modules can also be used in custom views:

```ruby
class User
view :all, :map => "function(doc) { emit(null, require("views/lib/test").test)}", :lib => {:test => "exports.test = 'test'"}, :include_docs => true, :type => :custom
end
```

If you don't want the results to be converted into models the raw view is your friend:

```ruby
Expand Down Expand Up @@ -450,49 +449,6 @@ You can pass in your own view specifications by passing in `:type => MyViewSpecC

If turned on, Couch Potato will append an MD5 digest of the map function to each view name. This makes sure (together with split_design_documents_per_view) that no views/design documents are ever updated. Instead, new ones are created. Since reindexing can take a long time once your database is larger, you want to avoid blocking your app while CouchDB is busy. Instead, you create a new view, warm it up, and only then start using it.

##### Lists

CouchPotato also supports [CouchDB lists](http://books.couchdb.org/relax/design-documents/lists). With lists you can process the result of a view query with another JavaScript function. This can be useful for example if you want to filter your results, or add some data to each document.

Defining a list works similarly to views:

```ruby
class User
include CouchPotato::Persistence

property :first_name
view :with_full_name, key: first_namne, list: :add_last_name
view :all, key: :first_name

list :add_last_name, <<-JS
function(head, req) {
var row;
send('{"rows": [');
while(row = getRow()) {
row.doc.name = row.doc.first_name + ' doe';
send(JSON.stringify(row));
};
send(']}');
}
JS
end

CouchPotato.database.save User.new(first_name: 'joe')
CouchPotato.database.view(User.with_full_name).first.name # => 'joe doe'
```

You can also pass in the list at query time:

```ruby
CouchPotato.database.view(User.all(list: :add_last_name))
```

And you can pass parameters to the list:

```ruby
CouchPotato.database.view(User.all(list: :add_last_name, list_params: {filter: '*'}))
```

#### Associations

Not supported. Not sure if they ever will be. You can implement those yourself using views and custom methods on your models.
Expand Down
10 changes: 9 additions & 1 deletion lib/couch_potato.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
CouchRest.decode_json_objects = true

module CouchPotato
Config = Struct.new(:database_host, :database_name, :digest_view_names,
Config = Struct.new(:database_host, :database_name, :digest_view_names, :single_design_document,
:split_design_documents_per_view, :default_language, :additional_databases).new
Config.split_design_documents_per_view = false
Config.single_design_document = false
Config.digest_view_names = false
Config.default_language = :javascript
Config.database_host = 'http://127.0.0.1:5984'
Expand All @@ -29,6 +30,7 @@ def self.configure(config)
Config.database_host = config['database_host'] if config['database_host']
Config.additional_databases = config['additional_databases'].stringify_keys if config['additional_databases']
Config.split_design_documents_per_view = config['split_design_documents_per_view'] if config['split_design_documents_per_view']
Config.single_design_document = config['single_design_document'] if config['single_design_document']
Config.digest_view_names = config['digest_view_names'] if config['digest_view_names']
Config.default_language = config['default_language'] if config['default_language']
end
Expand All @@ -40,6 +42,12 @@ def self.models
@models
end

# returns all the classes that include the CouchPotato::View::CustomViews module
def self.views
@views ||= []
@views
end

# Returns a database instance which you can then use to create objects and query views. You have to set the CouchPotato::Config.database_name before this works.
def self.database
Thread.current[:__couch_potato_database] ||= Database.new(couchrest_database)
Expand Down
2 changes: 0 additions & 2 deletions lib/couch_potato/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,6 @@ def raw_view(spec)
map: spec.map_function,
reduce: spec.reduce_function
} },
({ spec.list_name => spec.list_function } unless spec.list_name.nil?),
spec.lib,
spec.language
).query_view!(spec.view_parameters)
end
Expand Down
15 changes: 9 additions & 6 deletions lib/couch_potato/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
require File.dirname(__FILE__) + '/persistence/revisions'
require File.dirname(__FILE__) + '/forbidden_attributes_protection'
require File.dirname(__FILE__) + '/view/custom_views'
require File.dirname(__FILE__) + '/view/lists'
require File.dirname(__FILE__) + '/view/view_query'


module CouchPotato
module Persistence
module TrackModels
def inherited(child)
super
CouchPotato.models << child
end
end

def self.included(base) #:nodoc:
base.send :include, Properties, Callbacks, Json, CouchPotato::View::CustomViews,
CouchPotato::View::Lists
base.send :include, Properties, Callbacks, Json, CouchPotato::View::CustomViews
base.send :include, DirtyAttributes, GhostAttributes, Attachments
base.send :include, MagicTimestamps, ActiveModelCompliance,
ForbiddenAttributesProtection, Revisions
Expand All @@ -31,9 +35,8 @@ def self.included(base) #:nodoc:
alias_method :id, :_id
alias_method :id=, :_id=

def self.inherited(child)
super
CouchPotato.models << child
class << self
prepend TrackModels
end
end

Expand Down
5 changes: 0 additions & 5 deletions lib/couch_potato/rspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'couch_potato/rspec/matchers/map_to_matcher'
require 'couch_potato/rspec/matchers/reduce_to_matcher'
require 'couch_potato/rspec/matchers/map_reduce_to_matcher'
require 'couch_potato/rspec/matchers/list_as_matcher'

module RSpec
module Matchers
Expand All @@ -19,10 +18,6 @@ def rereduce(keys, values)
CouchPotato::RSpec::ReduceToProxy.new(keys, values, true)
end

def list(results)
CouchPotato::RSpec::ListAsProxy.new(results)
end

def map_reduce(*docs)
CouchPotato::RSpec::MapReduceToProxy.new(docs)
end
Expand Down
54 changes: 0 additions & 54 deletions lib/couch_potato/rspec/matchers/list_as_matcher.rb

This file was deleted.

3 changes: 1 addition & 2 deletions lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ def matches?(view_spec)
var options = #{@options.to_json};
var map = #{view_spec.map_function};
var reduce = #{view_spec.reduce_function};
var lib = #{view_spec.respond_to?(:lib) && view_spec.lib.to_json};
var collate = (function() { var module = {exports: {}}; var exports = module.exports; eval(#{File.read(File.expand_path(File.dirname(__FILE__) + '/../../../../vendor/pouchdb-collate/pouchdb-collate.js')).to_json}); return module.exports.collate;})();

// Map the input docs
var require = function(modulePath) {
var module = {exports: {}};
var exports = module.exports;
var pathArray = modulePath.split("/").slice(2);
var result = lib;
var result = {};
for (var i in pathArray) {
result = result[pathArray[i]];
}
Expand Down
3 changes: 1 addition & 2 deletions lib/couch_potato/rspec/matchers/map_to_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ def matches?(view_spec)
(function() {
var doc = #{@input_ruby.to_json};
var map = #{view_spec.map_function};
var lib = #{view_spec.respond_to?(:lib) && view_spec.lib.to_json};
var result = [];
var require = function(modulePath) {
var module = {exports: {}};
var exports = module.exports;
var pathArray = modulePath.split("/").slice(2);
var result = lib;
var result = {};
for (var i in pathArray) {
result = result[pathArray[i]];
}
Expand Down
4 changes: 2 additions & 2 deletions lib/couch_potato/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module CouchPotato
VERSION = '1.18.0'.freeze
RSPEC_VERSION = '4.1.0'.freeze
VERSION = '1.19.0'.freeze
RSPEC_VERSION = '4.2.0'.freeze
end
Loading