Skip to content

Commit

Permalink
All generators working with Rails 2 and 3. Tested in app for Rails 2 …
Browse files Browse the repository at this point in the history
…and 3.
  • Loading branch information
Jon Yurek committed Jun 7, 2010
1 parent f2ef8c4 commit c334c98
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
12 changes: 3 additions & 9 deletions lib/generators/paperclip/paperclip_generator.rb
@@ -1,10 +1,8 @@
class PaperclipGenerator < Rails::Generators::Base
include Rails::Generators::Migration
require 'rails/generators/active_record'

class PaperclipGenerator < ActiveRecord::Generators::Base
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.",
:banner => "attachment_one attachment_two attachment_three ..."

Expand All @@ -19,7 +17,7 @@ def generate_migration
protected

def migration_name
"add_attachment_#{attachment_names.join("_")}_to_#{attachment_class.underscore}"
"add_attachment_#{attachment_names.join("_")}_to_#{name.underscore}"
end

def migration_file_name
Expand All @@ -30,8 +28,4 @@ def migration_class_name
migration_name.camelize
end

def self.next_migration_number(dirname) #:nodoc:
Time.now.strftime("%Y%m%d%H%M%S")
end

end
16 changes: 8 additions & 8 deletions lib/generators/paperclip/templates/paperclip_migration.rb.erb
@@ -1,19 +1,19 @@
class <%= migration_class_name %> < ActiveRecord::Migration
def self.up
<% attachment_names.each do |attachment| -%>
add_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
add_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
add_column :<%= attachment_class.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 %>_file_name, :string
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at, :datetime
<% end -%>
end

def self.down
<% attachment_names.each do |attachment| -%>
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_file_name
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_content_type
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_file_size
remove_column :<%= attachment_class.underscore.camelize.tableize %>, :<%= attachment %>_updated_at
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at
<% end -%>
end
end
2 changes: 1 addition & 1 deletion lib/paperclip.rb
Expand Up @@ -337,7 +337,7 @@ def attachment_for name
@_paperclip_attachments ||= {}
@_paperclip_attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
end

def each_attachment
self.class.attachment_definitions.each do |name, definition|
yield(name, attachment_for(name))
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/railtie.rb
Expand Up @@ -4,7 +4,7 @@ module Paperclip
if defined? Rails::Railtie
require 'rails'
class Railtie < Rails::Railtie
config.after_initialize do
initializer "paperclip.insert_into_active_record" do
Paperclip::Railtie.insert
end
rake_tasks do
Expand Down
1 change: 1 addition & 0 deletions rails/init.rb
@@ -1,2 +1,3 @@
puts "THIS IS THE RAILS/INIT.RB FILE"
require 'paperclip/railtie'
Paperclip::Railtie.insert

0 comments on commit c334c98

Please sign in to comment.