Add ability to set custom thumbnail frame time

private-asciicasts
Marcin Kulik 10 years ago
parent 8d496bd418
commit c9ccc1b066

@ -40,7 +40,7 @@ class AsciicastsController < ApplicationController
def update
authorize asciicast
if asciicast.update_attributes(update_params)
if asciicast_updater.update(asciicast, update_params)
redirect_to asciicast_path(asciicast),
:notice => 'Asciicast was updated.'
else
@ -75,4 +75,8 @@ class AsciicastsController < ApplicationController
params.require(:asciicast).permit(*policy(asciicast).permitted_attributes)
end
def asciicast_updater
AsciicastUpdater.new
end
end

@ -12,6 +12,7 @@ class Asciicast < ActiveRecord::Base
validates :stdout_data, :stdout_timing, :presence => true
validates :terminal_columns, :terminal_lines, :duration, :presence => true
validates :snapshot_at, numericality: { greater_than: 0, allow_blank: true }
belongs_to :user
has_many :comments, -> { order(:created_at) }, :dependent => :destroy

@ -8,7 +8,7 @@ class AsciicastPolicy < ApplicationPolicy
def permitted_attributes
if user.admin? || record.user == user
attrs = [:title, :description, :theme_name]
attrs = [:title, :description, :theme_name, :snapshot_at]
attrs << :featured if user.admin?
attrs

@ -0,0 +1,18 @@
class AsciicastUpdater
def update(asciicast, attributes)
asciicast.attributes = attributes
need_snapshot_update = asciicast.snapshot_at_changed?
if asciicast.save
if need_snapshot_update
AsciicastSnapshotUpdater.new.update(asciicast)
end
true
else
false
end
end
end

@ -7,6 +7,7 @@
= f.input :title
= f.input :description, as: :text, input_html: { rows: 10 }, hint: 'Parsed with <a href="http://github.github.com/github-flavored-markdown/" target="_blank">GitHub Flavored Markdown</a>'.html_safe
= f.input :theme_name, label: 'Terminal theme', collection: themes_for_select, include_blank: default_asciicast_theme_label(decorated_current_user.theme)
= f.input :snapshot_at, label: 'Thumbnail frame', hint: 'Time of frame to be displayed as snapshot/thumbnail (float, in seconds, for example: 10.5)'
= f.buttons do
= f.button :submit, 'Save', class: 'btn-primary'
= link_to 'Cancel', asciicast_path, class: 'btn'

@ -110,7 +110,10 @@ describe AsciicastsController do
put :update, id: asciicast.id, asciicast: { title: 'title'}
}
let(:asciicast_updater) { double(:asciicast_updater) }
before do
allow(controller).to receive(:asciicast_updater) { asciicast_updater }
expect(Asciicast).to receive(:find).and_return(asciicast)
asciicast.user = user
end
@ -122,7 +125,7 @@ describe AsciicastsController do
context 'when update succeeds' do
before do
expect(asciicast).to receive(:update_attributes).and_return(true)
expect(asciicast_updater).to receive(:update).and_return(true)
make_request
end
@ -132,7 +135,7 @@ describe AsciicastsController do
context 'when update fails' do
before do
expect(asciicast).to receive(:update_attributes).and_return(false)
expect(asciicast_updater).to receive(:update).and_return(false)
make_request
end

@ -12,8 +12,8 @@ describe AsciicastPolicy do
context "when user is admin" do
let(:user) { stub_model(User, admin?: true) }
it "includes featured" do
expect(subject).to eq([:title, :description, :theme_name, :featured])
it "includes form fields + featured" do
expect(subject).to eq([:title, :description, :theme_name, :snapshot_at, :featured])
end
end
@ -27,8 +27,8 @@ describe AsciicastPolicy do
context "and is creator of the asciicast" do
let(:asciicast) { Asciicast.new(user: user) }
it "doesn't include featured" do
expect(subject).to eq([:title, :description, :theme_name])
it "includes form field, but no featured" do
expect(subject).to eq([:title, :description, :theme_name, :snapshot_at])
end
end
end

Loading…
Cancel
Save