Refactor profile link helpers and decorators

openid
Marcin Kulik 11 years ago
parent df8da8d6d0
commit e2e117ce84

@ -1,9 +1,9 @@
class AsciicastDecorator < ApplicationDecorator
decorates_association :user
THUMBNAIL_WIDTH = 20
THUMBNAIL_HEIGHT = 10
decorates_association :user
def os
if user_agent.present?
os_from_user_agent
@ -40,19 +40,11 @@ class AsciicastDecorator < ApplicationDecorator
end
def author_link
if user
user.link
else
author
end
user.link
end
def author_img_link
if user
user.img_link
else
h.avatar_image_tag nil
end
user.img_link
end
def other_by_user
@ -65,16 +57,6 @@ class AsciicastDecorator < ApplicationDecorator
end
end
def author
if user
user.nickname
elsif model.username
model.username
else
'anonymous'
end
end
def embed_script
src = h.asciicast_url(model, :format => :js)
id = "asciicast-#{model.id}"

@ -0,0 +1,24 @@
module AvatarHelper
def avatar_image_tag
h.image_tag avatar_url, alt: model.nickname, class: 'avatar'
end
private
def avatar_url
gravatar_url || model.avatar_url || default_avatar_filename
end
def gravatar_url
return unless model.email.present?
hash = Digest::MD5.hexdigest(model.email.to_s.downcase)
"//gravatar.com/avatar/#{hash}?s=128"
end
def default_avatar_filename
h.image_path "default_avatar.png"
end
end

@ -1,18 +1,12 @@
class UserDecorator < ApplicationDecorator
include AvatarHelper
def link(options = {})
text = block_given? ? yield : nickname
h.link_to text, h.profile_path(model), :title => options[:title] || nickname
def link
wrap_with_link(nickname)
end
def img_link(options = {})
link(options) do
h.avatar_image_tag(self)
end
end
def avatar_url
gravatar_url || model.avatar_url || h.default_avatar_filename
def img_link
wrap_with_link(avatar_image_tag)
end
def fullname_and_nickname
@ -25,11 +19,12 @@ class UserDecorator < ApplicationDecorator
private
def gravatar_url
return unless email.present?
hash = Digest::MD5.hexdigest(model.email.to_s.downcase)
"//gravatar.com/avatar/#{hash}?s=128"
def wrap_with_link(html)
if id
h.link_to html, h.profile_path(model), title: nickname
else
html
end
end
end

@ -66,18 +66,6 @@ module ApplicationHelper
content_tag(:abbr, time.to_s, options.merge(:title => time.getutc.iso8601))
end
def avatar_image_tag(user, options = {})
klass = options[:class] || "avatar"
title = options[:title] || user.try(:nickname)
avatar = user.try(:avatar_url) || default_avatar_filename
image_tag avatar, :alt => title, :class => klass
end
def default_avatar_filename
image_path "default_avatar.png"
end
def color_check_asciicast_path
if id = CFG['COLOR_CHECK_CAST_ID']
asciicast_path(id)

@ -27,6 +27,7 @@ module AsciicastsHelper
}
end
# TODO: move to AsciicastDecorator
def link_to_delete_asciicast(name, asciicast)
link_to name, asciicast_path(asciicast), :method => :delete,
:data => { :confirm => 'Really delete this asciicast?' }

@ -46,6 +46,10 @@ class Asciicast < ActiveRecord::Base
collection.order("#{ORDER_MODES[order]} DESC")
end
def user
super || self.user = User.new(nickname: username)
end
def stdout
@stdout ||= BufferedStdout.new(stdout_data.decompressed_path,
stdout_timing.decompressed_path).lazy

@ -21,7 +21,7 @@ header.navbar.navbar-default[role="navigation"]
- if current_user
li.dropdown
a.dropdown-toggle[href="#" data-toggle="dropdown"]
= avatar_image_tag(current_user)
= current_user.avatar_image_tag
= current_user.nickname
b.caret
ul.dropdown-menu

@ -3,7 +3,7 @@
.profile-page
section.cinema
.container
span.user-avatar = avatar_image_tag(@user)
span.user-avatar = @user.avatar_image_tag
h1
= @user.fullname_and_nickname
small

@ -23,7 +23,7 @@ module Asciinema
# config.i18n.default_locale = :de
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/decorators/helpers)
# Disable generation of helpers, javascripts, css, and view specs
config.generators do |generate|

@ -188,102 +188,30 @@ describe AsciicastDecorator do
end
end
describe '#author' do
let(:method) { :author }
context 'when user present' do
let(:nickname) { double('nickname') }
let(:user) { double('user', :nickname => nickname) }
before do
asciicast.user = User.new
end
it 'returns nickname from decorated user' do
decorator.should_receive(:user).twice.and_return(user)
subject.should == nickname
end
end
context 'when username present on asciicast' do
before do
asciicast.user = nil
asciicast.username = 'foo'
end
it { should == 'foo' }
end
context 'when no user nor username present' do
before do
asciicast.user = nil
asciicast.username = nil
end
it { should == 'anonymous' }
end
end
describe '#author_link' do
let(:method) { :author_link }
subject { decorator.author_link }
context 'when user present' do
let(:link) { double('link') }
let(:user) { double('user', :link => link) }
let(:asciicast) { double('asciicast', user: user) }
let(:user) { double('user', link: 'link') }
before do
asciicast.user = User.new
end
it 'returns link from decorated user' do
decorator.should_receive(:user).twice.and_return(user)
subject.should == link
end
before do
allow(user).to receive(:decorate) { user }
end
context 'when no user present' do
let(:author) { double('author') }
before do
asciicast.user = nil
end
it 'returns author from decorated user' do
decorator.should_receive(:author).and_return(author)
subject.should == author
end
end
it { should eq('link') }
end
describe '#author_img_link' do
let(:method) { :author_img_link }
context 'when user present' do
let(:img_link) { double('img_link') }
let(:user) { double('user', :img_link => img_link) }
subject { decorator.author_img_link }
before do
asciicast.user = User.new
end
let(:asciicast) { double('asciicast', user: user) }
let(:user) { double('user', img_link: 'img-link') }
it 'returns img_link from decorated user' do
decorator.should_receive(:user).twice.and_return(user)
subject.should == img_link
end
before do
allow(user).to receive(:decorate) { user }
end
context 'when no user present' do
let(:avatar_image) { double('avatar_image') }
before do
asciicast.user = nil
allow(helpers).to receive(:avatar_image_tag).with(nil) { avatar_image }
end
it 'returns avatar_image_tag' do
subject.should == avatar_image
end
end
it { should eq('img-link') }
end
describe '#other_by_user' do

@ -0,0 +1,42 @@
require 'spec_helper'
describe AvatarHelper do
def expected_img(src)
%(<img alt="satyr" class="avatar" src="#{src}" />)
end
let(:decorator) { double('decorator', h: h, model: model).
extend(described_class) }
let(:model) { double('model', nickname: 'satyr', avatar_url: avatar_url,
email: email) }
describe '#avatar_image_tag' do
subject { decorator.avatar_image_tag }
context "when user has an avatar_url" do
let(:avatar_url) { 'http://avatar/url' }
context "and user has an email" do
let(:email) { 'foo@email.com' }
it { should eq(expected_img(
'//gravatar.com/avatar/9dcfeb70fe212ea12562dddd22b0fc92?s=128')) }
end
context "and user has no email" do
let(:email) { nil }
it { should eq(expected_img("http://avatar/url")) }
end
end
context "when user has neither email nor avatar_url" do
let(:email) { nil }
let(:avatar_url) { nil }
it { should eq(expected_img('/assets/default_avatar.png')) }
end
end
end

@ -4,35 +4,64 @@ describe UserDecorator do
let(:decorator) { described_class.new(user) }
describe '#avatar_url' do
subject { decorator.avatar_url }
describe '#link' do
subject { decorator.link }
let(:user) { double('user', avatar_url: avatar_url, email: email) }
let(:user) { User.new(nickname: 'satyr') }
context "when use has avatar_url" do
let(:avatar_url) { 'http://avatar/url' }
before do
allow(h).to receive(:profile_path).with(user) { '/path' }
end
context "when user has id" do
before do
user.id = 1
end
it "is a nickname link to user's profile" do
expect(subject).to eq('<a href="/path" title="satyr">satyr</a>')
end
end
context "and user has email" do
let(:email) { 'foo@email.com' }
context "when user has no id" do
before do
user.id = nil
end
it {
should
eq('//gravatar.com/avatar/9dcfeb70fe212ea12562dddd22b0fc92?s=128')
}
it "is user's nickname" do
expect(subject).to eq('satyr')
end
end
end
describe '#img_link' do
subject { decorator.img_link }
let(:user) { User.new(nickname: 'satyr') }
before do
allow(h).to receive(:profile_path).with(user) { '/path' }
allow(decorator).to receive(:avatar_image_tag) { '<img ...>'.html_safe }
end
context "and user has no email" do
let(:email) { nil }
context "when user has id" do
before do
user.id = 1
end
it { should eq('http://avatar/url') }
it "is an avatar link to user's profile" do
expect(subject).to eq('<a href="/path" title="satyr"><img ...></a>')
end
end
context "when user has neither email nor avatar_url" do
let(:email) { nil }
let(:avatar_url) { nil }
context "when user has no id" do
before do
user.id = nil
end
it { should eq(h.default_avatar_filename) }
it "is user's avatar image" do
expect(subject).to eq('<img ...>')
end
end
end

@ -54,6 +54,30 @@ describe Asciicast do
let(:asciicast) { described_class.new }
describe '#user' do
subject { asciicast.user }
before do
asciicast.username = 'hugo'
end
context "when user was set" do
let(:user) { User.new }
before do
asciicast.user = user
end
it { should be(user) }
end
context "when user wasn't set" do
it 'defaults to a user with nickname set to username' do
expect(asciicast.user.nickname).to eq('hugo')
end
end
end
describe '#stdout' do
let(:asciicast) { Asciicast.new }
let(:data_uploader) { double('data_uploader',

Loading…
Cancel
Save