# Gravatarify
Removes any hassles building those pesky gravatar urls, it's not there arent any alternatives [out](http://github.com/mdeering/gravitar_image_tag),
[there](http://github.com/chrislloyd/gravtastic), but none seem to support stuff like `Proc`s for the default picture url, or
the multiple host names supported by gravatar.com (great when displaying lots of avatars).
Best of it? It works with Rails, probably Merb and even plain old Ruby :)
## Install
Just install the gem (ensure you have gemcutter in your sources!)
sudo gem install gravatarify
Ready to go! Using Rails? Either add (to `config/environment.rb`):
config.gem 'gravatarify', :source => 'http://gemcutter.org'
or install as Rails plugin:
./script/plugin install git://github.com/lwe/gravatarify.git
If the gem is available, just require it (for your simple ruby apps):
require 'rubygems'
require 'gravatarify'
## Using the view helpers (Rails only!)
Probably one of the easiest ways to add support for gravatar images is with the included view helpers:
<%= gravatar_tag @user %> # assumes @user has email or mail field!
This builds a neat ``-tag, if you need to pass in stuff like the size etc. just:
<%= gravatar_tag @user, :size => 25, :rating => :x, :class => "gravatar" %>
This will display an "X" rated avatar which is 25x25 pixel in size and the image tag will have the class `"gravatar"`.
If more control is required, or just the URL, well then go ahead and use `gravatar_url` instead:
<%= image_tag gravatar_url(@user.author_email, :size => 16), :size => "16x16",
:alt => @user.name, :class => "avatar avatar-16"}/
Using rails `image_tag` to create an `
`-tag with `gravatar_url`. It's important to know that
also an object can be passed to `gravatar_url`, if it responds to either `email` or `mail`. If not (like
in the example above), the email address must be passed in.
## Using the model helpers
Another way (especially cool) for models is to do:
class User < ActiveRecord::Base
gravatarify
end
Thats it! Well, at least if the `User` model responds to `email` or `mail`. Then in the views all left to do is:
<%= image_tag @user.gravatar_url %>
Neat, isn't it? Of course passing options works just like with the view helpers:
<%= image_tag @user.gravatar_url(:size => 16, :rating => :r) %>
Defaults can even be passed to the `gravatarify` call, so no need to repeat them on every `gravatar_url` call.
gravatarify :employee_mail, :size => 16, :rating => :r
All gravatars will now come from the `employee_mail` field, not the default `email` or `mail` field and be in 16x16px in size
and have a rating of 'r'. Of course these can be overriden in calls to `gravatar_url` like before. Pretty cool is also the
fact that an object can be passed directly to `gravatar_tag` if it responds to `gravatar_url`, like:
# model:
class User < ActiveRecord::Base
gravatarify :size => 16, :secure => true
end
# view:
<%= gravatar_tag @user %> # ->
The `gravatar_tag` looks if the object responds to `gravatar_url` and if so, just passes the options to it,
it works also with plain old ruby objects, of course :)
### PORO - plain old ruby objects (yeah, POJO sounds smoother :D)
Not using Rails, ActiveRecord or DataMapper? It's as easy as including `Gravatarify::ObjectSupport` to your
class:
require 'gravatarify'
class PoroUser
include Gravatarify::ObjectSupport
gravatarify
end
Tadaaa! Works exactly like the model helpers, so it's now possible to call `gravatar_url` on instances
of `PoroUser`.
## Back to the roots?
No need for sophisticated stuff like view helpers and ActiveRecord integration, want to go back to the roots?
Then feel free to use `Gravatarify::Base#build_gravatar_url` directly.
For example, want to use `build_gravatar_url` in a Sinatra app?
helpers Gravatarify::Base
Yeah, that should work :). See {Gravatarify::Base#build_gravatar_url} for more informations and usage examples.
## Need more control?
Option | Type | Description | Default |
---|---|---|---|
:default | String, Proc | Fully qualified URL to an image, which is used if gravatar.com has no image for the supplied email. Procs can be used to return e.g. an image based on the request size (see Advanced stuff). Furthermore gravatar.com provides several "special values" which generate icons, these are "wavatar", "monsterid" and "identicon", finally if set to 404 gravatar.com returns the HTTP 404 Not Found error. If nothing is specified gravatar.com returns it's gravatar icon. | - |
:rating | String, Symbol | Each avatar at gravatar.com has a rating associated (which is based on MPAAs rating system), valid values are: g - general audiences, pg - parental guidance suggested, r - restricted and x - x-rated :). Gravatar.com returns g-rated avatars, unless anything else is specified. |
- |
:size | Integer | Avatars are square, so :size defines the length of the sides in pixel, if nothing is specified gravatar.com returns 80x80px images. | - |
:secure | Boolean, Proc | If set to true gravatars secure host (https://secure.gravatar.com/) is used to serve the avatars from. Can be a Proc to inflect wheter or not to use the secure host based on request parameters. | false |
:filetype | String, Symbol | Change image type, gravatar.com supports :gif, :png and :jpg. | :jpg |