Skip to content

Commit

Permalink
merged Peter's stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Aug 24, 2009
2 parents feb09f9 + f8e6a81 commit 64a51b7
Show file tree
Hide file tree
Showing 20 changed files with 941 additions and 331 deletions.
29 changes: 20 additions & 9 deletions README.md
Expand Up @@ -103,24 +103,35 @@ Check spec/couchrest/more and spec/fixtures/more for more examples
save_callback :before, :generate_slug_from_title

def generate_slug_from_title
self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new_document?
self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new?
end
end

### Callbacks

`CouchRest::ExtendedDocuments` instances have 2 callbacks already defined for you:
`create_callback`, `save_callback`, `update_callback` and `destroy_callback`
`CouchRest::ExtendedDocuments` instances have 4 callbacks already defined for you:
`:validate`, `:create`, `:save`, `:update` and `:destroy`

In your document inherits from `CouchRest::ExtendedDocument`, define your callback as follows:
`CouchRest::CastedModel` instances have 1 callback already defined for you:
`:validate`

Define your callback as follows:

save_callback :before, :generate_slug_from_name
set_callback :save, :before, :generate_slug_from_name

CouchRest uses a mixin you can find in lib/mixins/callbacks which is extracted from Rails 3, here are some simple usage examples:

save_callback :before, :before_method
save_callback :after, :after_method, :if => :condition
save_callback :around {|r| stuff; yield; stuff }
set_callback :save, :before, :before_method
set_callback :save, :after, :after_method, :if => :condition
set_callback :save, :around {|r| stuff; yield; stuff }

Or the aliased short version:

before_save :before_method, :another_method
after_save :after_method, :another_method, :if => :condition
around_save {|r| stuff; yield; stuff }

To halt the callback, simply return a :halt symbol in your callback method.

Check the mixin or the ExtendedDocument class to see how to implement your own callbacks.

Expand Down Expand Up @@ -162,4 +173,4 @@ Low level usage:

CouchRest is compatible with rails and can even be used a Rails plugin.
However, you might be interested in the CouchRest companion rails project:
[http://github.com/hpoydar/couchrest-rails](http://github.com/hpoydar/couchrest-rails)
[http://github.com/hpoydar/couchrest-rails](http://github.com/hpoydar/couchrest-rails)
2 changes: 1 addition & 1 deletion couchrest.gemspec
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = %q{couchrest}
s.version = "0.33"
s.version = "0.34"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["J. Chris Anderson", "Matt Aimonetti"]
Expand Down
1 change: 1 addition & 0 deletions lib/couchrest.rb
Expand Up @@ -48,6 +48,7 @@ module CouchRest
require File.join(File.dirname(__FILE__), 'couchrest', 'core', 'rest_api')
require File.join(File.dirname(__FILE__), 'couchrest', 'core', 'http_abstraction')
require File.join(File.dirname(__FILE__), 'couchrest', 'mixins')
require File.join(File.dirname(__FILE__), 'couchrest', 'support', 'rails') if defined?(Rails)

# we extend CouchRest with the RestAPI module which gives us acess to
# the get, post, put, delete and copy
Expand Down
5 changes: 3 additions & 2 deletions lib/couchrest/core/document.rb
Expand Up @@ -23,9 +23,10 @@ def rev
end

# returns true if the document has never been saved
def new_document?
def new?
!rev
end
alias :new_document? :new?

# Saves the document to the db using create or update. Also runs the :save
# callbacks. Sets the <tt>_id</tt> and <tt>_rev</tt> fields based on
Expand Down Expand Up @@ -63,7 +64,7 @@ def copy(dest)

# Returns the CouchDB uri for the document
def uri(append_rev = false)
return nil if new_document?
return nil if new?
couch_uri = "http://#{database.root}/#{CGI.escape(id)}"
if append_rev == true
couch_uri << "?rev=#{rev}"
Expand Down

0 comments on commit 64a51b7

Please sign in to comment.