Module: Services::MdsUploadContent::ClassMethods

Defined in:
app/models/services/mds_upload_content.rb

Instance Method Summary collapse

Instance Method Details

#mds_upload_content(attr_name, args = {}) ⇒ Object

Specify that there is a file attachment associated with this model.

attr_name - the name of the parameter in the request that contains the file upload.
args       - a hash of options to control operations. Valid options include:

     :max_size => { :size => size-in-bytes,
                    :message => 'Error message if size exceeded' }

If :size is not specified, a default of 5.megabytes is used If :message is not specified, a default message of “uploaded file exceedes maximum of #{args[:size] bytes” is used

:presence => { :message => 'Error message if upload not provided.' }

If :message is not specified, a default message of “no file upload provided”

:name_match => { :regex => regular expression for file name to match,
                 :message => 'Error message if upload not provided.' }

If :regex is not specified, all names will match. If :message is not specified, a default message of “uploaded file name does not match regular expression”.

:file_type => { :message => 'Error message if no match on file type',
                :regex => regular expression for the results of 'file -b --mime' to match }

If :message is not specified, a default message of “unexpected upload file type” If :regex is not specified, then there will be no check of the file type.



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
# File 'app/models/services/mds_upload_content.rb', line 35

def mds_upload_content(attr_name, args={})
  
  # Define the setter method for the upload file.
  define_method("#{attr_name}=".to_sym) do | upload_file |
    return if upload_file.nil?
    self.original_file_name = File.basename(upload_file.original_filename)
    self.content_type = upload_file.content_type
    self.size = upload_file.size
    @upload_file = upload_file
  end

  define_method("clear_#{attr_name}".to_sym) do
    @upload_file = nil
  end

  define_max_size_validation(attr_name, args[:max_size]) if args.has_key?(:max_size)

  define_presence_validation(attr_name, args[:presence]) if args.has_key?(:presence)

  define_name_match_validation(attr_name, args[:name_match]) if args.has_key?(:name_match)

  define_file_type_validation(attr_name, args[:file_type]) if args.has_key?(:file_type)

  before_create :construct_file_contents
end