Commit 500f1bb1 by Daniel Schierbeck

Add a helper that creates the Paperclip columns

    This can be used in migrations, i.e.

      create_table :users do |t|
        t.has_attached_file :avatar
      end

(three commits squashed into one by Alexey Mahotkin <squadette@gmail.com>)
parent 5f3b88d1
...@@ -80,17 +80,13 @@ In your migrations: ...@@ -80,17 +80,13 @@ In your migrations:
class AddAvatarColumnsToUser < ActiveRecord::Migration class AddAvatarColumnsToUser < ActiveRecord::Migration
def self.up def self.up
add_column :users, :avatar_file_name, :string change_table :users do |t|
add_column :users, :avatar_content_type, :string t.has_attached_file :avatar
add_column :users, :avatar_file_size, :integer end
add_column :users, :avatar_updated_at, :datetime
end end
def self.down def self.down
remove_column :users, :avatar_file_name drop_attached_file :users, :avatar
remove_column :users, :avatar_content_type
remove_column :users, :avatar_file_size
remove_column :users, :avatar_updated_at
end end
end end
......
require 'paperclip' require 'paperclip'
require 'paperclip/schema'
module Paperclip module Paperclip
if defined? Rails::Railtie if defined? Rails::Railtie
...@@ -21,6 +22,9 @@ module Paperclip ...@@ -21,6 +22,9 @@ module Paperclip
File.send(:include, Paperclip::Upfile) File.send(:include, Paperclip::Upfile)
Paperclip.options[:logger] = defined?(ActiveRecord) ? ActiveRecord::Base.logger : Rails.logger Paperclip.options[:logger] = defined?(ActiveRecord) ? ActiveRecord::Base.logger : Rails.logger
ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema)
ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema)
end end
end end
end end
module Paperclip
module Schema
def has_attached_file(name)
column :"#{name}_file_name", :string
column :"#{name}_content_type", :string
column :"#{name}_file_size", :integer
column :"#{name}_updated_at", :datetime
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment