Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created June 15, 2010 01:14
Show Gist options
  • Save patrick99e99/438553 to your computer and use it in GitHub Desktop.
Save patrick99e99/438553 to your computer and use it in GitHub Desktop.
accepts_nested_attributes_for :photo
def image_file=(new_image_file)
# "image/jpeg; charset=binary" --> jpeg
server_format = File.mime_type?(new_image_file).match(/^.*\/([^\;]*)/).captures.first.upcase
set_photo(server_format, :image_file => new_image_file)
end
def image_file_url=(new_image_file_url)
return if new_image_file_url.blank?
server_format = Photo.get_server_format(new_image_file_url)
set_photo(server_format, :image_file_url => new_image_file_url)
end
def set_photo(server_format, photo_params)
self.photo.destroy unless self.photo.nil?
public_access = AccessLevel.find_by_name('public')
photo_album = PhotoAlbum.find_or_create_by_name(:name => 'users', :access_level => public_access)
photo_params.merge!(:photo_album_id => photo_album.id,
:server_format => server_format,
:name => full_name,
:description => "#{full_name} User photo",
:access_level => public_access)
self.photo = Photo.create(photo_params)
end
...
<ul class="photo_upload_form">
<%= content_tag :li, photo_display(@contact.photo) unless @contact.photo.nil? %>
<li>
<%= f.label :image_file, 'Upload Image:' %>
<%= f.file_field :image_file, :class => 'input_object'%>
</li>
<li>
<%= f.label :image_file_url, "or URL:" %>
<%= f.text_field :image_file_url, :class => 'input_object' %>
</li>
</ul>
def self.get_server_format(image_filename)
server_format = image_filename.split(".").last.upcase
server_format == "JPG" ? "JPEG" : server_format
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment