Skip to content
This repository
Browse code

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

  • Loading branch information...
commit fba5b8aeb0bc4060a1faa44a33bbb23eaec0ed6d 2 parents 81129ad + 693b528
Mike Burns authored December 12, 2011
12  README.md
Source Rendered
@@ -80,17 +80,13 @@ In your migrations:
80 80
 
81 81
     class AddAvatarColumnsToUser < ActiveRecord::Migration
82 82
       def self.up
83  
-        add_column :users, :avatar_file_name,    :string
84  
-        add_column :users, :avatar_content_type, :string
85  
-        add_column :users, :avatar_file_size,    :integer
86  
-        add_column :users, :avatar_updated_at,   :datetime
  83
+        change_table :users do |t|
  84
+          t.has_attached_file :avatar
  85
+        end
87 86
       end
88 87
 
89 88
       def self.down
90  
-        remove_column :users, :avatar_file_name
91  
-        remove_column :users, :avatar_content_type
92  
-        remove_column :users, :avatar_file_size
93  
-        remove_column :users, :avatar_updated_at
  89
+        drop_attached_file :users, :avatar
94 90
       end
95 91
     end
96 92
 
5  lib/paperclip/railtie.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require 'paperclip'
  2
+require 'paperclip/schema'
2 3
 
3 4
 module Paperclip
4 5
   if defined? Rails::Railtie
@@ -21,6 +22,10 @@ def self.insert
21 22
       File.send(:include, Paperclip::Upfile)
22 23
 
23 24
       Paperclip.options[:logger] = defined?(ActiveRecord) ? ActiveRecord::Base.logger : Rails.logger
  25
+
  26
+      ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Paperclip::Schema)
  27
+      ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema)
  28
+      ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema)
24 29
     end
25 30
   end
26 31
 end
39  lib/paperclip/schema.rb
... ...
@@ -0,0 +1,39 @@
  1
+module Paperclip
  2
+  # Provides two helpers that can be used in migrations.
  3
+  #
  4
+  # In order to use this module, the target class should implement a
  5
+  # +column+ method that takes the column name and type, both as symbols,
  6
+  # as well as a +remove_column+ method that takes a table and column name,
  7
+  # also both symbols.
  8
+  module Schema
  9
+    @@columns = {:file_name    => :string,
  10
+                 :content_type => :string,
  11
+                 :file_size    => :integer,
  12
+                 :updated_at   => :datetime}
  13
+
  14
+    def has_attached_file(attachment_name)
  15
+      with_columns_for(attachment_name) do |column_name, column_type|
  16
+        column(column_name, column_type)
  17
+      end
  18
+    end
  19
+
  20
+    def drop_attached_file(table_name, attachment_name)
  21
+      with_columns_for(attachment_name) do |column_name, column_type|
  22
+        remove_column(table_name, column_name)
  23
+      end
  24
+    end
  25
+
  26
+    protected
  27
+
  28
+    def with_columns_for(attachment_name)
  29
+      @@columns.each do |suffix, column_type|
  30
+        column_name = full_column_name(attachment_name, suffix)
  31
+        yield column_name, column_type
  32
+      end
  33
+    end
  34
+
  35
+    def full_column_name(attachment_name, column_name)
  36
+      "#{attachment_name}_#{column_name}".to_sym
  37
+    end
  38
+  end
  39
+end
98  test/schema_test.rb
... ...
@@ -0,0 +1,98 @@
  1
+
  2
+require 'test/helper'
  3
+
  4
+class MockSchema
  5
+  include Paperclip::Schema
  6
+
  7
+  def initialize(table_name = nil)
  8
+    @table_name = table_name
  9
+    @columns = {}
  10
+    @deleted_columns = []
  11
+  end
  12
+
  13
+  def column(name, type)
  14
+    @columns[name] = type
  15
+  end
  16
+
  17
+  def remove_column(table_name, column_name)
  18
+    return if @table_name && @table_name != table_name
  19
+    @columns.delete(column_name)
  20
+    @deleted_columns.push(column_name)
  21
+  end
  22
+
  23
+  def has_column?(column_name)
  24
+    @columns.key?(column_name)
  25
+  end
  26
+
  27
+  def deleted_column?(column_name)
  28
+    @deleted_columns.include?(column_name)
  29
+  end
  30
+
  31
+  def type_of(column_name)
  32
+    @columns[column_name]
  33
+  end
  34
+end
  35
+
  36
+class SchemaTest < Test::Unit::TestCase
  37
+  context "Migrating up" do
  38
+    setup do
  39
+      @schema = MockSchema.new
  40
+      @schema.has_attached_file :avatar
  41
+    end
  42
+
  43
+    should "create the file_name column" do
  44
+      assert @schema.has_column?(:avatar_file_name)
  45
+    end
  46
+
  47
+    should "create the content_type column" do
  48
+      assert @schema.has_column?(:avatar_content_type)
  49
+    end
  50
+
  51
+    should "create the file_size column" do
  52
+      assert @schema.has_column?(:avatar_file_size)
  53
+    end
  54
+
  55
+    should "create the updated_at column" do
  56
+      assert @schema.has_column?(:avatar_updated_at)
  57
+    end
  58
+
  59
+    should "make the file_name column a string" do
  60
+      assert_equal :string, @schema.type_of(:avatar_file_name)
  61
+    end
  62
+
  63
+    should "make the content_type column a string" do
  64
+      assert_equal :string, @schema.type_of(:avatar_content_type)
  65
+    end
  66
+
  67
+    should "make the file_size column an integer" do
  68
+      assert_equal :integer, @schema.type_of(:avatar_file_size)
  69
+    end
  70
+
  71
+    should "make the updated_at column a datetime" do
  72
+      assert_equal :datetime, @schema.type_of(:avatar_updated_at)
  73
+    end
  74
+  end
  75
+
  76
+  context "Migrating down" do
  77
+    setup do
  78
+      @schema = MockSchema.new(:users)
  79
+      @schema.drop_attached_file :users, :avatar
  80
+    end
  81
+
  82
+    should "remove the file_name column" do
  83
+      assert @schema.deleted_column?(:avatar_file_name)
  84
+    end
  85
+
  86
+    should "remove the content_type column" do
  87
+      assert @schema.deleted_column?(:avatar_content_type)
  88
+    end
  89
+
  90
+    should "remove the file_size column" do
  91
+      assert @schema.deleted_column?(:avatar_file_size)
  92
+    end
  93
+
  94
+    should "remove the updated_at column" do
  95
+      assert @schema.deleted_column?(:avatar_updated_at)
  96
+    end
  97
+  end
  98
+end

4 notes on commit fba5b8a

Prem Sichanugrist
Collaborator

This broke the build on 1.8.7

Mike Burns
Owner

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

Prem Sichanugrist
Collaborator

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

Mike Burns
Owner

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

Please sign in to comment.
Something went wrong with that request. Please try again.