Commit 3620fab
Changed files (3)
app
controllers
models
spec
models
app/controllers/training_sessions_controller.rb
@@ -1,5 +1,3 @@
-require "temporary_storage"
-
class TrainingSessionsController < ApplicationController
def index
@training_sessions = current_user.training_sessions.
@@ -7,25 +5,13 @@ class TrainingSessionsController < ApplicationController
end
def upload
- if legitimate_file?(params[:backup])
- UploadStrongliftsBackupJob.perform_later(
- current_user,
- storage.store(params[:backup]),
- Program.stronglifts
- )
+ backup_file = BackupFile.new(current_user, params[:backup])
+
+ if backup_file.valid?
+ backup_file.process_later(Program.stronglifts)
redirect_to dashboard_path, notice: t(".success")
else
redirect_to dashboard_path, alert: t(".failure")
end
end
-
- private
-
- def storage
- @storage ||= TemporaryStorage.new
- end
-
- def legitimate_file?(file)
- file.original_filename.end_with?(".stronglifts")
- end
end
app/models/backup_file.rb
@@ -0,0 +1,28 @@
+require "temporary_storage"
+
+class BackupFile
+ attr_reader :user, :backup_file
+
+ def initialize(user, backup_file)
+ @user = user
+ @backup_file = backup_file
+ end
+
+ def process_later(program)
+ UploadStrongliftsBackupJob.perform_later(
+ user,
+ storage.store(backup_file),
+ program
+ ) if valid?
+ end
+
+ def valid?
+ backup_file.original_filename.end_with?(".stronglifts")
+ end
+
+ private
+
+ def storage
+ @storage ||= TemporaryStorage.new
+ end
+end
spec/models/backup_file_spec.rb
@@ -0,0 +1,32 @@
+require "rails_helper"
+
+describe BackupFile do
+ let(:user) { build(:user) }
+
+ def fixture_file(name)
+ Rack::Test::UploadedFile.new(Rails.root.join("spec", "fixtures", name))
+ end
+
+ describe "#valid?" do
+ it "returns true" do
+ subject = BackupFile.new(user, fixture_file("backup.android.stronglifts"))
+ expect(subject).to be_valid
+ end
+
+ it "returns false" do
+ subject = BackupFile.new(user, fixture_file("unknown.file"))
+ expect(subject).to_not be_valid
+ end
+ end
+
+ describe "#process_later" do
+ let(:program) { build(:program) }
+
+ it "creates a job to process later" do
+ allow(UploadStrongliftsBackupJob).to receive(:perform_later)
+ subject = BackupFile.new(user, fixture_file("backup.ios.stronglifts"))
+ subject.process_later(program)
+ expect(UploadStrongliftsBackupJob).to have_received(:perform_later)
+ end
+ end
+end