Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Custom Migration Generator

I'm creating a Rails gem that integrates closely with Active Record. The gem requires a number of fields to be defined. For example:

class User < ActiveRecord::Base
  # requires 'avatar_identifier', 'avatar_extension', 'avatar_size'
  has_attached :avatar
end

Is it possible to have something like:

rails g model user name:string avatar:attached

Resulting in:

create_table :users do |t|
  t.string :name
  t.string :avatar_identifier
  t.string :avatar_extension
  t.integer :avatar_size
end

If this isn't possible, any way to make:

create_table :users do |t|
  t.string :name
  t.attached :avatar
end

Generate multiple fields? Thanks!

like image 389
Kevin Sylvestre Avatar asked Mar 05 '11 05:03

Kevin Sylvestre


People also ask

How do I migrate a specific migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

Which command is used to rollback migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

What is schema rb used for?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone.


1 Answers

While Pravin did point in the right direction, i found it was not straightforward to implement it. I did the following, i added a file in config/initializers (name is not relevant), containing the following:

require 'active_support'
require 'active_record'

class YourApplication
  module SchemaDefinitions

    module ExtraMethod
      def attachment(*args)
        options = args.extract_options!
        args.each do |col|
          column("#{col}_identifier", :string, options)
          column("#{col}_extension", :string, options)
          column("#{col}_size", :integer, options)
        end
      end
    end

    def self.load!
      ::ActiveRecord::ConnectionAdapters::TableDefinition.class_eval { include YourApplication::SchemaDefinitions::ExtraMethod }
    end

  end
end


ActiveSupport.on_load :active_record do
  YourApplication::SchemaDefinitions.load!
end

then you can just do something like:

rails g model Person name:string title:string avatar:attachment

which will create the following migration:

def self.up
  create_table :people do |t|
    t.string :name
    t.string :title
    t.attachment :avatar

    t.timestamps
  end
end

If you then run the migration, rake db:migrate it will create the following Person model:

ruby-1.9.2-p0 > Person
 => Person(id: integer, name: string, title: string, avatar_identifier: string, avatar_extension: string, avatar_size: integer, created_at: datetime, updated_at: datetime) 

Hope this helps!!

like image 113
nathanvda Avatar answered Oct 20 '22 02:10

nathanvda