Commit c334c989 by Jon Yurek

All generators working with Rails 2 and 3. Tested in app for Rails 2 and 3.

parent f2ef8c40
class PaperclipGenerator < Rails::Generators::Base require 'rails/generators/active_record'
include Rails::Generators::Migration
class PaperclipGenerator < ActiveRecord::Generators::Base
desc "Create a migration to add paperclip-specific fields to your model." desc "Create a migration to add paperclip-specific fields to your model."
argument :attachment_class, :required => true, :type => :string, :desc => "The class to migrate.",
:banner => "ClassName"
argument :attachment_names, :required => true, :type => :array, :desc => "The names of the attachment(s) to add.", argument :attachment_names, :required => true, :type => :array, :desc => "The names of the attachment(s) to add.",
:banner => "attachment_one attachment_two attachment_three ..." :banner => "attachment_one attachment_two attachment_three ..."
...@@ -19,7 +17,7 @@ class PaperclipGenerator < Rails::Generators::Base ...@@ -19,7 +17,7 @@ class PaperclipGenerator < Rails::Generators::Base
protected protected
def migration_name def migration_name
"add_attachment_#{attachment_names.join("_")}_to_#{attachment_class.underscore}" "add_attachment_#{attachment_names.join("_")}_to_#{name.underscore}"
end end
def migration_file_name def migration_file_name
...@@ -30,8 +28,4 @@ class PaperclipGenerator < Rails::Generators::Base ...@@ -30,8 +28,4 @@ class PaperclipGenerator < Rails::Generators::Base
migration_name.camelize migration_name.camelize
end end
def self.next_migration_number(dirname) #:nodoc:
Time.now.strftime("%Y%m%d%H%M%S")
end
end end
class <%= migration_class_name %> < ActiveRecord::Migration class <%= migration_class_name %> < ActiveRecord::Migration
def self.up def self.up
<% attachment_names.each do |attachment| -%> <% attachment_names.each do |attachment| -%>
add_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
add_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
add_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer
add_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_updated_at, :datetime add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at, :datetime
<% end -%> <% end -%>
end end
def self.down def self.down
<% attachment_names.each do |attachment| -%> <% attachment_names.each do |attachment| -%>
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_file_name remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_content_type remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_file_size remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_updated_at remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at
<% end -%> <% end -%>
end end
end end
...@@ -337,7 +337,7 @@ module Paperclip ...@@ -337,7 +337,7 @@ module Paperclip
@_paperclip_attachments ||= {} @_paperclip_attachments ||= {}
@_paperclip_attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name]) @_paperclip_attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
end end
def each_attachment def each_attachment
self.class.attachment_definitions.each do |name, definition| self.class.attachment_definitions.each do |name, definition|
yield(name, attachment_for(name)) yield(name, attachment_for(name))
......
...@@ -4,7 +4,7 @@ module Paperclip ...@@ -4,7 +4,7 @@ module Paperclip
if defined? Rails::Railtie if defined? Rails::Railtie
require 'rails' require 'rails'
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
config.after_initialize do initializer "paperclip.insert_into_active_record" do
Paperclip::Railtie.insert Paperclip::Railtie.insert
end end
rake_tasks do rake_tasks do
......
puts "THIS IS THE RAILS/INIT.RB FILE"
require 'paperclip/railtie' require 'paperclip/railtie'
Paperclip::Railtie.insert Paperclip::Railtie.insert
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