module HasAttachable::ClassMethods

Public Instance Methods

has_attachable(*attachable_options) click to toggle source
# File lib/has_attachable.rb, line 22
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 Instance Methods

initialize_attachable(field, options) click to toggle source
# File lib/has_attachable.rb, line 31
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_accessor "async_remove_#{field}".to_sym

    define_method("#{field}_processing?") { 
      if self.send("#{field}_job_id").present? 
        not Sidekiq::Status::complete? self.send("#{field}_job_id")
      else
        false
      end
    }

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

  end
end