diff options
author | Jarka Kadlecova <jarka@gitlab.com> | 2017-05-03 17:26:49 +0200 |
---|---|---|
committer | Jarka Kadlecova <jarka@gitlab.com> | 2017-05-31 07:17:03 +0200 |
commit | 4464c22d6d23d893494682d309aec3fb31c11ae3 (patch) | |
tree | cb7490034185f79c88db472650b87afdcdbafa3f /app/uploaders | |
parent | 8039b9c3c6caedc19e0e44d086a007e8975134b7 (diff) | |
download | gitlab-ce-4464c22d6d23d893494682d309aec3fb31c11ae3.tar.gz |
Support descriptions for snippets
Diffstat (limited to 'app/uploaders')
-rw-r--r-- | app/uploaders/file_mover.rb | 48 | ||||
-rw-r--r-- | app/uploaders/personal_file_uploader.rb | 6 |
2 files changed, 53 insertions, 1 deletions
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 diff --git a/app/uploaders/personal_file_uploader.rb b/app/uploaders/personal_file_uploader.rb index 969b0a20d38..7f857765fbf 100644 --- a/app/uploaders/personal_file_uploader.rb +++ b/app/uploaders/personal_file_uploader.rb @@ -10,6 +10,10 @@ class PersonalFileUploader < FileUploader end def self.model_path(model) - File.join("/#{base_dir}", model.class.to_s.underscore, model.id.to_s) + if model + File.join("/#{base_dir}", model.class.to_s.underscore, model.id.to_s) + else + File.join("/#{base_dir}", 'temp') + end end end |