From 4464c22d6d23d893494682d309aec3fb31c11ae3 Mon Sep 17 00:00:00 2001 From: Jarka Kadlecova Date: Wed, 3 May 2017 17:26:49 +0200 Subject: Support descriptions for snippets --- app/uploaders/file_mover.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/uploaders/file_mover.rb (limited to 'app/uploaders/file_mover.rb') diff --git a/app/uploaders/file_mover.rb b/app/uploaders/file_mover.rb new file mode 100644 index 00000000000..21e37a08a82 --- /dev/null +++ b/app/uploaders/file_mover.rb @@ -0,0 +1,48 @@ +class FileMover + attr_reader :secret, :file_name, :model + + def initialize(file_path, model, update_field = :description) + @secret = File.split(File.dirname(file_path)).last + @file_name = File.basename(file_path) + @model = model + end + + def execute + move + update_markdown + end + + private + + def move + FileUtils.mkdir_p(file_path) + FileUtils.move(temp_file_path, file_path) + end + + def update_markdown(field = :description) + updated_text = model.send(field).sub(temp_file_uploader.to_markdown, uploader.to_markdown) + model.update_attribute(field, updated_text) + end + + def temp_file_path + temp_file_uploader.retrieve_from_store!(file_name) + + temp_file_uploader.file.path + end + + def file_path + return @file_path if @file_path + + uploader.retrieve_from_store!(file_name) + + @file_path = uploader.file.path + end + + def uploader + @uploader ||= PersonalFileUploader.new(model, secret) + end + + def temp_file_uploader + @temp_file_uploader ||= PersonalFileUploader.new(nil, secret) + end +end -- cgit v1.2.1