Skip to content

Commit

Permalink
added Gravatar support through avatar gem
Browse files Browse the repository at this point in the history
  • Loading branch information
James Rosen committed Mar 26, 2008
1 parent 43a9527 commit 6babefa
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 40 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Install the Required Gems:
- acts_as_ferret
- ferret
- win32console (windows only)
- avatar

TIP:
Installing ferret on Windows
Expand Down
55 changes: 33 additions & 22 deletions app/helpers/photos_helper.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
module PhotosHelper



def image photo, size = :square, img_opts = {}
return image_tag(image_path( photo, size), :class => size) if photo.image.blank?
img_tag = image_tag(image_path( photo, size), {:title=>photo.caption, :alt=>photo.caption, :class=>size}.merge(img_opts))
img_tag
def self.included(base)
#this gets auto-included, but only later, so do it now:
base.send :include, ActionView::Helpers::AssetTagHelper

#replace image_path with a version that understands stored photos:
unless base.method_defined?(:image_path_without_photo)
base.send :alias_method, :image_path_without_photo, :image_path
end
base.send :include, PhotosHelper::InstanceMethods
end

module InstanceMethods
def image photo, size = :square, img_opts = {}
return image_tag(image_path( photo, size), :class => size) if photo.image.blank?
img_tag = image_tag(image_path( photo, size), {:title=>photo.caption, :alt=>photo.caption, :class=>size}.merge(img_opts))
img_tag
end

def photo_path photo, size
return "/images/missing_#{size}.png" if photo.image.blank?
if size
path = url_for_image_column(photo, :image, size) rescue path = "/images/missing_#{size}.png"
# QUESTION: Is there a way to do a file column return on a fixture and return a
# fake path when the actual path DNE? Returns nil if file missing
path = "/images/missing_#{size}.png" if path.nil?
else
path = url_for_file_column(photo, :image) rescue path = "/images/missing_.png"
# QUESTION: Is there a way to do a file column return on a fixture and return a
# fake path when the actual path DNE? Returns nil if file missing
path = "/images/missing_.png" if path.nil?
end
return path
end

def image_path photo = nil, size = :square
return "/images/missing_#{size}.png" if photo.image.blank?
if size
path = url_for_image_column(photo, :image, size) rescue path = "/images/missing_#{size}.png"
# QUESTION: Is there a way to do a file column return on a fixture and return a
# fake path when the actual path DNE? Returns nil if file missing
path = "/images/missing_#{size}.png" if path.nil?
else
path = url_for_file_column(photo, :image) rescue path = "/images/missing_.png"
# QUESTION: Is there a way to do a file column return on a fixture and return a
# fake path when the actual path DNE? Returns nil if file missing
path = "/images/missing_.png" if path.nil?
def image_path(source_or_photo, size = :square)
source_or_photo.respond_to?(:image) ? photo_path(source_or_photo, size) : image_path_without_photo(source_or_photo)
end
return path
end


end
end
21 changes: 12 additions & 9 deletions app/helpers/profiles_helper.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
require 'avatar/view/action_view_support'

module ProfilesHelper
include Avatar::View::ActionViewSupport

def icon profile, size = :small, img_opts = {}
return link_to(image_tag(icon_path( profile, size), {:title=>profile.full_name, :alt=>profile.full_name, :class=>size}.merge(img_opts)), profile_path(profile)) if profile.icon.blank?
link_to(image_tag(icon_path( profile, size), {:title=>profile.full_name, :alt=>profile.full_name, :class=>size}.merge(img_opts)), profile_path(profile)) rescue ''
end



def icon_path profile = nil, size = :small
return "/images/avatar_default_#{size}.png" if profile.icon.blank?
url_for_image_column(profile, :icon, size) rescue "/images/avatar_default_#{size}.png"
return "" if profile.nil?
img_opts = img_opts.merge(:title => profile.full_name, :alt => profile.full_name, :class => size).merge(img_opts)
link_to(avatar_tag(profile, {:size => size, :file_column_version => size, :default => default_image_url(size)}, img_opts))
end

def location_link profile = @p
return profile.location if profile.location == Profile::NOWHERE
link_to h(profile.location), search_profiles_path.add_param('search[location]' => profile.location)
end

private
def default_image_url(size)
req = controller.request
"#{req.protocol}#{req.host_with_port}#{image_path("/images/avatar_default_#{size}.png")}"
end
end
5 changes: 2 additions & 3 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ def after_create
feed_item = FeedItem.create(:item => self)
([profile] + profile.friends + profile.followers).each{ |p| p.feed_items << feed_item }
end


file_column :image, :root_path => File.join(RAILS_ROOT, "public/system"), :web_root => 'system/', :magick => {

file_column :image, :magick => {
:versions => {
:square => {:crop => "1:1", :size => "50x50", :name => "square"},
:small => "175x250>"
Expand Down
8 changes: 4 additions & 4 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ class Profile < ActiveRecord::Base

acts_as_ferret :fields => [ :location, :f, :about_me ], :remote=>true

file_column :icon, :root_path => File.join(RAILS_ROOT, "public/system"), :web_root => 'system/', :magick => {
file_column :icon, :magick => {
:versions => {
:big => {:crop => "1:1", :size => "150x150", :name => "big"},
:medium => {:crop => "1:1", :size => "100x100", :name => "medium"},
:small => {:crop => "1:1", :size => "50x50", :name => "small"}
:big => {:crop => "1:1", :size => "150x150", :name => "big"},
:medium => {:crop => "1:1", :size => "100x100", :name => "medium"},
:small => {:crop => "1:1", :size => "50x50", :name => "small"}
}
}

Expand Down
26 changes: 26 additions & 0 deletions config/initializers/avatar_sources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'avatar'
require 'avatar/source/file_column_source'
require 'avatar/source/source_chain'
require 'avatar/source/string_substitution_source'
require 'sized_gravatar_source'

# order:
# 1. FileColumn(Profile.icon)
# 2. Gravatar(nil_source, Profile.email)
#
# Gravatar accepts a default if no gravatar exists for the
# email address passed. Unfortunately, it needs to be
# a full URL (not just a path relative to the request).
# Thus, this won't work:
# gravatar.default_source = Avatar::Source::StringSubstitutionSource.new('/images/avatar_default_#{size}.png')
# Instead, we have to pass in a default in the avatar_url_for
# call in app/helpers/profiles_helper
#
# Additionally, Gravatar does not understand :small, :medium, and :big,
# so we must translate using SizedGravatarSource

chain = Avatar::Source::SourceChain.new
chain << Avatar::Source::FileColumnSource.new(:icon)
chain << SizedGravatarSource.new(nil, :email)

Avatar::source = chain
2 changes: 2 additions & 0 deletions config/initializers/file_column_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FileColumn::ClassMethods::DEFAULT_OPTIONS[:root_path] = File.join(RAILS_ROOT, "public", 'system')
FileColumn::ClassMethods::DEFAULT_OPTIONS[:web_root] = 'system/'
16 changes: 16 additions & 0 deletions lib/sized_gravatar_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'avatar/source/gravatar_source'

class SizedGravatarSource < Avatar::Source::GravatarSource

alias_method :parse_options_without_size, :parse_options

def self.sizes
{ :small => 50, :medium => 100, :large => 150, :big => 150 }
end

def parse_options(profile, options)
options[:size] = self.class.sizes[options[:size]] if self.class.sizes.has_key?(options[:size])
parse_options_without_size(profile, options)
end

end
1 change: 1 addition & 0 deletions lib/tasks/getting_started.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace :gems do
imagick
acts_as_ferret
ferret
avatar
]
gems << 'win32console' if windoz
sudo = windoz ? '' : 'sudo '
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
#

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

user:
user: user
first_name: De
last_name: Veloper
email: [email protected]
is_active: true
icon: Photo_2.jpg
icon: user.png

user2:
user: user2
Expand Down
27 changes: 27 additions & 0 deletions test/functional/profiles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ class ProfilesControllerTest < ActionController::TestCase
should_render_template :edit
should_render_a_form
should_not_set_the_flash
end

context 'rendering an avatar' do

should 'use the user\'s icon if it exists' do
p = profiles(:user)
p.icon = File.new(File.join(RAILS_ROOT, ['test', 'public','images','user.png']))
p.save!
#raise (p.send :icon_state).inspect
assert_not_nil p.icon
get :show, {:id => p.id, :public_view => true}, {:user => p.id}
assert_tag :img, :attributes => { :src => /\/system\/profile\/icon\/\d*\/big\/user.png/ }
end

should 'use gravatar otherwise' do
p = profiles(:user2)
assert_nil p.icon
get :show, {:id => p.id}, {:user => p.id, :public_view => true}
assert_tag :img, :attributes => {:src => /www\.gravatar\.com/}
end

should 'send the app\'s internal default as the default to gravatar' do
p = profiles(:user2)
assert_nil p.icon
get :show, {:id => p.id}, {:user => p.id, :public_view => true}
assert_tag :img, :attributes => {:src => /www\.gravatar\.com.*default=http:\/\/test.host\/images\/avatar_default_.*\.png/}
end
end


Expand Down
Binary file added test/public/images/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
require 'ostruct'
require 'mocha'

# for testing uploaded files
# place any "already uploaded" files in a subdirectory within /test/ instead of overwriting production files.
FileColumn::ClassMethods::DEFAULT_OPTIONS[:root_path] = File.join(RAILS_ROOT, 'test', "public", 'system')

class Test::Unit::TestCase
# Transactional fixtures accelerate your tests by wrapping each test method
# in a transaction that's rolled back on completion. This ensures that the
Expand Down Expand Up @@ -117,6 +121,5 @@ def _test_actions(controller, options={})

# Teardown and setup - for quick recycling of env. within a single test
def recycle; teardown; setup; end


end

0 comments on commit 6babefa

Please sign in to comment.