Use proper PlaybackOptions object for passing options to JS player

openid
Marcin Kulik 11 years ago
parent 2191dfe32a
commit 57bcab04c9

@ -29,6 +29,7 @@ gem 'sinatra', '~> 1.4.3', :require => false
gem 'active_model_serializers', '~> 0.8.1'
gem 'yajl-ruby', '~> 1.1.0', :require => 'yajl'
gem 'newrelic_rpm'
gem 'virtus', '~> 1.0.1'
group :development do
gem 'quiet_assets', '~> 1.0.1'

@ -41,6 +41,9 @@ GEM
json
arel (4.0.1)
atomic (1.1.14)
axiom-types (0.0.5)
descendants_tracker (~> 0.0.1)
ice_nine (~> 0.9)
builder (3.1.4)
cane (2.5.2)
parallel
@ -65,6 +68,8 @@ GEM
timers (~> 1.1.0)
cliver (0.3.1)
coderay (1.1.0)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
@ -76,6 +81,7 @@ GEM
daemons (1.1.9)
dalli (2.6.4)
database_cleaner (1.0.1)
descendants_tracker (0.0.3)
diff-lcs (1.2.5)
dotenv (0.8.0)
dotenv-rails (0.8.0)
@ -84,6 +90,7 @@ GEM
actionpack (>= 3.0)
activesupport (>= 3.0)
request_store (~> 1.0.3)
equalizer (0.0.9)
erubis (2.7.0)
eventmachine (1.0.3)
excon (0.25.3)
@ -123,6 +130,7 @@ GEM
hike (1.2.3)
httpauth (0.2.0)
i18n (0.6.9)
ice_nine (0.11.0)
jasmine-core (1.3.1)
jasmine-rails (0.4.5)
jasmine-core (~> 1.3)
@ -308,6 +316,11 @@ GEM
kgio (~> 2.6)
rack
raindrops (~> 0.7)
virtus (1.0.1)
axiom-types (~> 0.0.5)
coercible (~> 1.0)
descendants_tracker (~> 0.0.1)
equalizer (~> 0.0.7)
websocket-driver (0.3.1)
xpath (2.0.0)
nokogiri (~> 1.3)
@ -365,4 +378,5 @@ DEPENDENCIES
thin (~> 1.5.0)
uglifier (>= 2.3.1)
unicorn (~> 4.7)
virtus (~> 1.0.1)
yajl-ruby (~> 1.1.0)

@ -19,7 +19,9 @@ class AsciicastsController < ApplicationController
respond_to do |format|
format.html do
view_counter.increment(asciicast, cookies)
render locals: { page: AsciicastPresenter.new(asciicast, current_user) }
render locals: {
page: AsciicastPresenter.build(asciicast, current_user, params)
}
end
format.json do

@ -1,29 +1,16 @@
module AsciicastsHelper
def player(asciicast, options = {})
if params[:fallback]
player_class = "Asciinema.FallbackPlayer"
else
player_class =
"window.Worker ? Asciinema.Player : Asciinema.FallbackPlayer"
end
if custom_renderer = params[:renderer]
renderer_class = "Asciinema.Renderer.#{custom_renderer.capitalize}"
else
renderer_class = "Asciinema.Renderer.Pre"
end
def player(asciicast, options = PlaybackOptions.new)
render :partial => 'asciicasts/player', :locals => {
asciicast: serialized_asciicast(asciicast),
player_class: player_class,
speed: (options[:speed] || params[:speed] || 1).to_f,
benchmark: !!params[:bm],
container_width: params[:container_width],
renderer_class: renderer_class,
auto_play: options.key?(:auto_play) ? !!options[:auto_play] : false,
hud: options.key?(:hud) ? !!options[:hud] : true,
size: options[:size] || params[:size] || 'small'
asciicast: serialized_asciicast(asciicast),
player_class: options.player_class,
speed: options.speed,
benchmark: options.benchmark,
container_width: options.max_width,
renderer_class: options.renderer_class,
auto_play: options.autoplay,
hud: !options.hide_hud,
size: options.font_size,
}
end

@ -0,0 +1,26 @@
class PlaybackOptions
include Virtus.model
attribute :speed, Float, default: 1.0
attribute :font_size, String, default: 'small'
attribute :autoplay, Boolean, default: false
attribute :max_width, Integer
attribute :hide_hud, Boolean, default: false
attribute :fallback, Boolean, default: false
attribute :renderer, String, default: 'Pre'
attribute :benchmark, Boolean, default: false
def player_class
if fallback
"Asciinema.FallbackPlayer"
else
"window.Worker ? Asciinema.Player : Asciinema.FallbackPlayer"
end
end
def renderer_class
"Asciinema.Renderer.#{renderer}"
end
end

@ -1,10 +1,15 @@
class AsciicastPresenter
attr_reader :asciicast, :user
attr_reader :asciicast, :user, :playback_options
def initialize(asciicast, user)
@asciicast = asciicast.decorate
@user = user
def self.build(asciicast, user, playback_options)
new(asciicast.decorate, user, PlaybackOptions.new(playback_options))
end
def initialize(asciicast, user, playback_options)
@asciicast = asciicast
@user = user
@playback_options = playback_options
end
def title

@ -1,5 +1,11 @@
class HomePresenter
attr_reader :playback_options
def initialize
@playback_options = PlaybackOptions.new(speed: 2.0, hide_hud: true)
end
def asciicast
@asciicast ||= get_asciicast
end

@ -2,7 +2,7 @@
javascript:
$(function() {
var playerClass = #{player_class.html_safe};
var playerClass = #{player_class};
var containerWidth = #{container_width || 'null'};
window.player = new playerClass({
@ -11,7 +11,7 @@ javascript:
benchmark: #{benchmark},
model: new Asciinema.Asciicast(#{asciicast.html_safe}),
containerWidth: containerWidth || $('.cinema .player').parent().width(),
rendererClass: #{renderer_class.html_safe},
rendererClass: #{renderer_class},
autoPlay: #{auto_play},
hud: #{hud}
});

@ -3,7 +3,7 @@
.asciicast-page
section.cinema
.container
= player page.asciicast
= player page.asciicast, page.playback_options
section.odd.info
.container

@ -4,7 +4,7 @@
.row
.col-md-6
.player-wrapper
= player page.asciicast, speed: 2.0, hud: false if page.asciicast
= player page.asciicast, page.playback_options if page.asciicast
.col-md-6
h1

@ -52,10 +52,11 @@ describe AsciicastsController do
before do
allow(controller).to receive(:render)
allow(controller).to receive(:current_user) { user }
allow(AsciicastPresenter).to receive(:new).with(asciicast, user).
allow(AsciicastPresenter).to receive(:build).
with(asciicast, user, hash_including('speed' => '3.0')).
and_return(asciicast_presenter)
get :show, :id => asciicast.id, :format => :html
get :show, id: asciicast.id, format: :html, speed: 3.0
end
it { should be_success }

@ -2,10 +2,30 @@ require 'spec_helper'
describe AsciicastPresenter do
let(:presenter) { described_class.new(asciicast, current_user) }
let(:asciicast) { stub_model(Asciicast, decorate: decorated_asciicast) }
describe '.build' do
subject { described_class.build(asciicast, user, playback_options) }
let(:asciicast) { stub_model(Asciicast, decorate: decorated_asciicast) }
let(:user) { double('user') }
let(:playback_options) { { speed: 3.0 } }
let(:decorated_asciicast) { double('decorated_asciicast') }
it "builds presenter instance with given asciicast decorated" do
expect(subject.asciicast).to be(decorated_asciicast)
end
it "builds presenter instance with given user" do
expect(subject.user).to be(user)
end
it "builds presenter instance with given playback options" do
expect(subject.playback_options.speed).to eq(3.0)
end
end
let(:presenter) { described_class.new(asciicast, current_user, nil) }
let(:asciicast) { stub_model(Asciicast, user: author) }
let(:current_user) { User.new }
let(:decorated_asciicast) { stub_model(Asciicast, user: author) }
let(:author) { User.new }
let(:view_context) {
@ -14,15 +34,11 @@ describe AsciicastPresenter do
controller.view_context
}
before do
allow(asciicast).to receive(:decorate) { decorated_asciicast }
end
describe '#title' do
subject { presenter.title }
before do
allow(decorated_asciicast).to receive(:title) { 'the-title' }
allow(asciicast).to receive(:title) { 'the-title' }
end
it { should eq('the-title') }
@ -32,7 +48,7 @@ describe AsciicastPresenter do
subject { presenter.asciicast_title }
before do
allow(decorated_asciicast).to receive(:title) { 'the-title' }
allow(asciicast).to receive(:title) { 'the-title' }
end
it { should eq('the-title') }
@ -42,7 +58,7 @@ describe AsciicastPresenter do
subject { presenter.author_img_link }
before do
allow(decorated_asciicast).to receive(:author_img_link) { '<a href=...>' }
allow(asciicast).to receive(:author_img_link) { '<a href=...>' }
end
it { should eq('<a href=...>') }
@ -52,7 +68,7 @@ describe AsciicastPresenter do
subject { presenter.author_link }
before do
allow(decorated_asciicast).to receive(:author_link) { '<a href=...>' }
allow(asciicast).to receive(:author_link) { '<a href=...>' }
end
it { should eq('<a href=...>') }
@ -64,7 +80,7 @@ describe AsciicastPresenter do
let(:now) { Time.now }
before do
allow(decorated_asciicast).to receive(:created_at) { now }
allow(asciicast).to receive(:created_at) { now }
end
it { should eq(now) }
@ -74,9 +90,9 @@ describe AsciicastPresenter do
subject { presenter.asciicast_env_details }
before do
allow(decorated_asciicast).to receive(:os) { 'Linux' }
allow(decorated_asciicast).to receive(:shell) { 'bash' }
allow(decorated_asciicast).to receive(:terminal_type) { 'xterm' }
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') }
@ -86,7 +102,7 @@ describe AsciicastPresenter do
subject { presenter.views_count }
before do
allow(decorated_asciicast).to receive(:views_count) { 5 }
allow(asciicast).to receive(:views_count) { 5 }
end
it { should eq(5) }
@ -102,7 +118,7 @@ describe AsciicastPresenter do
}
before do
allow(decorated_asciicast).to receive(:id).and_return(123)
allow(asciicast).to receive(:id).and_return(123)
end
it 'is an async script tag including asciicast id' do
@ -114,7 +130,7 @@ describe AsciicastPresenter do
subject { presenter.show_admin_dropdown? }
before do
allow(decorated_asciicast).to receive(:managable_by?).
allow(asciicast).to receive(:managable_by?).
with(current_user) { managable }
end
@ -135,7 +151,7 @@ describe AsciicastPresenter do
subject { presenter.show_description? }
before do
allow(decorated_asciicast).to receive(:description) { description }
allow(asciicast).to receive(:description) { description }
end
context "when description is present" do
@ -155,7 +171,7 @@ describe AsciicastPresenter do
subject { presenter.description }
before do
allow(decorated_asciicast).to receive(:description) { 'i am description' }
allow(asciicast).to receive(:description) { 'i am description' }
end
it { should eq('i am description') }
@ -189,7 +205,7 @@ describe AsciicastPresenter do
before do
allow(author).to receive(:asciicasts_excluding).
with(decorated_asciicast, 3) { others }
with(asciicast, 3) { others }
end
it "returns decorated asciicasts excluding the given one" do

@ -28,6 +28,18 @@ describe HomePresenter do
end
end
describe '#playback_options' do
subject { presenter.playback_options }
it "has speed set to 2.0" do
expect(subject.speed).to eq(2.0)
end
it "has HUD hidden" do
expect(subject.hide_hud).to be(true)
end
end
describe '#latest_asciicasts' do
subject { presenter.latest_asciicasts }

Loading…
Cancel
Save