From 8d496bd4188f0c6719e00e7fd3f9c9e4fa0a2001 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Thu, 27 Nov 2014 11:48:41 +0000 Subject: [PATCH] Make snapshot generation aware of custom snapshot time on asciicast --- app/services/asciicast_snapshot_updater.rb | 3 ++- ...127112626_add_snapshot_at_to_asciicasts.rb | 5 ++++ db/schema.rb | 3 ++- .../asciicast_snapshot_updater_spec.rb | 24 ++++++++++++++----- 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20141127112626_add_snapshot_at_to_asciicasts.rb diff --git a/app/services/asciicast_snapshot_updater.rb b/app/services/asciicast_snapshot_updater.rb index 665f07e..d09c657 100644 --- a/app/services/asciicast_snapshot_updater.rb +++ b/app/services/asciicast_snapshot_updater.rb @@ -1,6 +1,7 @@ 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) asciicast.update_attribute(:snapshot, snapshot) end diff --git a/db/migrate/20141127112626_add_snapshot_at_to_asciicasts.rb b/db/migrate/20141127112626_add_snapshot_at_to_asciicasts.rb new file mode 100644 index 0000000..b91088e --- /dev/null +++ b/db/migrate/20141127112626_add_snapshot_at_to_asciicasts.rb @@ -0,0 +1,5 @@ +class AddSnapshotAtToAsciicasts < ActiveRecord::Migration + def change + add_column :asciicasts, :snapshot_at, :float + end +end diff --git a/db/schema.rb b/db/schema.rb index 4130d3d..12476ec 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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 enable_extension "plpgsql" @@ -52,6 +52,7 @@ ActiveRecord::Schema.define(version: 20141005152615) do t.string "stdout_frames" t.string "user_agent" t.string "theme_name" + t.float "snapshot_at" end add_index "asciicasts", ["created_at"], name: "index_asciicasts_on_created_at", using: :btree diff --git a/spec/services/asciicast_snapshot_updater_spec.rb b/spec/services/asciicast_snapshot_updater_spec.rb index c3c2dc7..f85fbeb 100644 --- a/spec/services/asciicast_snapshot_updater_spec.rb +++ b/spec/services/asciicast_snapshot_updater_spec.rb @@ -5,11 +5,13 @@ describe AsciicastSnapshotUpdater do let(:updater) { described_class.new } describe '#update' do - let(:asciicast) { double('asciicast', :duration => 5.0, :stdout => stdout, - :update_attribute => nil) } + let(:asciicast) { double('asciicast', duration: 5.0, stdout: stdout, + update_attribute: nil, + snapshot_at: snapshot_at) } let(:stdout) { double('stdout') } let(:terminal) { double('terminal') } let(:film) { double('film', :snapshot_at => 'foo') } + let(:snapshot_at) { nil } subject { updater.update(asciicast) } @@ -20,15 +22,25 @@ describe AsciicastSnapshotUpdater do subject 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 expect(asciicast).to have_received(:update_attribute). with(:snapshot, 'foo') 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 subject { updater.update(asciicast, 4.3) }