Make routes AsciicastPagePresenter's dependency

private-asciicasts
Marcin Kulik 9 years ago
parent 86559fad20
commit 7d199ad416

@ -19,7 +19,7 @@ class AsciicastsController < ApplicationController
format.html do
view_counter.increment(asciicast, cookies)
render locals: {
page: AsciicastPagePresenter.build(asciicast, current_user, params)
page: AsciicastPagePresenter.build(self, asciicast, current_user, params)
}
end

@ -1,8 +1,8 @@
class AsciicastPagePresenter
attr_reader :asciicast, :current_user, :policy, :playback_options
attr_reader :routes, :asciicast, :current_user, :policy, :playback_options
def self.build(asciicast, current_user, playback_options)
def self.build(routes, asciicast, current_user, playback_options)
decorated_asciicast = asciicast.decorate
policy = Pundit.policy(current_user, asciicast)
@ -10,11 +10,12 @@ class AsciicastPagePresenter
'theme' => decorated_asciicast.theme_name
}.merge(playback_options)
new(decorated_asciicast, current_user, policy,
new(routes, decorated_asciicast, current_user, policy,
PlaybackOptions.new(playback_options))
end
def initialize(asciicast, current_user, policy, playback_options)
def initialize(routes, asciicast, current_user, policy, playback_options)
@routes = routes
@asciicast = asciicast
@current_user = current_user
@policy = policy
@ -49,20 +50,20 @@ class AsciicastPagePresenter
asciicast.views_count
end
def embed_script(routes)
def embed_script
src = routes.asciicast_url(asciicast, format: :js)
id = "asciicast-#{asciicast.id}"
%(<script type="text/javascript" src="#{src}" id="#{id}" async></script>)
end
def embed_html_link(routes)
def embed_html_link
img_src = routes.asciicast_url(asciicast, format: :png)
url = routes.asciicast_url(asciicast)
width = %{width="#{asciicast.image_width}"} if asciicast.image_width
%(<a href="#{url}"><img src="#{img_src}" #{width}/></a>)
end
def embed_markdown_link(routes)
def embed_markdown_link
img_src = routes.asciicast_url(asciicast, format: :png)
url = routes.asciicast_url(asciicast)
"[![asciicast](#{img_src})](#{url})"
@ -107,7 +108,7 @@ class AsciicastPagePresenter
author.asciicasts_excluding(asciicast, 3).decorate
end
def asciicast_oembed_url(routes, format)
def asciicast_oembed_url(format)
routes.oembed_url(url: routes.asciicast_url(asciicast), format: format)
end

@ -1,7 +1,7 @@
- content_for(:title, page.title)
- content_for(:head) do
link rel="alternate" type="application/json+oembed" href="#{page.asciicast_oembed_url(self, :json)}"
link rel="alternate" type="text/xml+oembed" href="#{page.asciicast_oembed_url(self, :xml)}"
link rel="alternate" type="application/json+oembed" href="#{page.asciicast_oembed_url(:json)}"
link rel="alternate" type="text/xml+oembed" href="#{page.asciicast_oembed_url(:xml)}"
.asciicast-page
section.cinema
@ -89,7 +89,7 @@
h2 Player
p Paste this script tag where you want the player to be displayed on your page:
p
input[type="text" value=page.embed_script(self) data-behavior="auto-select" readonly]
input[type="text" value=page.embed_script data-behavior="auto-select" readonly]
p See the #{link_to 'embedding docs', docs_path(:embedding)} for additional options.
h2 Image link
@ -97,8 +97,8 @@
p
span HTML:
br
input[type="text" value=page.embed_html_link(self) data-behavior="auto-select" readonly]
input[type="text" value=page.embed_html_link data-behavior="auto-select" readonly]
p
span Markdown:
br
input[type="text" value=page.embed_markdown_link(self) data-behavior="auto-select" readonly]
input[type="text" value=page.embed_markdown_link data-behavior="auto-select" readonly]

@ -51,7 +51,7 @@ describe AsciicastsController do
allow(controller).to receive(:render)
allow(controller).to receive(:current_user) { user }
allow(AsciicastPagePresenter).to receive(:build).
with(asciicast, user, hash_including('speed' => '3.0')).
with(controller, asciicast, user, hash_including('speed' => '3.0')).
and_return(asciicast_presenter)
get :show, id: asciicast.id, format: :html, speed: 3.0

@ -2,8 +2,14 @@ require 'rails_helper'
describe AsciicastPagePresenter do
let(:routes) {
controller = ApplicationController.new
controller.request = ActionController::TestRequest.new
controller.view_context
}
describe '.build' do
subject { described_class.build(asciicast, user, playback_options) }
subject { described_class.build(routes, asciicast, user, playback_options) }
let(:asciicast) { stub_model(Asciicast, decorate: decorated_asciicast) }
let(:user) { double('user') }
@ -24,18 +30,12 @@ describe AsciicastPagePresenter do
end
end
let(:presenter) { described_class.new(asciicast, current_user, policy, nil) }
let(:presenter) { described_class.new(routes, asciicast, current_user, policy, nil) }
let(:asciicast) { stub_model(Asciicast, user: author).decorate }
let(:current_user) { User.new }
let(:policy) { double('policy') }
let(:author) { User.new }
let(:view_context) {
controller = ApplicationController.new
controller.request = ActionController::TestRequest.new
controller.view_context
}
describe '#title' do
subject { presenter.title }
@ -111,7 +111,7 @@ describe AsciicastPagePresenter do
end
describe '#embed_script' do
subject { presenter.embed_script(view_context) }
subject { presenter.embed_script }
let(:asciicast) { stub_model(Asciicast, id: 123).decorate }
let(:src_regexp) { /src="[^"]+\b123\b[^"]*\.js"/ }

Loading…
Cancel
Save