krasivotokak-avatar 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +63 -0
- data/License.txt +20 -0
- data/README.rdoc +71 -0
- data/init.rb +5 -0
- data/lib/avatar.rb +38 -0
- data/lib/avatar/object_support.rb +8 -0
- data/lib/avatar/source.rb +4 -0
- data/lib/avatar/source/abstract_source.rb +15 -0
- data/lib/avatar/source/file_column_source.rb +39 -0
- data/lib/avatar/source/gravatar_source.rb +118 -0
- data/lib/avatar/source/nil_source.rb +17 -0
- data/lib/avatar/source/paperclip_source.rb +52 -0
- data/lib/avatar/source/source_chain.rb +52 -0
- data/lib/avatar/source/static_url_source.rb +25 -0
- data/lib/avatar/source/wrapper.rb +6 -0
- data/lib/avatar/source/wrapper/abstract_source_wrapper.rb +33 -0
- data/lib/avatar/source/wrapper/rails_asset_source_wrapper.rb +38 -0
- data/lib/avatar/source/wrapper/string_substitution_source_wrapper.rb +55 -0
- data/lib/avatar/version.rb +9 -0
- data/lib/avatar/view.rb +4 -0
- data/lib/avatar/view/abstract_view_support.rb +15 -0
- data/lib/avatar/view/action_view_support.rb +23 -0
- data/rails/init.rb +1 -0
- metadata +84 -0
data/History.txt
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
== 0.0.6 2008-06-09
|
2
|
+
* 1 major fix:
|
3
|
+
* removed all task files (./task/*.rake) for Rails plugin compatability
|
4
|
+
|
5
|
+
* 1 minor enhancement
|
6
|
+
* removed redundant Hoe dependencies, since the gem is only hosted on GitHub
|
7
|
+
|
8
|
+
== 0.0.5 2008-04-01
|
9
|
+
* 4 major enhancements:
|
10
|
+
* added PaperclipSource for use with the Paperclip plugin
|
11
|
+
* added AbstractSourceWrapper
|
12
|
+
* changed StringSubstitutionSource to a SourceWrapper
|
13
|
+
* changed RailsAssetSource to a SourceWrapper
|
14
|
+
|
15
|
+
* 1 minor change:
|
16
|
+
* moved StringSubstitution out of separate module and into new StringSubstitutionSourceWrapper
|
17
|
+
|
18
|
+
== 0.0.4 2008-03-28
|
19
|
+
* 2 major fixes:
|
20
|
+
* GravatarSource downcases email to comply with Gravatar standards
|
21
|
+
* GravatarSource raises exception if passed a path instead of a URL for default
|
22
|
+
|
23
|
+
* 1 major enhancement:
|
24
|
+
* added RailsAssetSource
|
25
|
+
|
26
|
+
* 3 minor enhancements:
|
27
|
+
* added default_options to StringSubstitutionSource
|
28
|
+
* broke StringSubstitution out into a separate module
|
29
|
+
* added :any to GravatarSource::allowed_ratings
|
30
|
+
|
31
|
+
== 0.0.3 2008-03-27
|
32
|
+
* 2 minor fixes:
|
33
|
+
* GravatarSource now takes parameter :gravatar_field instead of :field for compatibility with other sources
|
34
|
+
* FileColumnSource now takes parameter :file_column_field instead of :field for compatibility with other sources
|
35
|
+
|
36
|
+
* 2 minor enhancements:
|
37
|
+
* broke out options parsing in FileColumnSource to allow overriding
|
38
|
+
* added :gravatar_xxx versions to allowed options for GravatarSource
|
39
|
+
|
40
|
+
== 0.0.2 2008-03-24
|
41
|
+
* 1 major fix:
|
42
|
+
* SourceChain duplicates options before passing to elements
|
43
|
+
|
44
|
+
* 1 minor fix:
|
45
|
+
* forced GravatarSource to coerce rating and size to proper values
|
46
|
+
|
47
|
+
* 1 minor enhancement:
|
48
|
+
* broke out URL generation in GravatarSource to allow overriding
|
49
|
+
|
50
|
+
== 0.0.1 2008-03-24
|
51
|
+
|
52
|
+
* 1 major enhancement:
|
53
|
+
* Initial release
|
54
|
+
* AbstractSource
|
55
|
+
* FileColumnSource
|
56
|
+
* GravatarSource
|
57
|
+
* NilSource
|
58
|
+
* SourceChain
|
59
|
+
* StaticUrlSource
|
60
|
+
* StringSubstitutionSource
|
61
|
+
* AbstractViewSupport
|
62
|
+
* ActionViewSupport
|
63
|
+
* tests
|
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Universal Presence, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
= avatar
|
2
|
+
|
3
|
+
* http://avatar.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
Avatar is a collection of Ruby utilities for displaying avatars.
|
7
|
+
|
8
|
+
== FEATURES/PROBLEMS:
|
9
|
+
|
10
|
+
Avatar currently supports the following sources:
|
11
|
+
* Gravatar (see http://www.gravatar.com)
|
12
|
+
* Initial Pavatar support (see http://pavatar.com/spec/)
|
13
|
+
* a constant URL (e.g. http://mysite.com/images/default_icon.png)
|
14
|
+
* parameterized URLs (e.g. http://mysite.com/images/famfamfam/user_#{color}.png)
|
15
|
+
* file_column (see http://www.kanthak.net/opensource/file_column/)
|
16
|
+
* chains of sources (e.g. file_column if exists; otherwise default constant URL)
|
17
|
+
|
18
|
+
Avatar currently supports the following views:
|
19
|
+
* ActionView (Rails), through avatar_tag
|
20
|
+
* AbstractView (any framework), through avatar_url_for
|
21
|
+
|
22
|
+
== BUG REPORTING:
|
23
|
+
|
24
|
+
Submit bugs to https://gcnovus.fogbugz.com/default.asp?pg=pgPublicEdit under the "Avatar Gem" project,
|
25
|
+
or discuss at http://groups.google.com/group/ruby-avatar/.
|
26
|
+
|
27
|
+
== BASICS:
|
28
|
+
|
29
|
+
in RAILS_ROOT/app/helpers/people_helper.rb
|
30
|
+
require 'avatar/view/action_view_support'
|
31
|
+
class PeopleHelper
|
32
|
+
include Avatar::View::ActionViewSupport
|
33
|
+
end
|
34
|
+
|
35
|
+
in RAILS_ROOT/app/views/people/show.html.erb:
|
36
|
+
<%= avatar_tag(@current_user, :size => 40) %>
|
37
|
+
|
38
|
+
== REQUIREMENTS:
|
39
|
+
|
40
|
+
* none for basic functionality
|
41
|
+
* will integrate with ActionView
|
42
|
+
* will integrate with the file_column Rails plugin
|
43
|
+
|
44
|
+
== INSTALL:
|
45
|
+
|
46
|
+
* sudo gem install avatar
|
47
|
+
|
48
|
+
== LICENSE:
|
49
|
+
|
50
|
+
(The MIT License)
|
51
|
+
|
52
|
+
Copyright (c) 2008 James Rosen
|
53
|
+
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
55
|
+
a copy of this software and associated documentation files (the
|
56
|
+
'Software'), to deal in the Software without restriction, including
|
57
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
58
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
59
|
+
permit persons to whom the Software is furnished to do so, subject to
|
60
|
+
the following conditions:
|
61
|
+
|
62
|
+
The above copyright notice and this permission notice shall be
|
63
|
+
included in all copies or substantial portions of the Software.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
66
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
67
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
68
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
69
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
70
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
71
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/init.rb
ADDED
data/lib/avatar.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'avatar/source/abstract_source'
|
3
|
+
require 'avatar/source/gravatar_source'
|
4
|
+
require 'avatar/source/pavatar_source'
|
5
|
+
|
6
|
+
# Helpers for displaying avatars.
|
7
|
+
# Usage in Rails:
|
8
|
+
# # in app/helpers/ProfileHelper.rb:
|
9
|
+
# include Avatar::ActionView::Support
|
10
|
+
#
|
11
|
+
# # in app/views/profiles/show.html.erb:
|
12
|
+
# <%= avatar_for @person => current_person %>
|
13
|
+
#
|
14
|
+
# By default, Avatar::source is a GravatarSource
|
15
|
+
module Avatar
|
16
|
+
|
17
|
+
@@source = Avatar::Source::GravatarSource.new
|
18
|
+
@@default_avatar_options = {}
|
19
|
+
|
20
|
+
def self.source
|
21
|
+
@@source.dup
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.source=(source)
|
25
|
+
raise ArgumentError.new("#{source} is not an Avatar::Source::AbstractSource") unless source.kind_of?(Avatar::Source::AbstractSource)
|
26
|
+
@@source = source
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.default_avatar_options
|
30
|
+
@@default_avatar_options.dup
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.default_avatar_options=(options)
|
34
|
+
raise ArgumentError.new("#{options} is not a Hash") unless options.kind_of?(Hash)
|
35
|
+
@@default_avatar_options = options
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Avatar # :nodoc:
|
2
|
+
module Source # :nodoc:
|
3
|
+
# To be included by classes that generate avatar URLs from profiles.
|
4
|
+
module AbstractSource
|
5
|
+
|
6
|
+
# Return an avatar URL for the person, or nil if this source cannot generate one.
|
7
|
+
# Including classes <em>must</em> override this method. In general, implementations
|
8
|
+
# should return nil if +person+ is nil.
|
9
|
+
def avatar_url_for(person, options = {})
|
10
|
+
raise NotImplementedError.new('including class must define avatar_url_for(person, options = {})')
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'avatar/source/abstract_source'
|
2
|
+
require 'file_column_helper'
|
3
|
+
|
4
|
+
module Avatar # :nodoc:
|
5
|
+
module Source # :nodoc:
|
6
|
+
# For use with the FileColumn Rails plugin.
|
7
|
+
class FileColumnSource
|
8
|
+
include AbstractSource
|
9
|
+
include FileColumnHelper
|
10
|
+
|
11
|
+
attr_accessor :default_field
|
12
|
+
|
13
|
+
def initialize(default_field = :avatar)
|
14
|
+
raise ArgumentError.new('default_field cannot be nil') if default_field.nil?
|
15
|
+
@default_field = default_field
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns nil if person is nil; otherwise, returns the (possibly nil) result of
|
19
|
+
# <code>url_for_image_column</code>, passing in all of +options+ except :file_column_field.
|
20
|
+
# Options:
|
21
|
+
# * <code>:file_column_field</code> - the image file column within +person+; by default, :avatar
|
22
|
+
# * <code>:file_column_version</code> - one of the versions of the file_column; no default
|
23
|
+
# If :file_column_version is not specified, all other options are passed to
|
24
|
+
# <code>url_for_image_column</code> as +options+ (see FileColumnHelper)
|
25
|
+
def avatar_url_for(person, options = {})
|
26
|
+
return nil if person.nil?
|
27
|
+
options = parse_options(person, options)
|
28
|
+
field = options.delete(:file_column_field) || default_field
|
29
|
+
return nil if field.nil? || person.send(field).nil?
|
30
|
+
options = options[:file_column_version] || options
|
31
|
+
url_for_image_column(person, field, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse_options(person, options)
|
35
|
+
options
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'avatar/object_support'
|
2
|
+
require 'avatar/source/abstract_source'
|
3
|
+
require 'avatar/source/static_url_source'
|
4
|
+
require 'avatar/source/nil_source'
|
5
|
+
require 'digest/md5'
|
6
|
+
|
7
|
+
module Avatar # :nodoc:
|
8
|
+
module Source # :nodoc:
|
9
|
+
# NOTE: since Gravatar always returns a URL (never a 404), instances of this
|
10
|
+
# class should only be placed at the end of a SourceChain.
|
11
|
+
# (see link:classes/Avatar/Source/SourceChain.html)
|
12
|
+
# Alternatively, use <code>default_source = ...</code> to generate a site-wide
|
13
|
+
# default to be passed to Gravatar. (In fact, since <code>default_source</code>
|
14
|
+
# is an instance of Avatar::Source::AbstractSource, it can generate a different
|
15
|
+
# default for each person.)
|
16
|
+
class GravatarSource
|
17
|
+
include AbstractSource
|
18
|
+
|
19
|
+
attr_accessor :default_field
|
20
|
+
attr_reader :default_source
|
21
|
+
|
22
|
+
# 'http://www.gravatar.com/avatar/'
|
23
|
+
def self.base_url
|
24
|
+
'http://www.gravatar.com/avatar/'
|
25
|
+
end
|
26
|
+
|
27
|
+
# ['G', 'PG', 'R', 'X', 'any']
|
28
|
+
def self.allowed_ratings
|
29
|
+
['G', 'PG', 'R', 'X', 'any']
|
30
|
+
end
|
31
|
+
|
32
|
+
# Arguments:
|
33
|
+
# * +default_source+: a Source to generate defaults to be passed to Gravatar; optional; default: nil (a NilSource).
|
34
|
+
# * +default_field+: the field within each +person+ passed to <code>avatar_url_for</code> in which to look for an email address
|
35
|
+
def initialize(default_source = nil, default_field = :email)
|
36
|
+
self.default_source = default_source #not @default_source = ... b/c want to use the setter function below
|
37
|
+
@default_field = default_field
|
38
|
+
raise "There's a bug in the code" if @default_source.nil?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Generates a Gravatar URL. Returns nil if person is nil.
|
42
|
+
# Options:
|
43
|
+
# * <code>:gravatar_field (Symbol)</code> - the field to call from person. By default, <code>:email</code>.
|
44
|
+
# * <code>:gravatar_default_url (String)</code> - override the default generated by <code>default_source</code>.
|
45
|
+
# * <code>:gravatar_size or size or :s</code> - the size in pixels of the avatar to render.
|
46
|
+
# * <code>:gravatar_rating or rating or :r</code> - the maximum rating; one of ['G', 'PG', 'R', 'X']
|
47
|
+
def avatar_url_for(person, options = {})
|
48
|
+
return nil if person.nil?
|
49
|
+
options = parse_options(person, options)
|
50
|
+
field = options.delete(:gravatar_field)
|
51
|
+
raise ArgumentError.new('No field specified; either specify a default field or pass in a value for :gravatar_field (probably :email)') unless field
|
52
|
+
|
53
|
+
email = person.send(field)
|
54
|
+
return nil if email.nil? || email.to_s.blank?
|
55
|
+
email = email.to_s.downcase
|
56
|
+
|
57
|
+
returning(self.class.base_url) do |url|
|
58
|
+
url << Digest::MD5::hexdigest(email).strip
|
59
|
+
# default must be last or the other options will be parameters to that URL, not the Gravatar one
|
60
|
+
[:size, :rating, :default].each do |k|
|
61
|
+
v = options[k]
|
62
|
+
next if v.nil?
|
63
|
+
url << (url.include?('?') ? '&' : '?')
|
64
|
+
url << "#{k}=#{v}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns a Hash containing
|
70
|
+
# * :field - value of :gravatar_field; defaults to <code>self.default_field</code>
|
71
|
+
# * :default - value of :gravatar_default_url; defaults to <code>self.default_avatar_url_for(+person+, +options+)</code>
|
72
|
+
# * :size - value of :gravatar_size or :size or :s passed through <em>only if a number</em>
|
73
|
+
# * :rating - value of :gravatar_rating or :rating or :r passed through <em>only if one of <code>self.class.allowed_ratings</code></em>
|
74
|
+
def parse_options(person, options)
|
75
|
+
returning({}) do |result|
|
76
|
+
result[:gravatar_field] = options[:gravatar_field] || default_field
|
77
|
+
|
78
|
+
default = options[:gravatar_default_url] || default_avatar_url_for(person, options)
|
79
|
+
raise "default must be a fully-qualified URL with port and host" unless self.class.valid_default_url?(default)
|
80
|
+
result[:default] = default
|
81
|
+
|
82
|
+
size = (options[:gravatar_size] || options[:size] || options[:s] || '').to_s.to_i
|
83
|
+
result[:size] = size if size > 0
|
84
|
+
|
85
|
+
rating = options[:gravatar_rating] || options[:rating] || options[:r]
|
86
|
+
result[:rating] = rating if rating and self.class.allowed_ratings.include?(rating.to_s)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Set the default source for all people.
|
91
|
+
# If +default+ is a String, it will be converted to an instance of Avatar::Source::StaticUrlSource.
|
92
|
+
# If +default+ is nil, sets the default to a NilSource.
|
93
|
+
def default_source=(default)
|
94
|
+
case default
|
95
|
+
when String
|
96
|
+
@default_source = StaticUrlSource.new(default)
|
97
|
+
when AbstractSource
|
98
|
+
@default_source = default
|
99
|
+
when NilClass
|
100
|
+
@default_source = NilSource.new
|
101
|
+
else
|
102
|
+
raise ArgumentError.new("#{default} must be either a String or an instance of #{AbstractSource}")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.valid_default_url?(url)
|
107
|
+
url.nil? || url =~ /^http[s]?\:/
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
def default_avatar_url_for(person, options)
|
113
|
+
@default_source.avatar_url_for(person, options)
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'avatar/source/abstract_source'
|
2
|
+
|
3
|
+
module Avatar # :nodoc:
|
4
|
+
module Source # :nodoc:
|
5
|
+
# A really dumb implementation that never returns a URL.
|
6
|
+
# Can be helpful for testing. Also used in GravatarSource::default_source.
|
7
|
+
class NilSource
|
8
|
+
include AbstractSource
|
9
|
+
|
10
|
+
# Always returns nil.
|
11
|
+
def avatar_url_for(person, options = {})
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'avatar/object_support'
|
2
|
+
require 'avatar/source/abstract_source'
|
3
|
+
|
4
|
+
module Avatar # :nodoc:
|
5
|
+
module Source # :nodoc:
|
6
|
+
# Source for a file attachment using Paperclip.
|
7
|
+
# See http://giantrobots.thoughtbot.com/2008/3/18/for-attaching-files-use-paperclip
|
8
|
+
class PaperclipSource
|
9
|
+
include AbstractSource
|
10
|
+
|
11
|
+
attr_accessor :default_field, :default_style
|
12
|
+
|
13
|
+
# Create a new FileColumnSource with a +default_field+ (by default, :avatar),
|
14
|
+
# and a +default_style+ (by default, nil)
|
15
|
+
def initialize(default_field = :avatar, default_style = nil)
|
16
|
+
@default_field = default_field
|
17
|
+
@default_style = default_style
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns a URL for a has_attached_file attribute, via
|
21
|
+
# <code>person.<paperclip_field>.url</code>, passing in :paperclip_style if present.
|
22
|
+
# Returns nil under any of the following circumstances:
|
23
|
+
# * person is nil
|
24
|
+
# * person.<paperclip_field> is nil
|
25
|
+
# * person.<paperclip_field>? returns false
|
26
|
+
# * person.<paperclip_field>.styles does not include :paperclip_style (if present)
|
27
|
+
# Options:
|
28
|
+
# * <code>:paperclip_field</code> - the has_attached_file column within +person+; by default, self.default_field
|
29
|
+
# * <code>:paperclip_style</code> - one of the styles of the has_attached_file; by default, self.default_style
|
30
|
+
def avatar_url_for(person, options = {})
|
31
|
+
return nil if person.nil?
|
32
|
+
options = parse_options(person, options)
|
33
|
+
field = options[:paperclip_field]
|
34
|
+
return nil if field.nil?
|
35
|
+
return nil unless person.send("#{field}?".to_sym)
|
36
|
+
avatar = person.send(field)
|
37
|
+
style = options[:paperclip_style]
|
38
|
+
return nil if style && !avatar.styles.keys.include?(style)
|
39
|
+
avatar.url(style)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Copies :paperclip_field and :paperclip_style from +options+, adding defaults if necessary.
|
43
|
+
def parse_options(person, options)
|
44
|
+
returning({}) do |result|
|
45
|
+
result[:paperclip_field] = options[:paperclip_field] || default_field
|
46
|
+
result[:paperclip_style] = options[:paperclip_style] || default_style
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'avatar/source/abstract_source'
|
2
|
+
|
3
|
+
module Avatar # :nodoc:
|
4
|
+
module Source # :nodoc:
|
5
|
+
class SourceChain
|
6
|
+
include AbstractSource
|
7
|
+
|
8
|
+
# :nodoc:
|
9
|
+
def initialize
|
10
|
+
clear!
|
11
|
+
end
|
12
|
+
|
13
|
+
# Clear the chain
|
14
|
+
def clear!
|
15
|
+
@chain = []
|
16
|
+
end
|
17
|
+
|
18
|
+
# Retrieve the +n+<sup>th</sup> Source.
|
19
|
+
def [](n)
|
20
|
+
@chain[n]
|
21
|
+
end
|
22
|
+
|
23
|
+
# Add a source to the chain. +source+ must be an instance of (a subclass of) Avatar::Source::AbstractSource.
|
24
|
+
def add_source(source)
|
25
|
+
raise ArgumentError.new("#{source} is not an Avatar::Source::AbstractSource") unless source.kind_of?(Avatar::Source::AbstractSource)
|
26
|
+
@chain << source
|
27
|
+
end
|
28
|
+
|
29
|
+
# Alias for <code>add_source</code>
|
30
|
+
def <<(source)
|
31
|
+
add_source(source)
|
32
|
+
end
|
33
|
+
|
34
|
+
# True unless a Source has been added.
|
35
|
+
def empty?
|
36
|
+
@chain.empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
# Iterate through the chain and return the first URL returned.
|
40
|
+
# Any error raised will propagate. Duplicates +options+ before
|
41
|
+
# passing so each Source receives the same arguments.
|
42
|
+
def avatar_url_for(person, options = {})
|
43
|
+
@chain.each do |source|
|
44
|
+
result = source.avatar_url_for(person, options.dup)
|
45
|
+
return result unless result.nil?
|
46
|
+
end
|
47
|
+
return nil
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'avatar/source/abstract_source'
|
2
|
+
|
3
|
+
module Avatar # :nodoc:
|
4
|
+
module Source # :nodoc:
|
5
|
+
# Source representing a constant URL.
|
6
|
+
# Good as a default or last-resort source.
|
7
|
+
class StaticUrlSource
|
8
|
+
include AbstractSource
|
9
|
+
|
10
|
+
attr_accessor :url
|
11
|
+
|
12
|
+
# Create a new source with static url +url+.
|
13
|
+
def initialize(url)
|
14
|
+
raise ArgumentError.new("URL cannot be nil") if url.nil?
|
15
|
+
@url = url.to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns nil if person is nil; the static url otherwise.
|
19
|
+
def avatar_url_for(person, options = {})
|
20
|
+
person.nil? ? nil : url
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'avatar/source/abstract_source'
|
2
|
+
|
3
|
+
module Avatar
|
4
|
+
module Source
|
5
|
+
module Wrapper
|
6
|
+
class AbstractSourceWrapper
|
7
|
+
include Avatar::Source::AbstractSource
|
8
|
+
|
9
|
+
attr_reader :underlying_source
|
10
|
+
|
11
|
+
# Create a new Wrapper
|
12
|
+
def initialize(underlying_source)
|
13
|
+
raise ArgumentError.new("underlying_source must be Source") unless underlying_source.kind_of?(Avatar::Source::AbstractSource)
|
14
|
+
@underlying_source = underlying_source
|
15
|
+
end
|
16
|
+
|
17
|
+
# Return nil if the underlying_source does; otherwise, calls <code>wrap</code>,
|
18
|
+
# passing the returned URL and the person and options passed.
|
19
|
+
def avatar_url_for(person, options = {})
|
20
|
+
url = @underlying_source.avatar_url_for(person, options)
|
21
|
+
url.nil? ? nil : wrap(url, person, options)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Apply appropriate wrapping of the +url+ returned by <code>underlying_source</code>.
|
25
|
+
# Will never be called with a nil +url+.
|
26
|
+
def wrap(url, person, options)
|
27
|
+
raise NotImplementedError('subclasses must override wrap(url, person, options)')
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'avatar/source/wrapper/abstract_source_wrapper'
|
2
|
+
require 'action_view/helpers/tag_helper'
|
3
|
+
require 'action_view/helpers/asset_tag_helper'
|
4
|
+
|
5
|
+
module Avatar # :nodoc:
|
6
|
+
module Source # :nodoc:
|
7
|
+
module Wrapper
|
8
|
+
# Wraps a Source using Rails' <code>AssetTagHelper#image_path</code>,
|
9
|
+
# which can turn path URLs (e.g. '/images/my_avatar.png')
|
10
|
+
# into absolute URLs( e.g. 'http://assets.mysite.com/images/my_avatar.png').
|
11
|
+
class RailsAssetSourceWrapper < AbstractSourceWrapper
|
12
|
+
|
13
|
+
attr_reader :url_helper
|
14
|
+
|
15
|
+
private :url_helper
|
16
|
+
|
17
|
+
def initialize(source)
|
18
|
+
super
|
19
|
+
@url_helper = Object.new
|
20
|
+
@url_helper.extend(ActionView::Helpers::AssetTagHelper)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Passes +url+ to <code>AssetTagHelper#image_path</code>. Raises
|
24
|
+
# an error if it cannot generate a fully-qualified URI. Try
|
25
|
+
# setting <code>ActionController::Base.asset_host</code> to
|
26
|
+
# avoid this error.
|
27
|
+
def wrap(url, person, options = {})
|
28
|
+
# url will never be nil b/c of guarantee in AbstractSourceWrapper
|
29
|
+
result = url_helper.image_path(url)
|
30
|
+
raise "could not generate protocol and host for #{url}. Have you set ActionController::Base.asset_host?" unless result =~ /^http[s]?\:\/\//
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'avatar/source/wrapper/abstract_source_wrapper'
|
2
|
+
require 'action_view/helpers/asset_tag_helper'
|
3
|
+
|
4
|
+
module Avatar # :nodoc:
|
5
|
+
module Source # :nodoc:
|
6
|
+
module Wrapper
|
7
|
+
# Wraps a Source using Rails' <code>AssetTagHelper#image_path</code>,
|
8
|
+
# which can turn path URLs (e.g. '/images/my_avatar.png')
|
9
|
+
# into absolute URLs( e.g. 'http://assets.mysite.com/images/my_avatar.png').
|
10
|
+
class StringSubstitutionSourceWrapper < AbstractSourceWrapper
|
11
|
+
|
12
|
+
attr_accessor :default_options
|
13
|
+
|
14
|
+
def initialize(source, default_options = {})
|
15
|
+
super(source)
|
16
|
+
self.default_options = default_options || {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# Passes +url+ to <code>AssetTagHelper#image_path</code>. Raises
|
20
|
+
# an error if it cannot generate a fully-qualified URI. Try
|
21
|
+
# setting <code>ActionController::Base.asset_host</code> to
|
22
|
+
# avoid this error.
|
23
|
+
def wrap(url, person, options = {})
|
24
|
+
# url will never be nil b/c of guarantee in AbstractSourceWrapper
|
25
|
+
result = apply_substitution(url, self.default_options.merge(options))
|
26
|
+
substitution_needed?(result) ? nil : result
|
27
|
+
end
|
28
|
+
|
29
|
+
def default_options=(opts)
|
30
|
+
@default_options = opts || {}
|
31
|
+
end
|
32
|
+
|
33
|
+
# For each key in +options+ replaces '#{key}' in +string+ with the
|
34
|
+
# corresponding value in +options+.
|
35
|
+
# +string+ should
|
36
|
+
# be of the form '...#{variable_a}...#{variable_b}...'. <em>Note the
|
37
|
+
# single quotes</em>. Double quotes will cause the variables to be
|
38
|
+
# substituted before this method is run, which is almost
|
39
|
+
# certainly <strong>not</strong> what you want.
|
40
|
+
def apply_substitution(string, options)
|
41
|
+
returning(string.dup) do |result|
|
42
|
+
options.each do |k,v|
|
43
|
+
result.gsub!(Regexp.new('#\{' + "#{k}" + '\}'), "#{v}")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def substitution_needed?(string)
|
49
|
+
string =~ /#\{.*\}/
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/avatar/view.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'avatar'
|
2
|
+
|
3
|
+
module Avatar # :nodoc:
|
4
|
+
module View # :nodoc:
|
5
|
+
module AbstractViewSupport
|
6
|
+
|
7
|
+
def avatar_url_for(person, options = {})
|
8
|
+
default_options = Avatar::default_avatar_options || {}
|
9
|
+
options = default_options.merge(options)
|
10
|
+
Avatar::source.avatar_url_for(person, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'avatar/view/abstract_view_support'
|
2
|
+
|
3
|
+
module Avatar # :nodoc:
|
4
|
+
module View # :nodoc:
|
5
|
+
module ActionViewSupport
|
6
|
+
include Avatar::View::AbstractViewSupport
|
7
|
+
|
8
|
+
# Generates an image_tag for the URL or path generated by
|
9
|
+
# avatar_url_for(+person+, +avatar_options+). Passes additional
|
10
|
+
# tag options from +html_options+. Returns an empty string
|
11
|
+
# if +person+ is nil.
|
12
|
+
#
|
13
|
+
# Because this method uses <code>image_tag</code>, Sources
|
14
|
+
# can generate paths instead of absolute URLs.
|
15
|
+
def avatar_tag(person, avatar_options={}, html_options={})
|
16
|
+
return "" if person.nil?
|
17
|
+
url = avatar_url_for(person, avatar_options)
|
18
|
+
image_tag(url, html_options)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# File so gem can act like Rails plugin
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: krasivotokak-avatar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Rosen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-17 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Adds support for rendering avatars from a variety of sources.
|
17
|
+
email: james.a.rosen@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- History.txt
|
26
|
+
- License.txt
|
27
|
+
- README.rdoc
|
28
|
+
- init.rb
|
29
|
+
- rails/init.rb
|
30
|
+
- lib/avatar
|
31
|
+
- lib/avatar/object_support.rb
|
32
|
+
- lib/avatar/source
|
33
|
+
- lib/avatar/source/abstract_source.rb
|
34
|
+
- lib/avatar/source/file_column_source.rb
|
35
|
+
- lib/avatar/source/gravatar_source.rb
|
36
|
+
- lib/avatar/source/nil_source.rb
|
37
|
+
- lib/avatar/source/paperclip_source.rb
|
38
|
+
- lib/avatar/source/source_chain.rb
|
39
|
+
- lib/avatar/source/static_url_source.rb
|
40
|
+
- lib/avatar/source/wrapper
|
41
|
+
- lib/avatar/source/wrapper/abstract_source_wrapper.rb
|
42
|
+
- lib/avatar/source/wrapper/rails_asset_source_wrapper.rb
|
43
|
+
- lib/avatar/source/wrapper/string_substitution_source_wrapper.rb
|
44
|
+
- lib/avatar/source/wrapper.rb
|
45
|
+
- lib/avatar/source.rb
|
46
|
+
- lib/avatar/version.rb
|
47
|
+
- lib/avatar/view
|
48
|
+
- lib/avatar/view/abstract_view_support.rb
|
49
|
+
- lib/avatar/view/action_view_support.rb
|
50
|
+
- lib/avatar/view.rb
|
51
|
+
- lib/avatar.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/gcnovus/avatar
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --line-numbers
|
57
|
+
- --inline-source
|
58
|
+
- --title
|
59
|
+
- Grammar RDoc
|
60
|
+
- --charset
|
61
|
+
- utf-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.2.0
|
80
|
+
signing_key:
|
81
|
+
specification_version: 2
|
82
|
+
summary: Multi-source avatar support
|
83
|
+
test_files: []
|
84
|
+
|