mirror of
https://github.com/discourse/discourse.git
synced 2025-03-24 13:15:41 +08:00
schemaless avatar urls
This commit is contained in:
parent
3cf5a363f7
commit
82b78ec6ba
@ -100,12 +100,11 @@ class Group < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.lookup_group(name)
|
def self.lookup_group(name)
|
||||||
id = AUTO_GROUPS[name]
|
if id = AUTO_GROUPS[name]
|
||||||
if id
|
|
||||||
Group.where(id: id).first
|
Group.where(id: id).first
|
||||||
else
|
else
|
||||||
unless group = Group.where(name: name).first
|
unless group = Group.where(name: name).first
|
||||||
raise ArgumentError, "unknown group" unless group
|
raise ArgumentError, "unknown group"
|
||||||
end
|
end
|
||||||
group
|
group
|
||||||
end
|
end
|
||||||
|
@ -8,9 +8,11 @@ require_dependency 'post_destroyer'
|
|||||||
require_dependency 'user_name_suggester'
|
require_dependency 'user_name_suggester'
|
||||||
require_dependency 'roleable'
|
require_dependency 'roleable'
|
||||||
require_dependency 'pretty_text'
|
require_dependency 'pretty_text'
|
||||||
|
require_dependency 'url_helper'
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
include Roleable
|
include Roleable
|
||||||
|
include UrlHelper
|
||||||
|
|
||||||
has_many :posts
|
has_many :posts
|
||||||
has_many :notifications, dependent: :destroy
|
has_many :notifications, dependent: :destroy
|
||||||
@ -301,21 +303,21 @@ class User < ActiveRecord::Base
|
|||||||
# - emails
|
# - emails
|
||||||
def small_avatar_url
|
def small_avatar_url
|
||||||
template = avatar_template
|
template = avatar_template
|
||||||
template.gsub("{size}", "45")
|
schemaless template.gsub("{size}", "45")
|
||||||
end
|
end
|
||||||
|
|
||||||
# the avatars might take a while to generate
|
# the avatars might take a while to generate
|
||||||
# so return the url of the original image in the meantime
|
# so return the url of the original image in the meantime
|
||||||
def uploaded_avatar_path
|
def uploaded_avatar_path
|
||||||
return unless SiteSetting.allow_uploaded_avatars? && use_uploaded_avatar
|
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
|
end
|
||||||
|
|
||||||
def avatar_template
|
def avatar_template
|
||||||
uploaded_avatar_path || User.gravatar_template(email)
|
uploaded_avatar_path || User.gravatar_template(email)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# The following count methods are somewhat slow - definitely don't use them in a loop.
|
# The following count methods are somewhat slow - definitely don't use them in a loop.
|
||||||
# They might need to be denormalized
|
# They might need to be denormalized
|
||||||
def like_count
|
def like_count
|
||||||
|
@ -924,4 +924,54 @@ describe User do
|
|||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user