Skip to content
This repository has been archived by the owner on Nov 17, 2018. It is now read-only.

Allow options to be set with a proc/lambda #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
gem 'rspec'
gem 'rr'
source 'https://rubygems.org'

gemspec
15 changes: 6 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
PATH
remote: ~/src/rocco
remote: .
specs:
rocco (0.5)
mustache
rdiscount
gravtastic (3.2.6)

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.1.2)
mustache (0.11.2)
rdiscount (1.6.5)
rr (1.0.0)
rspec (2.0.0)
rspec-core (= 2.0.0)
Expand All @@ -26,6 +23,6 @@ PLATFORMS
ruby

DEPENDENCIES
rocco!
rr
rspec
gravtastic!
rr (~> 1.0.0)
rspec (~> 2.0.0)
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@ Gravatar gives you some options. You can use them like this:

<%= image_tag @user.gravatar_url(:rating => 'R', :secure => true) %>

That will show R rated Gravatars over a secure connection. If you find yourself using the same options over and over again, you can set the Gravatar defaults. In your model, just change the `is_gravtastic` line to something like this:
That will show R rated Gravatars over a secure connection. If you find yourself using the same options over and over again, you can set the Gravatar defaults. In your model, just change the `gravtastic` line to something like this:

gravtastic :secure => true,
:filetype => :gif,
:size => 120

Now all your Gravatars will come from a secure connection, be a GIF and be 120x120px.

If your Gravatar defaults depend on your instance, you can define them with a lambda:

class User < ActiveRecord::Base
include Gravtastic
# default avatar is the first letter of the user's first name
gravtastic :default => lambda { |u| "https://assets.example.com/#{u.first_name[0]}.png" }

Gravatar needs an email address to find the person's avatar. By default, Gravtastic calls the `#email` method to find this. You can customise this.

gravtastic :author_email
Expand Down Expand Up @@ -110,7 +117,7 @@ Gravatar is really just simple Ruby. There is no special magic which ties it to
require 'gravtastic'
class MyClass
include Gravtastic
is_gravtastic
gravtastic
end

For instance, with the excellent [MongoMapper](http://github.com/jnunemaker/mongomapper) you can use do this:
Expand All @@ -119,14 +126,14 @@ For instance, with the excellent [MongoMapper](http://github.com/jnunemaker/mong
include MongoMapper::Document
include Gravtastic

is_gravtastic
gravtastic

key :email
end

And wallah! It's exactly the same as with ActiveRecord! Now all instances of the `Person` class will have `#gravatar_url` methods.

_Note: the `#gravatar_url` methods don't get included until you specify the class `is_gravtastic!`_
_Note: the `#gravatar_url` methods don't get included until you call the `gravtastic` method._


## Javascript
Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Gem::PackageTask.new(@spec) do |t|
end
CLEAN.add 'pkg'

require 'spec/rake/spectask'
RSpec::Core::RakeTask.new do |t|
t.rspec_opts = ['--color', '--require ./spec/helper']
end
3 changes: 3 additions & 0 deletions gravtastic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ require './lib/gravtastic/version'

s.has_rdoc = false

s.add_development_dependency 'rspec', '~> 2.0.0'
s.add_development_dependency 'rr', '~> 1.0.0'

s.require_path = 'lib'
s.files = %w(README.md Rakefile Gemfile) + Dir['{lib,spec,vendor}/**/*']
end
2 changes: 1 addition & 1 deletion lib/gravtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def process_options(options)
processed_options[key] = 'y'
end
else
processed_options[key] = val
processed_options[key] = val.is_a?(Proc) ? val.call(self) : val
end
end
processed_options
Expand Down
8 changes: 8 additions & 0 deletions spec/gravtastic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@
@user.gravatar_url.should == 'https://secure.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.png?r=R18&s=20'
end

it "makes a URL with the default option" do
@user.gravatar_url(:default => 'default.jpg').should == 'https://secure.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.png?d=default.jpg&r=PG'
end

it "makes a URL when the default option is a lambda" do
@user.gravatar_url(:default => lambda {|u| "#{u.email}.jpg"}).should == 'https://secure.gravatar.com/avatar/[email protected]&r=PG'
end

end

end