Introduce AsciicastPresenter
parent
d571be2bbd
commit
86f6af3d78
@ -0,0 +1,72 @@
|
|||||||
|
class AsciicastPresenter
|
||||||
|
|
||||||
|
attr_reader :asciicast, :user
|
||||||
|
|
||||||
|
def initialize(asciicast, user)
|
||||||
|
@asciicast = AsciicastDecorator.new(asciicast)
|
||||||
|
@user = user
|
||||||
|
end
|
||||||
|
|
||||||
|
def title
|
||||||
|
asciicast_title
|
||||||
|
end
|
||||||
|
|
||||||
|
def asciicast_title
|
||||||
|
asciicast.title
|
||||||
|
end
|
||||||
|
|
||||||
|
def author_img_link
|
||||||
|
asciicast.author_img_link
|
||||||
|
end
|
||||||
|
|
||||||
|
def author_link
|
||||||
|
asciicast.author_link
|
||||||
|
end
|
||||||
|
|
||||||
|
def asciicast_created_at
|
||||||
|
asciicast.created_at
|
||||||
|
end
|
||||||
|
|
||||||
|
def asciicast_env_details
|
||||||
|
"#{asciicast.os} / #{asciicast.shell} / #{asciicast.terminal_type}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def views_count
|
||||||
|
asciicast.views_count
|
||||||
|
end
|
||||||
|
|
||||||
|
def embed_script(h)
|
||||||
|
src = h.asciicast_url(asciicast, format: :js)
|
||||||
|
id = "asciicast-#{asciicast.id}"
|
||||||
|
%(<script type="text/javascript" src="#{src}" id="#{id}" async></script>)
|
||||||
|
end
|
||||||
|
|
||||||
|
def show_admin_dropdown?
|
||||||
|
asciicast.managable_by?(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
def show_description?
|
||||||
|
asciicast.description.present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def description
|
||||||
|
asciicast.description
|
||||||
|
end
|
||||||
|
|
||||||
|
def show_other_asciicasts_by_author?
|
||||||
|
author.asciicast_count > 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def other_asciicasts_by_author
|
||||||
|
asciicasts = author.asciicasts_excluding(asciicast, 3)
|
||||||
|
|
||||||
|
AsciicastDecorator.decorate_collection(asciicasts)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def author
|
||||||
|
asciicast.user
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -1,6 +0,0 @@
|
|||||||
- if @asciicast.other_by_user.any?
|
|
||||||
section.odd
|
|
||||||
.container
|
|
||||||
.other-asciicasts
|
|
||||||
h2 More by #{@asciicast.author_link}
|
|
||||||
= render 'previews', asciicasts: @asciicast.other_by_user, per_row: 3
|
|
@ -0,0 +1,198 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe AsciicastPresenter do
|
||||||
|
|
||||||
|
let(:presenter) { described_class.new(asciicast, current_user) }
|
||||||
|
let(:asciicast) { stub_model(Asciicast, user: author) }
|
||||||
|
let(:author) { User.new }
|
||||||
|
let(:current_user) { User.new }
|
||||||
|
|
||||||
|
let(:view_context) {
|
||||||
|
controller = ApplicationController.new
|
||||||
|
controller.request = ActionController::TestRequest.new
|
||||||
|
controller.view_context
|
||||||
|
}
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(AsciicastDecorator).to receive(:new).with(asciicast) { asciicast }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#title' do
|
||||||
|
subject { presenter.title }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:title) { 'the-title' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq('the-title') }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#asciicast_title' do
|
||||||
|
subject { presenter.asciicast_title }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:title) { 'the-title' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq('the-title') }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#author_img_link' do
|
||||||
|
subject { presenter.author_img_link }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:author_img_link) { '<a href=...>' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq('<a href=...>') }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#author_link' do
|
||||||
|
subject { presenter.author_link }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:author_link) { '<a href=...>' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq('<a href=...>') }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#asciicast_created_at' do
|
||||||
|
subject { presenter.asciicast_created_at }
|
||||||
|
|
||||||
|
let(:now) { Time.now }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:created_at) { now }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq(now) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#asciicast_env_details' do
|
||||||
|
subject { presenter.asciicast_env_details }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:os) { 'Linux' }
|
||||||
|
allow(asciicast).to receive(:shell) { 'bash' }
|
||||||
|
allow(asciicast).to receive(:terminal_type) { 'xterm' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq('Linux / bash / xterm') }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#views_count' do
|
||||||
|
subject { presenter.views_count }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:views_count) { 5 }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq(5) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#embed_script' do
|
||||||
|
subject { presenter.embed_script(view_context) }
|
||||||
|
|
||||||
|
let(:src_regexp) { /src="[^"]+\b123\b[^"]*\.js"/ }
|
||||||
|
let(:id_regexp) { /id="asciicast-123"/ }
|
||||||
|
let(:script_regexp) {
|
||||||
|
/^<script[^>]+#{src_regexp}[^>]+#{id_regexp}[^>]*><\/script>/
|
||||||
|
}
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:id).and_return(123)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'is an async script tag including asciicast id' do
|
||||||
|
expect(subject).to match(script_regexp)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#show_admin_dropdown?' do
|
||||||
|
subject { presenter.show_admin_dropdown? }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:managable_by?).
|
||||||
|
with(current_user) { managable }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when asciicast can't be managed by the user" do
|
||||||
|
let(:managable) { false }
|
||||||
|
|
||||||
|
it { should be(false) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when asciicast can be managed by the user" do
|
||||||
|
let(:managable) { true }
|
||||||
|
|
||||||
|
it { should be(true) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#show_description?' do
|
||||||
|
subject { presenter.show_description? }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:description) { description }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when description is present" do
|
||||||
|
let(:description) { 'i am description' }
|
||||||
|
|
||||||
|
it { should be(true) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when description isn't present" do
|
||||||
|
let(:description) { '' }
|
||||||
|
|
||||||
|
it { should be(false) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#description' do
|
||||||
|
subject { presenter.description }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(asciicast).to receive(:description) { 'i am description' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should eq('i am description') }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#show_other_asciicasts_by_author?' do
|
||||||
|
subject { presenter.show_other_asciicasts_by_author? }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(author).to receive(:asciicast_count) { count }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user has more than 1 asciicast" do
|
||||||
|
let(:count) { 2 }
|
||||||
|
|
||||||
|
it { should be(true) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user doesn't have more than 1 asciicasts" do
|
||||||
|
let(:count) { 1 }
|
||||||
|
|
||||||
|
it { should be(false) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#other_asciicasts_by_author' do
|
||||||
|
subject { presenter.other_asciicasts_by_author }
|
||||||
|
|
||||||
|
let(:others) { [stub_model(Asciicast, id: 123)] }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(author).to receive(:asciicasts_excluding).
|
||||||
|
with(asciicast, 3) { others }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns decorated asciicasts excluding the given one" do
|
||||||
|
expect(subject.first.title).to eq('asciicast:123')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue