Sha256: c30bf010eeec4bb720ab4ca019a643ddb02b09777a0e4c7a67f7e0a77c6107e4

Contents?: true

Size: 1.22 KB

Versions: 17

Compression:

Stored size: 1.22 KB

Contents

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

17 entries across 17 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.7 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.5 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.4 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.3 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.2 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.1 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/feeds/files/create_feed_file.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/feeds/files/create_feed_file.rb