diff --git a/app/controllers/direct_uploads_controller.rb b/app/controllers/direct_uploads_controller.rb index ccca2fb13..8fddf8b0c 100644 --- a/app/controllers/direct_uploads_controller.rb +++ b/app/controllers/direct_uploads_controller.rb @@ -8,13 +8,13 @@ class DirectUploadsController < ApplicationController helper_method :render_destroy_upload_link - # It should return cached attachment path or attachment errors def create @direct_upload = DirectUpload.new(direct_upload_params.merge(user: current_user, attachment: params[:attachment])) if @direct_upload.valid? @direct_upload.save_attachment @direct_upload.relation.set_cached_attachment_from_attachment(URI(request.url)) + render json: { cached_attachment: @direct_upload.relation.cached_attachment, filename: @direct_upload.relation.attachment.original_filename, destroy_link: render_destroy_upload_link(@direct_upload).html_safe, @@ -29,7 +29,6 @@ class DirectUploadsController < ApplicationController def destroy @direct_upload = DirectUpload.new(direct_upload_params.merge(user: current_user) ) - @direct_upload.relation.set_attachment_from_cached_attachment if @direct_upload.destroy_attachment diff --git a/app/models/direct_upload.rb b/app/models/direct_upload.rb index bc7c135ce..a6b6ef276 100644 --- a/app/models/direct_upload.rb +++ b/app/models/direct_upload.rb @@ -7,9 +7,27 @@ class DirectUpload :relation, :resource_relation, :attachment, :cached_attachment, :user - validates_presence_of :attachment, :resource_type, :resource_relation + validates_presence_of :attachment, :resource_type, :resource_relation, :user validate :parent_resource_attachment_validations, - if: -> { attachment.present? && resource_type.present? && resource_relation.present? } + if: -> { attachment.present? && resource_type.present? && resource_relation.present? && user.present? } + + def initialize(attributes = {}) + attributes.each do |name, value| + send("#{name}=", value) + end + + if @resource_type.present? && @resource_relation.present? && (@attachment.present? || @cached_attachment.present?) + @resource = @resource_type.constantize.find_or_initialize_by(id: @resource_id) + + if @resource.class.reflections[@resource_relation].macro == :has_one + @relation = @resource.send("build_#{resource_relation}", relation_attributtes) + else + @relation = @resource.send(@resource_relation).build(relation_attributtes) + end + + @relation.user = user + end + end def save_attachment @relation.attachment.save @@ -23,24 +41,6 @@ class DirectUpload false end - def initialize(attributes = {}) - attributes.each do |name, value| - send("#{name}=", value) - end - - if @resource_type.present? - @resource = @resource_type.constantize.find_or_initialize_by(id: @resource_id) - end - - if @resource.class.reflections[@resource_relation].macro == :has_one - @relation = @resource.send("build_#{resource_relation}", attachment: @attachment, cached_attachment: @cached_attachment) - else - @relation = @resource.send(resource_relation).build(attachment: @attachment, cached_attachment: @cached_attachment) - end - - @relation.user = user - end - private def parent_resource_attachment_validations @@ -51,4 +51,12 @@ class DirectUpload end end + def relation_attributtes + { + attachment: @attachment, + cached_attachment: @cached_attachment, + user: @user + } + end + end \ No newline at end of file diff --git a/app/models/document.rb b/app/models/document.rb index a04313e50..0bd41d673 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -20,8 +20,6 @@ class Document < ActiveRecord::Base validates :documentable_id, presence: true, if: -> { persisted? } validates :documentable_type, presence: true, if: -> { persisted? } - after_save :remove_cached_document, if: -> { valid? && persisted? && cached_attachment.present? } - def set_cached_attachment_from_attachment(prefix) self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem attachment.path @@ -42,7 +40,7 @@ class Document < ActiveRecord::Base attachment.instance.prefix(attachment, style) end - def prefix(attachment, _style) + def prefix(attachment, style) if !attachment.instance.persisted? "cached_attachments/user/#{attachment.instance.user_id}" else @@ -80,8 +78,4 @@ class Document < ActiveRecord::Base end end - def remove_cached_document - File.delete(cached_attachment) if File.exist?(cached_attachment) - end - end diff --git a/app/models/image.rb b/app/models/image.rb index 201ec571d..358453385 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -29,9 +29,6 @@ class Image < ActiveRecord::Base validate :validate_image_dimensions, if: -> { attachment.present? && attachment.dirty? } - after_create :redimension_using_origin_styles - after_save :remove_cached_image, if: -> { valid? && persisted? && cached_attachment.present? } - def set_cached_attachment_from_attachment(prefix) self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem attachment.path @@ -60,13 +57,8 @@ class Image < ActiveRecord::Base end end - private - def redimension_using_origin_styles - attachment.reprocess! - end - def imageable_class imageable_type.constantize if imageable_type.present? end @@ -102,10 +94,6 @@ class Image < ActiveRecord::Base end end - def remove_cached_image - File.delete(cached_attachment) if File.exists?(cached_attachment) - end - def attachment_of_valid_content_type? attachment.present? && imageable_accepted_content_types.include?(attachment_content_type) end diff --git a/spec/factories.rb b/spec/factories.rb index a53389fb9..160b81664 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -820,7 +820,8 @@ LOREM_IPSUM end factory :direct_upload do - + user + trait :proposal do resource_type "Proposal" end @@ -836,6 +837,7 @@ LOREM_IPSUM resource_relation "image" attachment { File.new("spec/fixtures/files/clippy.jpg") } end + initialize_with { new(attributes) } end end diff --git a/spec/models/direct_upload_spec.rb b/spec/models/direct_upload_spec.rb index b1bc9a3a9..2663bd5b1 100644 --- a/spec/models/direct_upload_spec.rb +++ b/spec/models/direct_upload_spec.rb @@ -2,18 +2,54 @@ require 'rails_helper' describe DirectUpload do - it "should be valid for different kind of combinations when attachment is valid" do - expect(build(:direct_upload, :proposal, :documents)).to be_valid - expect(build(:direct_upload, :proposal, :image)).to be_valid - expect(build(:direct_upload, :budget_investment, :documents)).to be_valid - expect(build(:direct_upload, :budget_investment, :image)).to be_valid + context "configurations" do + + it "should be valid for different kind of combinations when attachment is valid" do + expect(build(:direct_upload, :proposal, :documents)).to be_valid + expect(build(:direct_upload, :proposal, :image)).to be_valid + expect(build(:direct_upload, :budget_investment, :documents)).to be_valid + expect(build(:direct_upload, :budget_investment, :image)).to be_valid + end + + it "should not be valid for different kind of combinations with invalid atttachment content types" do + expect(build(:direct_upload, :proposal, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid + expect(build(:direct_upload, :proposal, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid + expect(build(:direct_upload, :budget_investment, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid + expect(build(:direct_upload, :budget_investment, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid + end + + it "should not be valid without resource_type" do + expect(build(:direct_upload, :proposal, :documents, resource_type: nil)).not_to be_valid + end + + it "should not be valid without resource_relation" do + expect(build(:direct_upload, :proposal, :documents, resource_relation: nil)).not_to be_valid + end + + it "should not be valid without attachment" do + expect(build(:direct_upload, :proposal, :documents, attachment: nil)).not_to be_valid + end + + it "should not be valid without user" do + expect(build(:direct_upload, :proposal, :documents, user: nil)).not_to be_valid + end end - it "should not be valid for different kind of combinations when invalid atttachment content types" do - expect(build(:direct_upload, :proposal, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid - expect(build(:direct_upload, :proposal, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid - expect(build(:direct_upload, :budget_investment, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid - expect(build(:direct_upload, :budget_investment, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid + context "save_attachment" do + it "should create all uploaded file versions" do + proposal_document_direct_upload = build(:direct_upload, :proposal, :documents) + + expect{ proposal_document_direct_upload.save_attachment }.to eq(true) + end + end + context "destroy_attachment" do + it "should create all uploaded file versions" do + + end + + end + + end \ No newline at end of file