Controlador para administrar las fotos de la fototeca
Añadir una foto a un álbum
# File app/controllers/admin/photos_controller.rb, line 118 def add_to_album @photo = Photo.find(params[:id]) respond_to do |format| format.js do render :update do |page| if params[:album_id].present? @photo.albums << Album.find(params[:album_id]) page.replace "albums_for_#{@photo.id}", :partial => "albums_select", :locals => {:photo => @photo} end page.visual_effect :highlight, "albums_for_#{@photo.id}" end end end end
Lista de tags para el campo de auto-complete
# File app/controllers/admin/photos_controller.rb, line 178 def auto_complete_for_photo_tag_list_es auto_complete_for_tag_list(params[:photo][:tag_list_es]) if @tags.length > 0 render :inline => "<%= content_tag(:ul, @tags.map {|t| content_tag(:li, t.nombre)}) %>" else render :nothing => true end end
Modificar los atributos de varias fotos
# File app/controllers/admin/photos_controller.rb, line 95 def batch_edit @photos = Photo.find(params[:ids]) end
Actualizar los atributos de varias fotos
# File app/controllers/admin/photos_controller.rb, line 100 def batch_update @photos = Photo.find(params[:photos].keys.collect(&:to_i)) begin Photo.transaction do @photos.each do |photo| photo.update_attributes!(params[:photos][photo.id.to_s]) end flash[:notice] = "Las fotos se han actualizado correctamente" redirect_to admin_albums_path end rescue flash[:error] = "Las fotos no se han actualizado" render :action => "batch_edit" end end
Importar fotos nuevas, tanto en un álbum nuevo como en uno existente
# File app/controllers/admin/photos_controller.rb, line 56 def create @photo = Photo.new(params[:photo].merge(:file_path => params[:dir_path], :dir_path => params[:dir_path])) if @photo.valid? && params[:dir_path].present? if params[:album_id].to_i == 0 @album = Album.create(:title_es => params[:photo][:title_es], :title_eu => params[:photo][:title_eu], :title_en => params[:photo][:title_en]) else @album = Album.find(params[:album_id]) end import_photos else @photo.errors.add_to_base("No ha indicado ningún directorio") if params[:dir_path].blank? render :action => "new" and return end end
Eliminar una foto
# File app/controllers/admin/photos_controller.rb, line 157 def destroy @album = Album.find(params[:album_id]) if params[:album_id] @photo = Photo.find(params[:id]) if @photo.destroy flash[:notice] = "La foto ha sido eliminada" if @album redirect_to admin_album_path(@album) else redirect_to orphane_admin_photos_path end else flash[:error] = "La foto no ha podido ser eliminada" if @album redirect_to admin_album_photo_path(@album, @photo) else redirect_to orphane_admin_photos_path end end end
Modificar una foto
# File app/controllers/admin/photos_controller.rb, line 74 def edit @album = Album.find(params[:album_id]) if params[:album_id] @photo = Photo.find(params[:id]) end
Busca fotos en el directorio especificado
# File app/controllers/admin/photos_controller.rb, line 134 def find_photos p = Photo.new(:dir_path => params[:dir_path]) if !p.valid? && p.errors.on("dir_path") render :update do |page| page.replace_html :find_photos, :text => "<span style='color:red'>Directorio incorrecto.<br/> #{p.errors.on('dir_path')}</span>" end else dir = Photo::PHOTOS_PATH logger.info "Buscando fotos #{dir + params[:dir_path] + "*.jpg"}" if (found_files = Dir.glob(dir + params[:dir_path] + "*.jpg") + Dir.glob(dir + params[:dir_path] + "solo_irekia/*.jpg")) && found_files.length>0 render :update do |page| page.replace_html :find_photos, :text => "<span style='color:green'>#{found_files.length} fotos encontradas</span>" end else render :update do |page| page.replace_html :find_photos, :text => "<span style='color:red'>fotos NO encontradas</span>" end end end end
before_filter :get_album, :only => [:destroy, :update]
# File app/controllers/admin/photos_controller.rb, line 30 def index @album = Album.find(params[:album_id]) @aphotos = @album.album_photos.ordered_by_title.paginate :joins => :photo, :page => params[:page] end
Listado de fotos que no pertenecen a ningún álbum
# File app/controllers/admin/photos_controller.rb, line 36 def orphane @photos = Photo.paginate :page => params[:page], :order => "created_at DESC", :conditions => "NOT EXISTS (SELECT 1 FROM album_photos WHERE album_photos.photo_id=photos.id)" end
Vista de una foto
# File app/controllers/admin/photos_controller.rb, line 42 def show if params[:album_id] @album = Album.find(params[:album_id]) @photo = @album.photos.find(params[:id]) all_photos = @album.photos.ordered_by_title @next_photo = all_photos[all_photos.index(@photo) + 1] @prev_photo = all_photos[all_photos.index(@photo) - 1] unless all_photos.index(@photo) == 0 else @photo = Photo.find(params[:id]) end end
Actualizar los atributos de una foto
# File app/controllers/admin/photos_controller.rb, line 80 def update @album = Album.find(params[:album_id]) if params[:album_id] @photo = Photo.find(params[:id]) if @photo.update_attributes(params[:photo]) if @album && @album.photos.exists?(@photo.id) redirect_to admin_photo_path(@photo, :album_id => @album.id) else redirect_to admin_photo_path(@photo) end else render :action => "new" end end