Sha256: faabbd851d99001d471614fabecdcda262400404fccf5c1c729169084d4002e9

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

# coding: utf-8
module UploadifyRails
  module Controllers
    module Base
      def self.included(base)
        base.extend ClassMethods
      end

      module ClassMethods
        def uploadify_nested_resource
          unless included_modules.include? InstanceMethods
            include InstanceMethods
          end
          skip_before_filter :verify_authenticity_token, :only => [:create]
        end
      end

      module InstanceMethods
        def create
          object = controller_name.classify.constantize.new
          object.build_nested_resource(params, current_user)
          if object.save
            render :partial => "shared/uploadify/#{controller_name}/fields",
                   :locals => {:object => object}
          else
            head 442
          end
        end

        def destroy
          load_object
          @object.destroy
          head :ok
        end

        protected

        def load_object
          model = controller_name.classify.constantize
          object_by_session = model.where(:session_id => session[:session_id], :id => params[:id]).first
          object_by_current_user = current_user.send(controller_name).find_by_id(params[:id]) if current_user
          @object = object_by_session || (object_by_current_user if object_by_current_user)
          permission_denied if @object.nil?
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
uploadify_rails-0.0.15 lib/uploadify_rails/controllers/base.rb
uploadify_rails-0.0.14 lib/uploadify_rails/controllers/base.rb
uploadify_rails-0.0.12 lib/uploadify_rails/controllers/base.rb