module Para::Ext::Paperclip::HasAttachedFileMixin

Public Instance Methods

define() click to toggle source
Calls superclass method
# File lib/para/ext/paperclip.rb, line 7
def define
  super
  define_removeable
end

Private Instance Methods

define_removeable() click to toggle source
# File lib/para/ext/paperclip.rb, line 14
def define_removeable
  return if @options[:removeable] == false

  attachment_name = @name
  klass = @klass
  # Define the setter to remove the attachment
  #
  @klass.send :define_method, :"remove_#{ attachment_name }=" do |value|
    if value == '1'
      removed_attachments << attachment_name

      # Notify ActiveRecord that the model has changed so nested models
      # get to run validation hooks and attachments are cleared
      column_name = attribute_container_column_for(attachment_name)
      send(:"#{ column_name }_will_change!")
    end
  end

  @klass.send :define_method, :"remove_#{ attachment_name }" do |value|
    removed_attachments.include?(attachment_name) ? '1' : nil
  end

  # Lazy method initialization on attachment target class
  #
  unless @klass.method_defined?(:removed_attachments)
    # List of all removed attachments for the current instance
    #
    @klass.send :define_method, :removed_attachments do
      @removed_attachments ||= []
    end

    # Define before validation hook to clear removed attachments before
    # the instance is validated
    #
    @klass.send :define_method, :clear_removed_attachments do
      removed_attachments.each do |name|
        if send(:"#{ name }?")
          send(name).clear
        end
      end

      removed_attachments.clear
    end

    @klass.send(:before_validation, :clear_removed_attachments)
  end

  @klass.send(:define_method, :attribute_container_column_for) do |attachment_name|
    attribute_name = "#{ attachment_name }_file_name"

    if self.class.columns.any? { |column| column.name == attribute_name }
      return attribute_name
    end

    stored_attribute = self.class.stored_attributes.each do |key, fields|
      return key if fields.include?(attribute_name.to_sym)
    end
  end
end