forked from stevenbristol/lovd-by-less
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Gravatar support through avatar gem
- Loading branch information
James Rosen
committed
Mar 26, 2008
1 parent
43a9527
commit 6babefa
Showing
13 changed files
with
130 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters