This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
94 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
tmp | ||
test/s3.yml | ||
public | ||
paperclip*.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Description: | ||
Explain the generator | ||
|
||
Example: | ||
rails generate paperclip Thing | ||
|
||
This will create: | ||
what/will/it/create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'rails/generators/active_record' | ||
|
||
class PaperclipGenerator < ActiveRecord::Generators::Base | ||
desc "Create a migration to add paperclip-specific fields to your model." | ||
|
||
argument :attachment_names, :required => true, :type => :array, :desc => "The names of the attachment(s) to add.", | ||
:banner => "attachment_one attachment_two attachment_three ..." | ||
|
||
def self.source_root | ||
@source_root ||= File.expand_path('../templates', __FILE__) | ||
end | ||
|
||
def generate_migration | ||
migration_template "paperclip_migration.rb.erb", "db/migrate/#{migration_file_name}" | ||
end | ||
|
||
protected | ||
|
||
def migration_name | ||
"add_attachment_#{attachment_names.join("_")}_to_#{name.underscore}" | ||
end | ||
|
||
def migration_file_name | ||
"#{migration_name}.rb" | ||
end | ||
|
||
def migration_class_name | ||
migration_name.camelize | ||
end | ||
|
||
end |
19 changes: 19 additions & 0 deletions
19
lib/generators/paperclip/templates/paperclip_migration.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class <%= migration_class_name %> < ActiveRecord::Migration | ||
def self.up | ||
<% attachment_names.each do |attachment| -%> | ||
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 :<%= 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'paperclip' | ||
|
||
module Paperclip | ||
if defined? Rails::Railtie | ||
require 'rails' | ||
class Railtie < Rails::Railtie | ||
initializer "paperclip.insert_into_active_record" do | ||
Paperclip::Railtie.insert | ||
end | ||
rake_tasks do | ||
load "tasks/paperclip.rake" | ||
end | ||
end | ||
end | ||
|
||
class Railtie | ||
def self.insert | ||
ActiveRecord::Base.send(:include, Paperclip) | ||
File.send(:include, Paperclip::Upfile) | ||
end | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
puts "THIS IS THE RAILS/INIT.RB FILE" | ||
require 'paperclip/railtie' | ||
Paperclip::Railtie.insert |