public
Description: Organisation tool written in Rails. Inspired by Backpack
Homepage: http://jamesu.github.com/rucksack
Clone URL: git://github.com/jamesu/rucksack.git
rucksack / app / controllers / album_pictures_controller.rb
100644 163 lines (137 sloc) 5.392 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
160
161
162
163
#==
# Copyright (C) 2008 James S Urquhart
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#++
 
class AlbumPicturesController < ApplicationController
  before_filter :grab_page
  before_filter :grab_album
  
  cache_sweeper :page_sweeper, :only => [:create, :update, :destroy]
  
  # GET /album_pictures
  # GET /album_pictures.xml
  def index
    @album_pictures = @album.pictures.find(:all)
 
    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @album_pictures }
    end
  end
 
  # GET /album_pictures/1
  # GET /album_pictures/1.xml
  def show
    @album_picture = @album.pictures.find(params[:id])
    @new_picture = !params[:is_new].nil?
    
    if !@new_picture
      el_id = @album.pictures.find(:first,
                                   :conditions => ['position < ?', @album_picture.position],
                                   :select => 'id',
                                   :order => 'position DESC')
      @insert_element = "album_picture_#{el_id}" unless el_id.nil?
    
    elsif params[:el_id]
      @insert_element = params[:el_id]
    end
    
    @insert_element ||= 'album_picture_form'
 
    respond_to do |format|
      format.html # show.html.erb
      format.js
      format.xml { render :xml => @album_picture }
    end
  end
 
  # GET /album_pictures/new
  # GET /album_pictures/new.xml
  def new
    return error_status(true, :cannot_create_albumpicture) unless (AlbumPicture.can_be_created_by(@logged_user, @album))
    
    @album_picture = @album.pictures.build
    @picture = @album_picture
 
    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @album_picture }
    end
  end
 
  # GET /album_pictures/1/edit
  def edit
    @album_picture = @album.pictures.find(params[:id])
    @picture = @album_picture
    return error_status(true, :cannot_edit_albumpicture) unless (@album_picture.can_be_edited_by(@logged_user))
  end
 
  # POST /album_pictures
  # POST /album_pictures.xml
  def create
    return error_status(true, :cannot_create_albumpicture) unless (AlbumPicture.can_be_created_by(@logged_user, @album))
    
    @album_picture = @album.pictures.build(params[:picture])
    @album_picture.created_by = @logged_user
 
    respond_to do |format|
      if @album_picture.save
        flash[:notice] = 'AlbumPicture was successfully created.'
        format.html { redirect_to(@album.page) }
        format.js { render :action => 'create', :content_type => 'text/html'}
        format.xml { render :xml => @album_picture, :status => :created, :location => page_album_album_picture_path(:page_id => @page.id, :album_id => @album.id, :id => @album_picture.id) }
      else
        format.html { render :action => "new" }
        format.js
        format.xml { render :xml => @album_picture.errors, :status => :unprocessable_entity }
      end
    end
  end
 
  # PUT /album_pictures/1
  # PUT /album_pictures/1.xml
  def update
    @album_picture = @album.pictures.find(params[:id])
    return error_status(true, :cannot_edit_albumpicture) unless (@album_picture.can_be_edited_by(@logged_user))
    
    @album_picture.updated_by = @logged_user
 
    respond_to do |format|
      if @album_picture.update_attributes(params[:picture])
        flash[:notice] = 'AlbumPicture was successfully updated.'
        format.html { redirect_to(@album.page) }
        format.js { render :action => 'update', :content_type => 'text/html'}
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.js
        format.xml { render :xml => @album_picture.errors, :status => :unprocessable_entity }
      end
    end
  end
 
  # DELETE /album_pictures/1
  # DELETE /album_pictures/1.xml
  def destroy
    @album_picture = @album.pictures.find(params[:id])
    return error_status(true, :cannot_edit_albumpicture) unless (@album_picture.can_be_deleted_by(@logged_user))
    
    @album_picture.updated_by = @logged_user
    @album_picture.destroy
 
    respond_to do |format|
      format.html { redirect_to(album_pictures_url) }
      format.js
      format.xml { head :ok }
    end
  end
 
protected
 
  def grab_album
    begin
      @album = @page.albums.find(params[:album_id])
    rescue ActiveRecord::RecordNotFound
      error_status(true, :cannot_find_album)
      return false
    end
    
    true
  end
end