Class: Upload

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/models/upload.rb

Overview

Schema Information

Table name: uploads

id                 :integer          not null, primary key
source             :string(255)
rights             :string(255)
description        :text
upload_file_name    :string(255)
upload_content_type :string(255)
upload_file_size    :integer
created_at         :datetime         not null
updated_at         :datetime         not null
attachable_id      :integer
attachable_type    :string(255)
alt_text           :string(255)
sorter_number      :integer

Constant Summary collapse

REGEXP =
case ActiveRecord::Base.connection.adapter_name.downcase.to_sym
  when :postgresql
    "~"
  else
    "REGEXP"
end

Instance Method Summary collapse

Instance Method Details

#complete_list_nameObject



84
85
86
87
88
89
# File 'app/models/upload.rb', line 84

def complete_list_name
  result = ""
  result << "#{self.upload_file_name} " if self.upload_file_name.present?
  result << "(#{self.source}, #{self.rights}) " if self.source.present? || self.rights.present?
  result << "- #{self.created_at}"
end

#image_file?Boolean

Internal: Makes sure to only post-process files which are either pdf or image files. Word documents would break file upload.

No params

Returns true for image or pdf files and false for everything else

Returns:

  • (Boolean)


117
118
119
# File 'app/models/upload.rb', line 117

def image_file?
  !(self.upload_content_type =~ /^image.*/).nil? || !(self.upload_content_type =~ /pdf/).nil?
end

#titleObject



78
79
80
# File 'app/models/upload.rb', line 78

def title
  "#{self.upload_file_name} (#{self.upload_content_type})"
end

#to_jq_uploadObject

include I18n::Backend::Fallbacks include I18n::Backend::Simple



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/upload.rb', line 127

def to_jq_upload
  {
    "id"            => read_attribute(:id),
    "name"          => read_attribute(:upload_file_name),
    "content_type"  => read_attribute(:upload_content_type),
    "size"          => read_attribute(:upload_file_size),
    "updated_at"    => read_attribute(:updated_at),
    "created_at"    => read_attribute(:created_at),
    "images" =>  {
      "original"    => upload.url,
      "large"       => upload.url(:large  ),
      "big"         => upload.url(:big    ),
      "medium"      => upload.url(:medium ),
      "thumb"       => upload.url(:thumb  ),
      "mini"        => upload.url(:mini   ),
      # Logo
      "logo"        => upload.url(:logo   )
    },
    "url"           => upload.url(:original),
    "thumb"         => upload.url(:thumb),
    "delete_url"    => admin_upload_path(self),
    "delete_type"   => "DELETE",
    "i18n" => {
      "size"        => number_to_human_size(read_attribute(:upload_file_size)),
      "updated_at"  => I18n.l(read_attribute(:updated_at), format: :long),
      "created_at"  => I18n.l(read_attribute(:created_at), format: :long),
    }
  }
end

#unzip_filesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/upload.rb', line 91

def unzip_files
  if self.upload_file_name.include?(".zip") && File.exists?(self.image.path)
    require 'zip/zip'
    zipped_files = Zip::ZipFile.open(self.image.path)
    int = 0
    zipped_files.each do |zipped_file|
      int = int + 1
      if zipped_file.file?
        zipped_file.extract("tmp/#{self.id}_unzipped_#{int}.jpg")
        Upload.create(:image => File.open("tmp/#{self.id}_unzipped_#{int}.jpg"),
                    :source => self.source,
                    :rights => self.rights,
                    :description => self.description,
                    :tag_list => self.tag_list.join(", ") )
        File.delete("tmp/#{self.id}_unzipped_#{int}.jpg")
      end
    end
  end
end