-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtind_validator.rb
More file actions
63 lines (50 loc) · 2.1 KB
/
Copy pathtind_validator.rb
File metadata and controls
63 lines (50 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class TindValidator < ActiveRecord::Base
extend ActiveModel::Naming
# REQUIRED_PARAMS = %i[directory 980__a 982__a 982__b 540__a 336__a 852__c 902__n].freeze
REQUIRED_PARAMS = %i[980__a 982__a 982__b 540__a 336__a 852__c 902__n].freeze
OPTIONAL_PARAMS = %i[982__p 991__a 5880_a directory].freeze
has_one_attached :input_file
validate :directory_must_exist
validate :directory_has_digital_assets
validates :'980__a', :'982__a', :'982__b', :'336__a', :'540__a', :'852__c', :'902__n', :input_file, presence: true
validate :correct_mime_type
# validates :directory, :'980__a', :'982__a', :'982__b', :'336__a', :'540__a', :'852__c', :'902__n', :input_file, presence: true
# validate :directory_must_exist
# validate :directory_has_digital_assets
def initialize(params)
super()
@params = params
@errors = ActiveModel::Errors.new(self)
end
def read_attribute_for_validation(key)
@params[key]
end
def permitted_params
permitted = {}
REQUIRED_PARAMS.each do |value|
permitted[value] = @params[value]
end
OPTIONAL_PARAMS.each do |value|
permitted[value] = @params[value]
end
permitted
end
private
def correct_mime_type
error_msg = 'Input file must be a CSV or XLSX file'
content_types = %w[text/csv application/vnd.openxmlformats-officedocument.spreadsheetml.sheet]
errors.add(:input_file, error_msg) unless @params[:input_file].content_type.in?(content_types)
end
def directory_has_digital_assets
error_msg = "#{@params[:directory]} has no digital asset files."
unless (Dir.exist? "#{Rails.application.config.tind_data_root_dir}/#{@params[:directory]}") &&
Dir.glob("#{Rails.application.config.tind_data_root_dir}/#{@params[:directory]}/**/*.*").empty?
return
end
errors.add(:directory, error_msg)
end
def directory_must_exist
error_msg = 'Path is invalid. It should be a directory path based on a collection root directory. (e.g. librettos/ucb/incoming)'
errors.add(:directory, error_msg) unless File.directory? "#{Rails.application.config.tind_data_root_dir}/#{@params[:directory]}"
end
end