Commit fba5b8ae by Mike Burns

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

parents 81129ad1 693b5280
......@@ -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
......
require 'paperclip'
require 'paperclip/schema'
module Paperclip
if defined? Rails::Railtie
......@@ -21,6 +22,10 @@ module Paperclip
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
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
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
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