Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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>)
  • Loading branch information...
commit 500f1bb11ecd10dd6ac01a0f8a7635ba4245015b 1 parent 5f3b88d
@dasch dasch authored
Showing with 18 additions and 8 deletions.
  1. +4 −8 README.md
  2. +4 −0 lib/paperclip/railtie.rb
  3. +10 −0 lib/paperclip/schema.rb
View
12 README.md
@@ -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
View
4 lib/paperclip/railtie.rb
@@ -1,4 +1,5 @@
require 'paperclip'
+require 'paperclip/schema'
module Paperclip
if defined? Rails::Railtie
@@ -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
View
10 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
Please sign in to comment.
Something went wrong with that request. Please try again.