Permalink
Browse files

Merge branch 'schema-helper' of https://github.com/dasch/paperclip

  • Loading branch information...
2 parents 81129ad + 693b528 commit fba5b8aeb0bc4060a1faa44a33bbb23eaec0ed6d @mike-burns mike-burns committed Dec 12, 2011
Showing with 146 additions and 8 deletions.
  1. +4 −8 README.md
  2. +5 −0 lib/paperclip/railtie.rb
  3. +39 −0 lib/paperclip/schema.rb
  4. +98 −0 test/schema_test.rb
View
@@ -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
@@ -1,4 +1,5 @@
require 'paperclip'
+require 'paperclip/schema'
module Paperclip
if defined? Rails::Railtie
@@ -21,6 +22,10 @@ def self.insert
File.send(:include, Paperclip::Upfile)
Paperclip.options[:logger] = defined?(ActiveRecord) ? ActiveRecord::Base.logger : Rails.logger
+
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Paperclip::Schema)
+ ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema)
+ ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema)
end
end
end
@@ -0,0 +1,39 @@
+module Paperclip
+ # Provides two helpers that can be used in migrations.
+ #
+ # In order to use this module, the target class should implement a
+ # +column+ method that takes the column name and type, both as symbols,
+ # as well as a +remove_column+ method that takes a table and column name,
+ # also both symbols.
+ module Schema
+ @@columns = {:file_name => :string,
+ :content_type => :string,
+ :file_size => :integer,
+ :updated_at => :datetime}
+
+ def has_attached_file(attachment_name)
+ with_columns_for(attachment_name) do |column_name, column_type|
+ column(column_name, column_type)
+ end
+ end
+
+ def drop_attached_file(table_name, attachment_name)
+ with_columns_for(attachment_name) do |column_name, column_type|
+ remove_column(table_name, column_name)
+ end
+ end
+
+ protected
+
+ def with_columns_for(attachment_name)
+ @@columns.each do |suffix, column_type|
+ column_name = full_column_name(attachment_name, suffix)
+ yield column_name, column_type
+ end
+ end
+
+ def full_column_name(attachment_name, column_name)
+ "#{attachment_name}_#{column_name}".to_sym
+ end
+ end
+end
View
@@ -0,0 +1,98 @@
+
+require 'test/helper'
+
+class MockSchema
+ include Paperclip::Schema
+
+ def initialize(table_name = nil)
+ @table_name = table_name
+ @columns = {}
+ @deleted_columns = []
+ end
+
+ def column(name, type)
+ @columns[name] = type
+ end
+
+ def remove_column(table_name, column_name)
+ return if @table_name && @table_name != table_name
+ @columns.delete(column_name)
+ @deleted_columns.push(column_name)
+ end
+
+ def has_column?(column_name)
+ @columns.key?(column_name)
+ end
+
+ def deleted_column?(column_name)
+ @deleted_columns.include?(column_name)
+ end
+
+ def type_of(column_name)
+ @columns[column_name]
+ end
+end
+
+class SchemaTest < Test::Unit::TestCase
+ context "Migrating up" do
+ setup do
+ @schema = MockSchema.new
+ @schema.has_attached_file :avatar
+ end
+
+ should "create the file_name column" do
+ assert @schema.has_column?(:avatar_file_name)
+ end
+
+ should "create the content_type column" do
+ assert @schema.has_column?(:avatar_content_type)
+ end
+
+ should "create the file_size column" do
+ assert @schema.has_column?(:avatar_file_size)
+ end
+
+ should "create the updated_at column" do
+ assert @schema.has_column?(:avatar_updated_at)
+ end
+
+ should "make the file_name column a string" do
+ assert_equal :string, @schema.type_of(:avatar_file_name)
+ end
+
+ should "make the content_type column a string" do
+ assert_equal :string, @schema.type_of(:avatar_content_type)
+ end
+
+ should "make the file_size column an integer" do
+ assert_equal :integer, @schema.type_of(:avatar_file_size)
+ end
+
+ should "make the updated_at column a datetime" do
+ assert_equal :datetime, @schema.type_of(:avatar_updated_at)
+ end
+ end
+
+ context "Migrating down" do
+ setup do
+ @schema = MockSchema.new(:users)
+ @schema.drop_attached_file :users, :avatar
+ end
+
+ should "remove the file_name column" do
+ assert @schema.deleted_column?(:avatar_file_name)
+ end
+
+ should "remove the content_type column" do
+ assert @schema.deleted_column?(:avatar_content_type)
+ end
+
+ should "remove the file_size column" do
+ assert @schema.deleted_column?(:avatar_file_size)
+ end
+
+ should "remove the updated_at column" do
+ assert @schema.deleted_column?(:avatar_updated_at)
+ end
+ end
+end

4 comments on commit fba5b8a

@sikachu
Member

This broke the build on 1.8.7

@mike-burns
Member

Travis-CI says it's 1.9.2, which makes more sense since I only ran it on 1.8.7.

@sikachu
Member

@mike-burns lol. Maybe I was asleep. :P

@mike-burns
Member

65a6ae8 - added this commit in an attempt to fix this.

Please sign in to comment.