Sha256: eed8cfdad48ad814fe612b4ce065082012ed2cdd7713e7caeef9f793df089776

Contents?: true

Size: 1.25 KB

Versions: 15

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "renalware/feeds"

module Renalware
  module Feeds
    module Files
      class CreateFeedFile
        include Virtus.model
        attribute :uploaded_file, Pathname
        attribute :file_type, Renalware::Feeds::FileType
        attribute :user, Renalware::User

        def self.call(*args)
          new(*args).call
        end

        def call
          copy_uploaded_file_to_uploads_dir
          create_feed_file
        end

        private

        def copy_uploaded_file_to_uploads_dir
          FileUtils.cp(uploaded_file, new_filepath)
        end

        def new_filepath
          @new_filename ||= begin
            name = "#{SecureRandom.uuid}_#{uploaded_file.basename}".truncate(254)
            ::File.join(uploads_dir, name)
          end
        end

        def uploads_dir
          Rails.root.join("tmp/uploads").tap do |uploads_path|
            warn "using upload path #{uploads_path}"
            Dir.mkdir(uploads_path) unless Dir.exist?(uploads_path)
          end
        end

        def create_feed_file
          Renalware::Feeds::File.build(
            location: new_filepath,
            file_type: file_type,
            user: user
          ).tap(&:save!)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.1.0 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.167 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.166 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.165 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.164 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.163 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.162 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.161 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.160 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.159 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.158 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.157 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.156 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.155 app/models/renalware/feeds/files/create_feed_file.rb