Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Add a helper that creates the Paperclip columns
Browse files Browse the repository at this point in the history
    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>)
  • Loading branch information
dasch committed Dec 12, 2011
1 parent 5f3b88d commit 500f1bb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
12 changes: 4 additions & 8 deletions README.md
Expand Up @@ -80,17 +80,13 @@ In your migrations:

class AddAvatarColumnsToUser < ActiveRecord::Migration
def self.up
add_column :users, :avatar_file_name, :string
add_column :users, :avatar_content_type, :string
add_column :users, :avatar_file_size, :integer
add_column :users, :avatar_updated_at, :datetime
change_table :users do |t|
t.has_attached_file :avatar
end
end

def self.down
remove_column :users, :avatar_file_name
remove_column :users, :avatar_content_type
remove_column :users, :avatar_file_size
remove_column :users, :avatar_updated_at
drop_attached_file :users, :avatar
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/paperclip/railtie.rb
@@ -1,4 +1,5 @@
require 'paperclip'
require 'paperclip/schema'

module Paperclip
if defined? Rails::Railtie
Expand All @@ -21,6 +22,9 @@ def self.insert
File.send(:include, Paperclip::Upfile)

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
10 changes: 10 additions & 0 deletions lib/paperclip/schema.rb
@@ -0,0 +1,10 @@
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

0 comments on commit 500f1bb

Please sign in to comment.