Module: ModelAttachment::InstanceMethods

Defined in:
lib/model_attachment.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#basenameObject

:nodoc:



190
191
192
# File 'lib/model_attachment.rb', line 190

def basename #:nodoc:
  File.basename(file_name, extension)
end

#extensionObject

:nodoc:



186
187
188
# File 'lib/model_attachment.rb', line 186

def extension #:nodoc:
  File.extname(file_name)
end

#filename(type = "") ⇒ Object

returns the filename, including any type modifier type: type from has_attachment, ex. small, large



181
182
183
184
# File 'lib/model_attachment.rb', line 181

def filename(type = "")
  type      = "_#{type}" if type != ""
  "#{basename}#{type}#{extension}"
end

#full_filename(type = "") ⇒ Object

returns the full system path/filename type: type from has_attachment, ex. small, large



196
197
198
# File 'lib/model_attachment.rb', line 196

def full_filename(type = "")
  full_path + filename(type)
end

#full_pathObject

returns the full system path of the file



175
176
177
# File 'lib/model_attachment.rb', line 175

def full_path
  File.join(Rails.root, path)
end

#image?Boolean

decide whether or not this is an image

Returns:

  • (Boolean)


201
202
203
# File 'lib/model_attachment.rb', line 201

def image?
  content_type =~ /^image\//
end

#interpolate(path, *args) ⇒ Object

create the path based on the template



211
212
213
214
215
216
217
218
219
# File 'lib/model_attachment.rb', line 211

def interpolate(path, *args)
  #methods = ["domain", "folder", "document", "version", "user", "account"]
  valid_methods.inject( path.dup ) do |result, tag|
    #$stderr.puts("Result: #{result} Tag: #{tag}")
    result.gsub(/:#{tag}/) do |match|
      send( tag, *args )
    end
  end
end

#pathObject

returns the rails path of the file



166
167
168
169
170
171
172
# File 'lib/model_attachment.rb', line 166

def path 
  if (self.class.attachment_options[:path]) 
    return interpolate(self.class.attachment_options[:path])
  else 
    return "/public/system/#{self.class.to_s.downcase.pluralize}/" + sprintf("%04d", id) + "/"
  end
end

#public?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/model_attachment.rb', line 161

def public?
  (path =~ /^\/public/)
end

#url(options = {}) ⇒ Object

return the url based on location

  • proto: set the protocol, defaults to http

  • port: sets the port if required

  • server_name: sets the server name, defaults to localhost

  • path: sets the path, defaults to /documents/send

  • type: sets the type, types come from the has_attachment method, ex. small, large



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
156
157
158
159
# File 'lib/model_attachment.rb', line 129

def url(options = {})
  proto       = options[:proto]        || "http://"
  port        = options[:port]
  server_name = options[:server_name]  || "localhost"
  url_path    = options[:path]         || "/#{self.class.to_s.downcase.pluralize}/"
  type        = options[:type]
  
  server_name += ":" + port.to_s if !port.nil? && port != 80 && port != 443
  type_string = "?type=#{type}" if type
  
  # if we aren't using aws set @bucket to nil
  if !self.class.attachment_options[:aws]
    @bucket = nil
  end
  
  # if files are public, serve public url
  if public?
    url_path = path.gsub(/^\/public(.*)/, '\1')
    type_string = "_#{type}" if type
    url = "#{proto}#{server_name}#{url_path}#{basename}#{type_string}#{extension}"
  elsif bucket.nil?
    # otherwise use private url with deliver
    url = "#{proto}#{server_name}#{url_path}#{id}#{type_string}"
  else
    # if bucket is set, then use aws url
    url = aws_url(type)
  end
  
  log("Providing URL: #{url}")
  return url
end

#valid_methodsObject



205
206
207
208
# File 'lib/model_attachment.rb', line 205

def valid_methods
  # reject any methods that will cause issues
  self.class.instance_methods.sort.reverse.reject {|m| m =~ /\W+/ }
end