Make snapshot generation aware of custom snapshot time on asciicast

This commit is contained in:
Marcin Kulik 2014-11-27 11:48:41 +00:00
parent 1e9423bc89
commit 8d496bd418
4 changed files with 27 additions and 8 deletions

View File

@ -1,6 +1,7 @@
class AsciicastSnapshotUpdater class AsciicastSnapshotUpdater
def update(asciicast, at_seconds = asciicast.duration / 2) def update(asciicast, at_seconds = nil)
at_seconds ||= asciicast.snapshot_at || asciicast.duration / 2
snapshot = generate_snapshot(asciicast, at_seconds) snapshot = generate_snapshot(asciicast, at_seconds)
asciicast.update_attribute(:snapshot, snapshot) asciicast.update_attribute(:snapshot, snapshot)
end end

View File

@ -0,0 +1,5 @@
class AddSnapshotAtToAsciicasts < ActiveRecord::Migration
def change
add_column :asciicasts, :snapshot_at, :float
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141005152615) do ActiveRecord::Schema.define(version: 20141127112626) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -52,6 +52,7 @@ ActiveRecord::Schema.define(version: 20141005152615) do
t.string "stdout_frames" t.string "stdout_frames"
t.string "user_agent" t.string "user_agent"
t.string "theme_name" t.string "theme_name"
t.float "snapshot_at"
end end
add_index "asciicasts", ["created_at"], name: "index_asciicasts_on_created_at", using: :btree add_index "asciicasts", ["created_at"], name: "index_asciicasts_on_created_at", using: :btree

View File

@ -5,11 +5,13 @@ describe AsciicastSnapshotUpdater do
let(:updater) { described_class.new } let(:updater) { described_class.new }
describe '#update' do describe '#update' do
let(:asciicast) { double('asciicast', :duration => 5.0, :stdout => stdout, let(:asciicast) { double('asciicast', duration: 5.0, stdout: stdout,
:update_attribute => nil) } update_attribute: nil,
snapshot_at: snapshot_at) }
let(:stdout) { double('stdout') } let(:stdout) { double('stdout') }
let(:terminal) { double('terminal') } let(:terminal) { double('terminal') }
let(:film) { double('film', :snapshot_at => 'foo') } let(:film) { double('film', :snapshot_at => 'foo') }
let(:snapshot_at) { nil }
subject { updater.update(asciicast) } subject { updater.update(asciicast) }
@ -20,15 +22,25 @@ describe AsciicastSnapshotUpdater do
subject subject
end end
it "generates the snapshot at half of asciicast's duration" do
expect(film).to have_received(:snapshot_at).with(2.5)
end
it "updates asciicast's snapshot to the terminal's snapshot" do it "updates asciicast's snapshot to the terminal's snapshot" do
expect(asciicast).to have_received(:update_attribute). expect(asciicast).to have_received(:update_attribute).
with(:snapshot, 'foo') with(:snapshot, 'foo')
end end
context "when no snapshot time set on asciicast nor custom time given" do
it "generates the snapshot at half of asciicast's duration" do
expect(film).to have_received(:snapshot_at).with(2.5)
end
end
context "when snapshot time set on asciicast" do
let(:snapshot_at) { 2.0 }
it "generates the snapshot at half of asciicast's duration" do
expect(film).to have_received(:snapshot_at).with(2.0)
end
end
context "when snapshot time given" do context "when snapshot time given" do
subject { updater.update(asciicast, 4.3) } subject { updater.update(asciicast, 4.3) }