Sha256: b1b3a6120012985ef2be35697431519cc34f6b9c2264501cad662cbdfa2b86fa

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

class Lolita::UploadsController < ApplicationController
  include Lolita::ControllerAdditions
  before_filter :authenticate_lolita_user!
  before_filter :set_tab, :only=>[:index,:edit,:update,:destroy,:create]
  before_filter :set_resource, :only=>[:create,:edit,:update,:destroy]
  before_filter :set_file, :only=>[:edit,:update,:destroy,:create]

  respond_to :js,:html

  def index
    render_component *@tab.build("",:list,:file=>@file)
  end

  def create
    authorization_proxy.can?(:create,@file.class)
    @file.send(:"#{@tab.uploader}=",params[:file])
    @file.save! 
    render_component *@tab.build("",:image,:file=>@file, :format => "html")
  end

  def edit
    authorization_proxy.can?(:update,@file.class)
    render_component *@tab.build("",:edit,:file=>@file)
  end

  def update
    authorization_proxy.can?(:update,@file.class)
    @file.update_attributes!(params[:file])
    render_component *@tab.build("",:update,:file=>@file)
  end

  def destroy
    authorization_proxy.can?(:destroy,@file.class)
    @file.destroy
    render_component *@tab.build("",:destroy,:file=>@file)
  end

  private

  def set_resource
    association_id = params["#{lolita_mapping.singular}_id"] || params[:upload]["#{lolita_mapping.singular}_id"] || params[:upload]["#{@tab.association.options[:as]}_id"]
    self.resource = resource_class.find_by_id(association_id)
  end

  def build_file
    @file = @tab.association.klass.new
    params[:upload].each do |k,v|
      @file[k] = v
    end
    @file
  end

  def set_file
    dbi=Lolita::DBI::Base.new(@tab.association.klass)
    @file = dbi.find_by_id(params[:id]) || build_file
  end

  def set_tab
    @tab=tab_by_association(params[:association])
  end

  def tab_by_association name
    resource_class.lolita.tabs.detect{|tab| tab.type == :files && (tab.try(:association) && tab.association.name == name.to_sym)}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lolita-file-upload-0.5.0 app/controllers/lolita/uploads_controller.rb
lolita-file-upload-0.4.13 app/controllers/lolita/uploads_controller.rb