Sha256: 0aac835406be8609f5f3bcd2dc71e9f6d3899700aa0d40779a55ec34b4e71c41

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Merb::Generators

  class PaperclipGenerator < NamespacedGenerator
    
    def self.source_root
      File.dirname(__FILE__) / 'templates'
    end
    
    desc <<-DESC
      Generators a paperclip migration
    DESC
    
    first_argument  :name, :required => true, :desc => "model name"
    second_argument :attachments, :required => true, :as => :array, :default => [], :desc => "space separated list of fields"

    template :paperclip do
      source(File.dirname(__FILE__) / 'templates' / '%file_name%.rb')
      destination("schema/migrations/#{migration_file_name}.rb")
    end
    
    def version
      format("%03d", current_migration_nr + 1)
    end
    
    def migration_file_name
      names = migration_attachments
      "#{version}_add_attachments_#{names.join("_")}_to_#{class_name.underscore}"
    end
    
    def migration_name
      names = migration_attachments
      "add_attachments_#{names.join("_")}_to_#{class_name.underscore}".classify
    end

    protected
    
    def migration_attachments      
      names = attachments.map(&:underscore)
      attachments.length == 1 ? names : names[0..-2] + ["and", names[-1]]
    end
    
    def destination_directory
      File.join(destination_root, 'schema', 'migrations')
    end

    def current_migration_nr
      Dir["#{destination_directory}/*"].map{|f| File.basename(f).match(/^(\d+)/)[0].to_i  }.max.to_i
    end
    
  end

  add :paperclip, PaperclipGenerator
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
jeremydurham-merb_paperclip-0.9.12 lib/generators/paperclip_generator.rb
jeremydurham-merb_paperclip-0.9.13 lib/generators/paperclip_generator.rb
rughetto-merb_paperclip-0.9.13 lib/generators/paperclip_generator.rb