CouchRestAdapter
This is a realy early version.
This project rocks and uses MIT-LICENSE.
Requirements
-
Rails > 4
-
Couchrest
-
Requires config/database.yml file
Instalation
Right now there will be a conflict if you use it as it is mixed with ActoveRecord, so you should create your reails project without AR, as in rails new my_app -O
In Gemfile
gem 'couch_rest_adapter'
Sample config/couchdb.yml
All this are required attributes.
defaults: &defaults
host: localhost
port: 5984
protocol: http
design_doc: my_app
# optional
# username: username
# password: password
development:
<<: *defaults
name: db
test:
<<: *defaults
name: db_test
Versions > 0.8.0 support https protocol, s
CouchViews
After installing the gem you should run “‘rake db:push:config“` this will setup a design document called as the database.yml “`design_doc“` value with the next structure:
{
"_id": "_design/my_app",
"language": "coffeescript",
"views": {
"all": {
"map": "(d) ->\n split_id = d._id.split('/')\n t = split_id[0]\n emit t, d\n",
"reduce": "# This is only for demosntration one can use the built in count\n(keys, values, rereduce)->\n if rereduce\n sum(values)\n else\n values.length\n"
},
"by_attribute": {
"map": "(doc) ->\n type = doc._id.split('/')[0]\n for a of doc\n emit([type, a, doc[a]], doc._id)\n"
},
"by_type": {
"map": "(d)->\n emit d.type.toLowerCase(), d._id if d.type\n"
},
}
}
You can add more views as needed into “‘db/designs/“` folder.
This is how the designs are delcared:
db
designs
my_map_name.map.coffee
my_filter_name.filter.coffee
my_reduce_name.reduce.coffee
Then if you run “‘rake db:push:design“` the “`_design/my_app“ document will be updated. The views need to be written on coffeescript code.
One thing: this will allow you to add tests to your couch views ;-)
Model Declaration
class User < CouchRestAdpater::Base
use_default_database
end
TODO
-
Allow setting new attributes with “‘model.attribute = value“`
-
More test coverage
-
Add tasks for pushing design documents