From 94c0eed279fd00962b091155aa3b05359ba2d5eb Mon Sep 17 00:00:00 2001 From: Coleman Date: Thu, 3 Jul 2008 11:37:33 -0500 Subject: [PATCH] configurable album thumbnails --- app/controllers/photos.rb | 15 +++++++++++++++ app/models/album.rb | 5 +---- app/views/photos/show.html.erb | 1 + config/router.rb | 4 ++++ .../migrations/012_album_thumbnail_migration.rb | 13 +++++++++++++ schema/schema.rb | 10 ++++++++-- 6 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 schema/migrations/012_album_thumbnail_migration.rb diff --git a/app/controllers/photos.rb b/app/controllers/photos.rb index d903f87..f32f2a5 100644 --- a/app/controllers/photos.rb +++ b/app/controllers/photos.rb @@ -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:
' + @album.errors.each_full { |msg| flash[:error] += "#{msg}
" } + end + redirect url(:photo, @photo) + end end diff --git a/app/models/album.rb b/app/models/album.rb index 385f9a6..cff7338 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -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 diff --git a/app/views/photos/show.html.erb b/app/views/photos/show.html.erb index 7ce0815..59d2955 100644 --- a/app/views/photos/show.html.erb +++ b/app/views/photos/show.html.erb @@ -229,6 +229,7 @@ function toggle_photo_tag_editor(direction) <% throw_content :for_sidebar do -%> Back to <%= @photo.album.name -%>
+ <% if allowed_to?(:edit_album, @photo.album) and @photo.album.album_thumbnail_id != @photo.id -%> Make album thumbnail
<% end -%> <% if allowed_to?(:edit_photo, @photo) and @photo.exist? -%> Edit photo
<% end %> <% if allowed_to?(:delete_photo, @photo) -%> Destroy photo
<% end -%> <% if @photo.exist? -%> Download original
<% end -%> diff --git a/config/router.rb b/config/router.rb index 0e616ad..3ccd353 100644 --- a/config/router.rb +++ b/config/router.rb @@ -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', diff --git a/schema/migrations/012_album_thumbnail_migration.rb b/schema/migrations/012_album_thumbnail_migration.rb new file mode 100644 index 0000000..14ff4db --- /dev/null +++ b/schema/migrations/012_album_thumbnail_migration.rb @@ -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 diff --git a/schema/schema.rb b/schema/schema.rb index 24349aa..5cd5ac5 100644 --- a/schema/schema.rb +++ b/schema/schema.rb @@ -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"