Module: ActiveRecord::Acts::UploaderUpload::InstanceMethods

Defined in:
lib/active_record/acts/uploader_upload.rb

Overview

All the methods available to a record that has had acts_as_uploader specified.

Instance Method Summary collapse

Instance Method Details

#calculate_sizes(style) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/active_record/acts/uploader_upload.rb', line 191

def calculate_sizes(style)
  if image_ratio > 1
    @image_width ||= width > max_dimension(style) ? max_dimension(style) : width
    @image_height ||= (@image_width / image_ratio).round
  else
    @image_height ||= height > max_dimension(style) ? max_dimension(style) : height
    @image_width ||= (@image_height * image_ratio).round
  end
  @image_size ||= "#{@image_width.to_i}x#{@image_height.to_i}"
end

#can_edit?(check_user) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/active_record/acts/uploader_upload.rb', line 155

def can_edit?(check_user)
  return false if check_user.blank?
  check_user == self.creator
end

#display_nameObject



151
152
153
# File 'lib/active_record/acts/uploader_upload.rb', line 151

def display_name
  CGI::escapeHTML(self.local_file_name)
end

#fileObject



66
67
68
# File 'lib/active_record/acts/uploader_upload.rb', line 66

def file
  local_file_name ? local : remote
end

#file_nameObject



70
71
72
# File 'lib/active_record/acts/uploader_upload.rb', line 70

def file_name
  remote_file_name || local_file_name
end

#height(style = :default) ⇒ Object



169
170
171
172
173
174
# File 'lib/active_record/acts/uploader_upload.rb', line 169

def height(style = :default)
  return nil unless self[:height]
  return self[:height] if style == :default
  calculate_sizes(style.to_sym)
  return @image_height.to_i
end

#iconObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_record/acts/uploader_upload.rb', line 133

def icon
  if self.is_pdf?
    '/images/file_icons/file_pdf.gif'
  elsif self.is_word?
    '/images/file_icons/file_doc.gif'
  elsif self.is_image?
    self.file.url(:icon)
  elsif self.is_mp3?
    '/images/file_icons/file_mp3.gif'
  elsif self.is_excel?
    '/images/file_icons/file_xls.gif'
  elsif self.is_text?
    '/images/file_icons/file_txt.gif'
  else
    '/images/file_icons/file_raw.gif'
  end
end

#image_ratioObject



187
188
189
# File 'lib/active_record/acts/uploader_upload.rb', line 187

def image_ratio
  @image_ratio ||= width.to_f / height.to_f
end

#is_excel?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/active_record/acts/uploader_upload.rb', line 99

def is_excel?
  Uploader::MimeTypeGroups::EXCEL_TYPES.include?(self.local_content_type)
end

#is_image?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/active_record/acts/uploader_upload.rb', line 91

def is_image?
  Uploader::MimeTypeGroups::IMAGE_TYPES.include?(self.local_content_type)
end

#is_mp3?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/active_record/acts/uploader_upload.rb', line 95

def is_mp3?
  Uploader::MimeTypeGroups::MP3_TYPES.include?(self.local_content_type)
end

#is_pdf?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/active_record/acts/uploader_upload.rb', line 103

def is_pdf?
  Uploader::MimeTypeGroups::PDF_TYPES.include?(self.local_content_type)
end

#is_text?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/active_record/acts/uploader_upload.rb', line 111

def is_text?
  Uploader::MimeTypeGroups::TEXT_TYPES.include?(self.local_content_type)
end

#is_word?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/active_record/acts/uploader_upload.rb', line 107

def is_word?
  Uploader::MimeTypeGroups::WORD_TYPES.include?(self.local_content_type)
end

#max_dimension(style) ⇒ Object



183
184
185
# File 'lib/active_record/acts/uploader_upload.rb', line 183

def max_dimension(style)
  @max_dimension ||= Paperclip::Geometry.parse(self.local.styles[style][:geometry]).width.to_f
end

#send_to_remoteObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_record/acts/uploader_upload.rb', line 74

def send_to_remote
  if local_file_name
    self.remote = local.to_file
    if self.save and remote.original_filename and remote.exists?
      self.local = nil
      self.save
    else
      false
    end
  end
end

#size(style = :default) ⇒ Object



176
177
178
179
180
181
# File 'lib/active_record/acts/uploader_upload.rb', line 176

def size(style = :default)
  return nil unless width || height
  return "#{width}x#{height}" if style == :default
  calculate_sizes(style.to_sym)
  return @image_size
end

#swfupload_local=(filedata) ⇒ Object



86
87
88
89
# File 'lib/active_record/acts/uploader_upload.rb', line 86

def swfupload_local=(filedata)
  filedata.content_type = MIME::Types.type_for(filedata.original_filename)[0].to_s
  self.local = filedata
end

#upload_typeObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_record/acts/uploader_upload.rb', line 115

def upload_type
  if self.is_pdf?
    'Adobe pdf file'
  elsif self.is_word?
    'Word document'
  elsif self.is_image?
    'photo'
  elsif self.is_mp3?
    'mp3'
  elsif self.is_excel?
    'Excel document'
  elsif self.is_text?
    'text file'
  else
    'file'
  end
end

#width(style = :default) ⇒ Object

Image dimension calculations



162
163
164
165
166
167
# File 'lib/active_record/acts/uploader_upload.rb', line 162

def width(style = :default)
  return nil unless self[:width]
  return self[:width] if style == :default
  calculate_sizes(style.to_sym)
  return @image_width.to_i
end