Sha256: 3c08240d3bfde26e39c2e2329655eb23bbe1e99b9b0059612b1227ebac0198b5

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'active_support/concern'

module Uploader
  module Fileuploads
    extend ActiveSupport::Concern

    included do
      class_attribute :fileupload_options, instance_writer: false

      delegate :asset, :multiple?, :params, :klass, to: :fileupload_glue, prefix: :fileupload

      after_create :fileupload_update, if: :fileupload_changed?
    end

    module ClassMethods
      # Join ActiveRecord object with uploaded file
      # Usage:
      #
      #   class Article < ActiveRecord::Base
      #     has_one :picture, as: :assetable, dependent: :destroy
      #
      #     fileuploads :picture
      #   end
      #
      #
      def fileuploads(*args)
        options = args.extract_options!

        self.fileupload_options ||= {}

        args.each do |column|
          self.fileupload_options[column] = options
        end
      end
    end

    # Generate unique key per form
    def fileupload_guid
      @fileupload_guid ||= Uploader.guid
    end

    def fileupload_guid=(value)
      @fileupload_changed = (@fileupload_guid != value) if @fileupload_changed.nil?
      @fileupload_guid = value.blank? ? nil : value
    end

    def fileupload_changed?
      @fileupload_changed == true
    end

    protected

    def fileupload_glue
      @fileupload_glue ||= Uploader::FileuploadGlue.new(self)
    end

    def fileupload_update
      fileupload_glue.join!
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails-uploader-0.4.4 lib/uploader/fileuploads.rb
rails-uploader-0.4.3 lib/uploader/fileuploads.rb