class Admin::ArchiveFilesController < OrbitAdminController def index @table_fields = ["Status","Category","Title"] @categories = @module_app.categories @tags = @module_app.tags @filter_fields = { :status=>[{:title=>"is_top",:id=>"is_top"},{:title=>"is_hot",:id=>"is_hot"},{:title=>"is_hidden",:id=>"is_hidden"}], :category=>@categories.map{|c| {:title=>c.title, :id=>c.id}}, :tags=>@tags.map{|tag| {:title=>tag.name, :id=>tag.id}} } status = params[:filters][:status].blank? ? [] : params[:filters][:status] rescue [] categories = params[:filters][:category].blank? ? [] : params[:filters][:category] rescue [] tags = params[:filters][:tags].blank? ? [] : params[:filters][:tags] rescue [] @archives = ArchiveFile.order_by(sort).with_categories(categories).with_tags(tags).with_status(status).page(params[:page]).per(10) if request.xhr? render :partial => "index" end end def new @archive_file = ArchiveFile.new @tags = @module_app.tags @categories = @module_app.categories end def edit @archive_file = ArchiveFile.find(params[:id]) @tags = @module_app.tags @categories = @module_app.categories end def create @archive_file = ArchiveFile.new(archive_vars) @archive_file.create_user_id = current_user.id @archive_file.update_user_id = current_user.id respond_to do |format| if @archive_file.save format.html { redirect_to(admin_archive_files_path) } format.xml { render :xml => @archive_file, :status => :created, :location => @archive_file } else @tags = @module_app.tags format.html { render :action => "new" } format.xml { render :xml => @archive_file.errors, :status => :unprocessable_entity } end end end # PUT /archive_files/1 # PUT /archive_files/1.xml def update @archive_file = ArchiveFile.find(params[:id]) @archive_file.update_user_id = current_user.id respond_to do |format| if @archive_file.update_attributes(archive_vars) format.html { redirect_to(admin_archive_files_path(:page => params[:page])) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @archive_file.errors, :status => :unprocessable_entity } end end end private def archive_vars params[:archive_file][:tags] ||=[] params.require(:archive_file).permit! end def setup_vars @module_app = ModuleApp.where(:key => "archive").first end def sort unless params[:sort].blank? case params[:sort] when "status" @sort = [[:is_top, params[:order]], [:is_hot, params[:order]], [:is_hidden,params[:order]]] when "category" @sort = {:category_id=>params[:order]} when "title" @sort = {:title=>params[:order]} when "last_modified" @sort = {:update_user_id=>params[:order]} end else @sort = {:created_at=>'desc'} end @sort end end