configurable album thumbnails

master
Coleman 2008-07-03 11:37:33 -05:00
parent 56faf0d1c0
commit 94c0eed279
6 changed files with 42 additions and 6 deletions

View File

@ -88,4 +88,19 @@ class Photos < Application
end
alias_method :thumbnail, :send_rmagicked_image
alias_method :screen, :send_rmagicked_image
def set_album_thumbnail
only_provides :html
@photo = Photo.find(params[:id])
raise NotFound unless @photo
@album = @photo.album
raise NotFound unless @album
if @album.update_attribute(:album_thumbnail_id, @photo.id)
flash[:notice] = 'Very nice!'
else
flash[:error] = 'Could not update the album:<br />'
@album.errors.each_full { |msg| flash[:error] += "#{msg}<br />" }
end
redirect url(:photo, @photo)
end
end

View File

@ -5,6 +5,7 @@ class Album < ActiveRecord::Base
has_and_belongs_to_many :tags, :order => 'tags.name ASC'
has_many :photos
after_create :save_tags
belongs_to :album_thumbnail, :class_name => 'Photo'
def tag_names
self.tags.collect { |t| t.name }.join(' ')
@ -22,10 +23,6 @@ class Album < ActiveRecord::Base
self.tags = new_tags
end
def album_thumbnail
self.photos.first
end
def self.for_select
self.find(:all, :select => 'name', :order => 'name ASC').collect do |a|
a.name

View File

@ -229,6 +229,7 @@ function toggle_photo_tag_editor(direction)
<% throw_content :for_sidebar do -%>
<a href="<%= url(:album, :id => @photo.album.name.gsub(/ /, '_')) -%>"><img src="/images/camera-photo.png" /> Back to <%= @photo.album.name -%></a><br />
<% if allowed_to?(:edit_album, @photo.album) and @photo.album.album_thumbnail_id != @photo.id -%><a href="<%= url(:action => :set_album_thumbnail, :id => @photo.id) -%>" rel="nofollow"><img src="/images/emblem-photos.png" /> Make album thumbnail</a><br /><% end -%>
<% if allowed_to?(:edit_photo, @photo) and @photo.exist? -%><a href="<%= url(:edit_photo, :id => @photo.id) -%>" rel="nofollow"><img src="/images/document-save.png" /> Edit photo</a><br /><% end %>
<% if allowed_to?(:delete_photo, @photo) -%><a href="<%= url(:delete_photo, :id => @photo.id) -%>" onclick="if(!confirm('Are you sure you want to delete this photo?')){return false;}" rel="nofollow"><img src="/images/edit-delete.png" /> Destroy photo</a><br /><% end -%>
<% if @photo.exist? -%><a href="<%= photo_url(@photo) -%>"><img src="/images/image-x-generic.png" /> Download original</a><br /><% end -%>

View File

@ -34,6 +34,10 @@ Merb::Router.prepare do |r|
:controller => 'photos',
:action => 'screen'
)
r.match('/photos/set_album_thumbnail/:id').to(
:controller => 'photos',
:action => 'set_album_thumbnail'
)
r.resources :photo_tags
r.match('/').to(
:controller => 'news',

View File

@ -0,0 +1,13 @@
class AlbumThumbnailMigration < ActiveRecord::Migration
def self.up
add_column :albums, :album_thumbnail_id, :integer
add_index :albums, :album_thumbnail_id
Album.find(:all).each do |album|
album.update_attribute :album_thumbnail_id, (album.photos.first.id rescue nil)
end
end
def self.down
remove_column :albums, :album_thumbnail_id
end
end

View File

@ -9,13 +9,15 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 11) do
ActiveRecord::Schema.define(:version => 12) do
create_table "albums", :force => true do |t|
t.string "name", :limit => 128
t.string "name", :limit => 128
t.integer "album_thumbnail_id", :limit => 11
end
add_index "albums", ["name"], :name => "index_albums_on_name"
add_index "albums", ["album_thumbnail_id"], :name => "index_albums_on_album_thumbnail_id"
create_table "albums_tags", :id => false, :force => true do |t|
t.integer "album_id", :limit => 11
@ -112,6 +114,10 @@ ActiveRecord::Schema.define(:version => 11) do
add_index "photos", ["author_id"], :name => "index_photos_on_author_id"
add_index "photos", ["album_id"], :name => "index_photos_on_album_id"
create_table "schema_info", :id => false, :force => true do |t|
t.integer "version", :limit => 11
end
create_table "sessions", :force => true do |t|
t.string "session_id"
t.text "data"