schemaless avatar urls

This commit is contained in:
Régis Hanol 2013-11-22 19:18:45 +01:00
parent 3cf5a363f7
commit 82b78ec6ba
3 changed files with 57 additions and 6 deletions

View file

@ -100,12 +100,11 @@ class Group < ActiveRecord::Base
end
def self.lookup_group(name)
id = AUTO_GROUPS[name]
if id
if id = AUTO_GROUPS[name]
Group.where(id: id).first
else
unless group = Group.where(name: name).first
raise ArgumentError, "unknown group" unless group
raise ArgumentError, "unknown group"
end
group
end

View file

@ -8,9 +8,11 @@ require_dependency 'post_destroyer'
require_dependency 'user_name_suggester'
require_dependency 'roleable'
require_dependency 'pretty_text'
require_dependency 'url_helper'
class User < ActiveRecord::Base
include Roleable
include UrlHelper
has_many :posts
has_many :notifications, dependent: :destroy
@ -301,21 +303,21 @@ class User < ActiveRecord::Base
# - emails
def small_avatar_url
template = avatar_template
template.gsub("{size}", "45")
schemaless template.gsub("{size}", "45")
end
# the avatars might take a while to generate
# so return the url of the original image in the meantime
def uploaded_avatar_path
return unless SiteSetting.allow_uploaded_avatars? && use_uploaded_avatar
uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url)
avatar_template = uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url)
schemaless avatar_template
end
def avatar_template
uploaded_avatar_path || User.gravatar_template(email)
end
# The following count methods are somewhat slow - definitely don't use them in a loop.
# They might need to be denormalized
def like_count

View file

@ -924,4 +924,54 @@ describe User do
end
end
describe "#gravatar_template" do
it "returns a gravatar based template" do
User.gravatar_template("em@il.com").should == "//www.gravatar.com/avatar/6dc2fde946483a1d8a84b89345a1b638.png?s={size}&r=pg&d=identicon"
end
end
describe ".small_avatar_url" do
let(:user) { build(:user, use_uploaded_avatar: true, uploaded_avatar_template: "http://test.localhost/uploaded/avatar/template/{size}.png") }
it "returns a 45-pixel-wide avatar" do
user.small_avatar_url.should == "//test.localhost/uploaded/avatar/template/45.png"
end
end
describe ".uploaded_avatar_path" do
let(:user) { build(:user, use_uploaded_avatar: true, uploaded_avatar_template: "http://test.localhost/uploaded/avatar/template/{size}.png") }
it "returns nothing when uploaded avatars are not allowed" do
SiteSetting.expects(:allow_uploaded_avatars).returns(false)
user.uploaded_avatar_path.should be_nil
end
it "returns a schemaless avatar template" do
user.uploaded_avatar_path.should == "//test.localhost/uploaded/avatar/template/{size}.png"
end
end
describe ".avatar_template" do
let(:user) { build(:user, email: "em@il.com") }
it "returns the uploaded_avatar_path by default" do
user.expects(:uploaded_avatar_path).returns("/uploaded/avatar.png")
user.avatar_template.should == "/uploaded/avatar.png"
end
it "returns the gravatar when no avatar has been uploaded" do
user.expects(:uploaded_avatar_path)
User.expects(:gravatar_template).with(user.email).returns("//gravatar.com/avatar.png")
user.avatar_template.should == "//gravatar.com/avatar.png"
end
end
end