Class: PlateVolume

Inherits:
ApplicationRecord show all
Extended by:
DbFile::Uploader
Defined in:
app/models/plate_volume.rb

Constant Summary collapse

ASSAY_TYPE =
'Volume Check'
ASSAY_VERSION =
'1.0'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DbFile::Uploader

extended, has_uploaded

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Class Method Details

.all_plate_volume_file_names(folder) ⇒ Object



81
82
83
# File 'app/models/plate_volume.rb', line 81

def all_plate_volume_file_names(folder)
  Dir.entries(folder).reject { |f| File.directory?(File.join(folder, f)) }
end

.bugfix_filename_duplicate_back_to_normal(filename) ⇒ Object

Given a .csv filename it removes the characters (2) that were appended to indicate the file was a duplicate. This is currently happening to files handled by CarrierWave during the save() action.

An example of this method, suppose file1.csv –> file1(2).csv then the action of this method would revert file1(2).csv into file1.csv



130
131
132
133
134
# File 'app/models/plate_volume.rb', line 130

def bugfix_filename_duplicate_back_to_normal(filename)
  matching_regexp = /\(\d*\)\.CSV/i
  filename.gsub!(matching_regexp, '.csv') if filename.match(matching_regexp)
  filename
end

.find_for_filename(filename) ⇒ Object

rubocop:disable Metrics/MethodLength



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/plate_volume.rb', line 101

def find_for_filename(filename)
  find_by(uploaded_file_name: filename) or
    lambda do |filename, file|
      # TODO: After saving, the uploaded_file_name is renamed internally by CarrierWave to (2).CSV
      # This should be amended in future.

      instance = PlateVolume.new(uploaded_file_name: filename, updated_at: file.stat.mtime, uploaded: file)
      instance.save
    ensure
      unless instance.nil?
        ActiveRecord::Base.connection.execute(
          "
              UPDATE plate_volumes
              SET uploaded_file_name='#{bugfix_filename_duplicate_back_to_normal(instance.uploaded_file_name)}'
              WHERE plate_volumes.id=#{instance.id}
            "
        )
      end
    end
end

.handle_volume(filename, file) ⇒ Object



86
87
88
89
90
# File 'app/models/plate_volume.rb', line 86

def handle_volume(filename, file)
  ActiveRecord::Base.transaction { find_for_filename(sanitized_filename(file)).call(filename, file) }
rescue => e
  Rails.logger.warn("Error processing volume file #{filename}: #{e.message}")
end

.process_all_volume_check_files(folder = configatron.plate_volume_files) ⇒ Object



75
76
77
78
79
# File 'app/models/plate_volume.rb', line 75

def process_all_volume_check_files(folder = configatron.plate_volume_files)
  all_plate_volume_file_names(folder).each do |filename|
    File.open(File.join(folder, filename), 'r') { |file| catch(:no_source_plate) { handle_volume(filename, file) } }
  end
end

.sanitized_filename(file) ⇒ Object



94
95
96
97
98
# File 'app/models/plate_volume.rb', line 94

def sanitized_filename(file)
  # We need to use the Carrierwave sanitized filename for lookup, else files with spaces are repetedly processed
  # Later versions of carrierwave expose this sanitization better, but for now we are forced to create an object
  CarrierWave::SanitizedFile.new(file).filename
end

Instance Method Details

#call(filename, file) ⇒ Object



19
20
21
22
23
24
25
# File 'app/models/plate_volume.rb', line 19

def call(filename, file)
  return unless update_required?(file.stat.mtime)

  db_files.map(&:destroy)
  reload
  update!(uploaded_file_name: filename, updated_at: file.stat.mtime, uploaded: file)
end

#update_required?(modified_timestamp) ⇒ Boolean

Is an update required given the timestamp specified

Returns:

  • (Boolean)


15
16
17
# File 'app/models/plate_volume.rb', line 15

def update_required?(modified_timestamp)
  updated_at.to_i < modified_timestamp.to_i
end