Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 86 lines (70 sloc) 2.945 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
require 'active_support/deprecation'
require 'paperclip/style_adder'
require 'paperclip/style_remover'

module Paperclip
  # Provides helper methods that can be used in migrations.
  module Schema
    COLUMNS = {:file_name => :string,
               :content_type => :string,
               :file_size => :integer,
               :updated_at => :datetime}

    def self.included(base)
      ActiveRecord::ConnectionAdapters::Table.send :include, TableDefinition
      ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TableDefinition
      ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements

      if defined?(ActiveRecord::Migration::CommandRecorder) # Rails 3.1+
        ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
      end
    end

    module Statements
      def add_attachment(table_name, *attachment_names)
        raise ArgumentError, "Please specify attachment name in your add_attachment call in your migration." if attachment_names.empty?

        attachment_names.each do |attachment_name|
          COLUMNS.each_pair do |column_name, column_type|
            add_column(table_name, "#{attachment_name}_#{column_name}", column_type)
          end
        end
      end

      def remove_attachment(table_name, *attachment_names)
        raise ArgumentError, "Please specify attachment name in your remove_attachment call in your migration." if attachment_names.empty?

        attachment_names.each do |attachment_name|
          COLUMNS.each_pair do |column_name, column_type|
            remove_column(table_name, "#{attachment_name}_#{column_name}")
          end
        end
      end

      def drop_attached_file(*args)
        ActiveSupport::Deprecation.warn "Method `drop_attached_file` in the migration has been deprecated and will be replaced by `remove_attachment`."
        remove_attachment(*args)
      end

      def add_style(table_name, attachment_name, styles)
        StyleAdder.run(table_name, attachment_name, styles)
      end

      def remove_style(table_name, attachment_name, style_name)
        StyleRemover.run(table_name, attachment_name, style_name)
      end
    end

    module TableDefinition
      def attachment(*attachment_names)
        attachment_names.each do |attachment_name|
          COLUMNS.each_pair do |column_name, column_type|
            column("#{attachment_name}_#{column_name}", column_type)
          end
        end
      end

      def has_attached_file(*attachment_names)
        ActiveSupport::Deprecation.warn "Method `t.has_attached_file` in the migration has been deprecated and will be replaced by `t.attachment`."
        attachment(*attachment_names)
      end
    end

    module CommandRecorder
      def add_attachment(*args)
        record(:add_attachment, args)
      end

      private

      def invert_add_attachment(args)
        [:remove_attachment, args]
      end
    end
  end
end
Something went wrong with that request. Please try again.