lib/generators/dust/albums/templates/app/controllers/photos_controller.rb in dust-generators-0.2.3 vs lib/generators/dust/albums/templates/app/controllers/photos_controller.rb in dust-generators-0.2.4
- old
+ new
@@ -1,89 +1,86 @@
-class PhotosController < ApplicationController
+class <%=child_plural_class_name%>Controller < ApplicationController
filter_access_to :index, :new, :create, :destroy
filter_access_to :edit, :update, :attribute_check => true
layout 'cms'
def index
- @photos = Photo.page(params[:search], params[:page])
- @photo_albums = @photos.group_by { |photo| photo.album.title }
+ @<%=child_plural_name%> = <%=child_class_name%>.page(params[:search], params[:page])
+ @<%=child_singular_name%>_<%=parent_plural_name%> = @<%=child_plural_name%>.group_by { |<%=child_singular_name%>| <%=child_singular_name%>.<%=parent_singular_name%>.title }
end
def show
- @photo = Photo.find(params[:id], :include => :album)
- @total_uploads = Photo.find(:all, :conditions => { :album_id => @photo.album.id})
+ @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id], :include => :<%=parent_singular_name%>)
+ @total_uploads = <%=child_class_name%>.find(:all, :conditions => { :<%=parent_singular_name%>_id => @<%=child_singular_name%>.<%=parent_singular_name%>.id})
+ @<%=parent_singular_name%> = @<%=child_singular_name%>.<%=parent_singular_name%>
end
def new
- @photo = Photo.new
+ @<%=child_singular_name%> = <%=child_class_name%>.new
end
def create
newparams = coerce(params)
- @photo = Photo.new(newparams[:photo])
- if @photo.save
- flash[:notice] = "Successfully created photo."
+ @<%=child_singular_name%> = <%=child_class_name%>.new(newparams[:<%=child_singular_name%>])
+ if @<%=child_singular_name%>.save
+ flash[:notice] = "Successfully created <%=child_singular_name%>."
respond_to do |format|
- format.html { redirect_to @photo.album }
- format.json { render :json => { :result => 'success', :photo => photo_url(@photo) } }
+ format.html { redirect_to @<%=child_singular_name%>.<%=parent_singular_name%> }
+ format.json { render :json => { :result => 'success', :<%=child_singular_name%> => <%=child_singular_name%>_url(@<%=child_singular_name%>) } }
end
else
render :action => 'new'
end
end
def edit
- if params[:edit] == 'photo'
+ if params[:edit] == '<%=child_singular_name%>'
render :layout => false
- @photo = Photo.find(params[:id])
+ @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id])
else
- @photo = Photo.find(params[:id])
+ @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id])
end
end
def update
- @photo = Photo.find(params[:id])
- if @photo.update_attributes(params[:photo])
- flash[:notice] = "Successfully updated photo."
- respond_to do |format|
- format.html { redirect_to album_url(@photo.album)}
- format.js { render :json => { :result => 'success', :album => album_url(@photo.album) } }
- end
+ @<%=child_singular_name%> = <%=child_class_name%>.find(params[:id])
+ if @<%=child_singular_name%>.update_attributes(params[:<%=child_singular_name%>])
+ flash[:notice] = "Successfully updated <%=child_singular_name%>."
+ redirect_to <%=parent_singular_name%>_url(@<%=child_singular_name%>.<%=parent_singular_name%>)
else
render :action => 'edit'
end
end
- def destroy
+ def destroy
if request.post?
- if params[:photo_ids]
- params[:photo_ids].each do |photo|
- @photo = Photo.find photo
- @photo.destroy
+ if params[:<%=child_singular_name%>_ids]
+ params[:<%=child_singular_name%>_ids].each do |<%=child_singular_name%>|
+ @<%=child_singular_name%> = <%=child_class_name%>.find <%=child_singular_name%>
+ @<%=child_singular_name%>.destroy
end
- flash[:notice] = "Photos Successfully deleted!"
+ flash[:notice] = "<%=child_plural_class_name%> Successfully deleted!"
end
- respond_to do |format|
- if Photo.all.blank?
- format.html { redirect_to(albums_url) }
- else
- format.html { redirect_to(:back) }
- end
- end
+ redirect_to(<%=parent_singular_name%>_path(@<%=child_singular_name%>.<%=parent_singular_name%>))
end
end
+ def array
+ menu_sort(params[:ul])
+ flash[:notice] = "Successfully Sorted <%=child_plural_class_name%>."
+ end
+
private
def coerce(params)
- if params[:photo].nil?
+ if params[:<%=child_singular_name%>].nil?
h = Hash.new
- h[:photo] = Hash.new
- h[:photo][:album_id] = params[:album_id]
- h[:photo][:title] = params[:title]
- h[:photo][:file] = params[:Filedata]
- h[:photo][:file].content_type = MIME::Types.type_for(h[:photo][:file].original_filename).to_s
+ h[:<%=child_singular_name%>] = Hash.new
+ h[:<%=child_singular_name%>][:<%=parent_singular_name%>_id] = params[:<%=parent_singular_name%>_id]
+ h[:<%=child_singular_name%>][:title] = params[:title]
+ h[:<%=child_singular_name%>][:file] = params[:Filedata]
+ h[:<%=child_singular_name%>][:file].content_type = MIME::Types.type_for(h[:<%=child_singular_name%>][:file].original_filename).to_s
h
else
params
end
end