Sha256: 372e4fda9bd96a2342ddd1380b04caad79619b51a7bc0c7435e5277fe0d84aff

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require "has_attachable/version"
require "has_attachable/processing"

module HasAttachable
  extend ActiveSupport::Concern

  class NoUploaderError < StandardError
    def message
      "No Uploader found, please specify an uploader"
    end
  end

  included do 
    after_update -> {
      AttachableWorker.
        perform_async('process', 
                       processing_options) if process_attachable?
      AttachableWorker.
        perform_async('remove', 
                       processing_options) if remove_attachable?
    }
    include Attachable::Processing
  end

  module ClassMethods
    def has_attachable(*attachable_options)
      field = attachable_options[0]
      options = attachable_options[1]
      options[:type] = :graphic unless options[:type].present?
      initialize_attachable(field, options)
    end

    private 

    def initialize_attachable(field, options)
      class_attribute :attachable_options unless defined? self.attachable_options
      self.attachable_options ||= {}
      self.attachable_options[field] = options

      class_attribute :attachable_fields unless defined? self.attachable_fields
      self.attachable_fields ||= []
      self.attachable_fields << field

      raise NoUploaderError          unless attachable_options[field][:uploader].present?
      attachable_field = :attachable unless field.present?
      

      class_eval do 
        attr_accessible "async_remove_#{field}".to_sym, "#{field}_name".to_sym
        attr_accessor "async_remove_#{field}".to_sym

        mount_uploader field, 
                       attachable_options[field][:uploader], 
                       mount_on: "#{field}_name"

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_attachable-0.0.1 lib/has_attachable.rb