Skip to content

Commit

Permalink
Better loading of albums
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesu committed Apr 23, 2011
1 parent 7d0c067 commit a6440d3
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions app/controllers/albums_controller.rb
Expand Up @@ -25,6 +25,7 @@

class AlbumsController < ApplicationController
before_filter :grab_page
before_filter :load_album, :except => [:index, :new, :create]

cache_sweeper :page_sweeper, :only => [:create, :update, :destroy, :transfer, :reorder]

Expand All @@ -42,8 +43,6 @@ def index
# GET /albums/1
# GET /albums/1.xml
def show
@album = @page.albums.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.js
Expand All @@ -66,7 +65,6 @@ def new

# GET /albums/1/edit
def edit
@album = @page.albums.find(params[:id])
return error_status(true, :cannot_edit_album) unless (@album.can_be_edited_by(@logged_user))
end

Expand Down Expand Up @@ -106,7 +104,6 @@ def create
# PUT /albums/1
# PUT /albums/1.xml
def update
@album = @page.albums.find(params[:id])
return error_status(true, :cannot_edit_album) unless (@album.can_be_edited_by(@logged_user))

@album.updated_by = @logged_user
Expand All @@ -128,7 +125,6 @@ def update
# DELETE /albums/1
# DELETE /albums/1.xml
def destroy
@album = @page.albums.find(params[:id])
return error_status(true, :cannot_delete_album) unless (@album.can_be_deleted_by(@logged_user))

@slot_id = @album.page_slot.id
Expand All @@ -145,7 +141,6 @@ def destroy

# PUT /albums/1/transfer
def transfer
@album = @page.albums.find(params[:id])
@item = AlbumPicture.find(params[:picture][:id])

return error_status(true, :insufficient_permissions) unless (@album.can_be_edited_by(@logged_user) and @item.can_be_edited_by(@logged_user))
Expand All @@ -162,16 +157,15 @@ def transfer

# POST /albums/1/reorder
def reorder
album = @page.albums.find(params[:id])
return error_status(true, :cannot_edit_album) unless (album.can_be_edited_by(@logged_user))
return error_status(true, :cannot_edit_album) unless (@album.can_be_edited_by(@logged_user))

order = params[:pictures].collect { |id| id.to_i }

album.album_items.each do |item|
idx = order.index(item.id)
item.position = idx
item.position ||= album.album_items.length
item.save!
@album.album_items.each do |item|
idx = order.index(item.id)
item.position = idx
item.position ||= @album.album_items.length
item.save!
end

respond_to do |format|
Expand All @@ -181,4 +175,15 @@ def reorder
end
end

protected

def load_album
begin
@album = @page.albums.find(params[:id])
rescue ActiveRecord::RecordNotFound
error_status(true, :cannot_find_album)
return false
end
end

end

0 comments on commit a6440d3

Please sign in to comment.