Module: Sufia::UserLocalDirectoryBehavior

Defined in:
lib/sufia/models/user_local_directory_behavior.rb

Overview

To enable local file ingest,

  • Make User model define .directory method that returns a String corresponding to the User’s personal import directory on the server. This can be a simple ActiveRecord attribute on the User model, or it can be something more elaborate.

  • Include this module in your User model, or define a .files() method that behaves the same

  • Set Sufia.config.enable_local_ingest to true

Instance Method Summary collapse

Instance Method Details

#directory_must_existObject

You can use this validator in your User model. Ensures that a string defining the path to the user’s directory has been provided and corresponds to a real directory on the server.

Examples:

validate :directory_must_exist


12
13
14
15
# File 'lib/sufia/models/user_local_directory_behavior.rb', line 12

def directory_must_exist
  return if directory.blank? || File.directory?(directory)
  errors.add(:directory, "must be an existing directory")
end

#filesObject

List the contents of the user’s directory on the server Indicates whether each item is a directory or not.



19
20
21
22
23
24
# File 'lib/sufia/models/user_local_directory_behavior.rb', line 19

def files
  return [] unless directory.present? && File.directory?(directory)
  Dir[File.join(directory, '*')].each_with_object([]) do |val, accum|
    accum << { name: File.basename(val), directory: File.directory?(val) }
  end
end