Skip to content

Commit 82b78ec

Browse files
committed
schemaless avatar urls
1 parent 3cf5a36 commit 82b78ec

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

app/models/group.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ def self.[](name)
100100
end
101101

102102
def self.lookup_group(name)
103-
id = AUTO_GROUPS[name]
104-
if id
103+
if id = AUTO_GROUPS[name]
105104
Group.where(id: id).first
106105
else
107106
unless group = Group.where(name: name).first
108-
raise ArgumentError, "unknown group" unless group
107+
raise ArgumentError, "unknown group"
109108
end
110109
group
111110
end

app/models/user.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
require_dependency 'user_name_suggester'
99
require_dependency 'roleable'
1010
require_dependency 'pretty_text'
11+
require_dependency 'url_helper'
1112

1213
class User < ActiveRecord::Base
1314
include Roleable
15+
include UrlHelper
1416

1517
has_many :posts
1618
has_many :notifications, dependent: :destroy
@@ -301,21 +303,21 @@ def self.gravatar_template(email)
301303
# - emails
302304
def small_avatar_url
303305
template = avatar_template
304-
template.gsub("{size}", "45")
306+
schemaless template.gsub("{size}", "45")
305307
end
306308

307309
# the avatars might take a while to generate
308310
# so return the url of the original image in the meantime
309311
def uploaded_avatar_path
310312
return unless SiteSetting.allow_uploaded_avatars? && use_uploaded_avatar
311-
uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url)
313+
avatar_template = uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url)
314+
schemaless avatar_template
312315
end
313316

314317
def avatar_template
315318
uploaded_avatar_path || User.gravatar_template(email)
316319
end
317320

318-
319321
# The following count methods are somewhat slow - definitely don't use them in a loop.
320322
# They might need to be denormalized
321323
def like_count

spec/models/user_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,4 +924,54 @@
924924
end
925925
end
926926

927+
describe "#gravatar_template" do
928+
929+
it "returns a gravatar based template" do
930+
User.gravatar_template("em@il.com").should == "//www.gravatar.com/avatar/6dc2fde946483a1d8a84b89345a1b638.png?s={size}&r=pg&d=identicon"
931+
end
932+
933+
end
934+
935+
describe ".small_avatar_url" do
936+
937+
let(:user) { build(:user, use_uploaded_avatar: true, uploaded_avatar_template: "http://test.localhost/uploaded/avatar/template/{size}.png") }
938+
939+
it "returns a 45-pixel-wide avatar" do
940+
user.small_avatar_url.should == "//test.localhost/uploaded/avatar/template/45.png"
941+
end
942+
943+
end
944+
945+
describe ".uploaded_avatar_path" do
946+
947+
let(:user) { build(:user, use_uploaded_avatar: true, uploaded_avatar_template: "http://test.localhost/uploaded/avatar/template/{size}.png") }
948+
949+
it "returns nothing when uploaded avatars are not allowed" do
950+
SiteSetting.expects(:allow_uploaded_avatars).returns(false)
951+
user.uploaded_avatar_path.should be_nil
952+
end
953+
954+
it "returns a schemaless avatar template" do
955+
user.uploaded_avatar_path.should == "//test.localhost/uploaded/avatar/template/{size}.png"
956+
end
957+
958+
end
959+
960+
describe ".avatar_template" do
961+
962+
let(:user) { build(:user, email: "em@il.com") }
963+
964+
it "returns the uploaded_avatar_path by default" do
965+
user.expects(:uploaded_avatar_path).returns("/uploaded/avatar.png")
966+
user.avatar_template.should == "/uploaded/avatar.png"
967+
end
968+
969+
it "returns the gravatar when no avatar has been uploaded" do
970+
user.expects(:uploaded_avatar_path)
971+
User.expects(:gravatar_template).with(user.email).returns("//gravatar.com/avatar.png")
972+
user.avatar_template.should == "//gravatar.com/avatar.png"
973+
end
974+
975+
end
976+
927977
end

0 commit comments

Comments
 (0)