Resource Count

Added by Soleone at October 25, 2009 16:12 Star_small_grayStar_small_grayStar_small_grayStar_small_grayStar_small_gray
GitHub stats:
Magnifier watcher(s)
Arrow_branch fork(s)
Wrench

About

Extend your RESTful controllers by simply returning the number of models in the database for any call to a URL like modelname/count.

Details

Extend your RESTful controllers by simply returning the number of models in the database for any call to a URL like modelname/count.

Examples:
/users/count
returns User.count in plain text

/articles/1/comments/count.xml
returns Comment.count as XML, e.g.
<comments type="integer">5</comments>
Note: this does not return Articles.find(1).comments.count like it maybe should!

Usage

Simply require in environment.rb with:
config.middleware.use "ResourceCount"

Requirements when not using Rails:

  1. Use Rails conventions (controller name is pluralized name of model)
  2. Model.count should be available, but size or length also works
  3. These String methods from Rails need to be available: singularize, classify and constantize
blog comments powered by Disqus