开发者

Ruby on Rails Custom Migration Generator

开发者 https://www.devze.com 2023-02-15 10:56 出处:网络
I\'m creating a Rails gem that integrates closely with Active Record. The gem requires a number of fields to be defined. For example:

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_开发者_Python百科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!


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!!


Actually if you call

rails g model profile name:string next:attached

rails allready generates you a migration with

def self.up
  create_table :profiles do |t|
    t.string :name
    t.attached :next

    t.timestamps
  end
end

however you can override the default migration template by placing it in /lib/templates/active_record/model/migration.rb

You should write a rake my_gem:setup task to put the file there I haven't tried, but i guess rails does not search in non-engine gems for these templates

Your migration template contents would then look like

class <%= migration_class_name %> < ActiveRecord::Migration
  def self.up
    create_table :<%= table_name %> do |t|
<% for attribute in attributes -%>
  <% if attribute.type.to_s == "attached" %>
      t.string :<%= attribute.name %>_identifier
      t.string :<%= attribute.name %>_extension
      t.integer :<%= attribute.name %>_size
  <% else %>
      t.<%= attribute.type %> :<%= attribute.name %>
  <% end %>
<% end -%>
<% if options[:timestamps] %>
      t.timestamps
<% end -%>
    end
  end

  def self.down
    drop_table :<%= table_name %>
  end
end


I consider t.attached similar to t.references in a polymorphic association.

With reference to the references method you can have something like below

def attached(*args)
  options = args.extract_options!
  column(:avatar_identifier, :string, options)
  column(:avatar_extension, :string, options)
  column(:avatar_size, :integer, options)
end

You might like to extend ActiveRecord::ConnectionAdapters::TableDefinition
Have look at this http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-references

0

精彩评论

暂无评论...
验证码 换一张
取 消