Ruby on Rails自定义迁移生成器 [英] Ruby on Rails Custom Migration Generator
问题描述
我正在创建一个与Active Record紧密集成的Rails创业板。宝石需要定义许多字段。例如:
class User< ActiveRecord :: Base
#需要'avatar_identifier','avatar_extension','avatar_size'
has_attached:avatar
end
有可能有这样的东西:
rails g模型用户名: string avatar:attached
结果:
create_table:users do | t |
t.string:name
t.string:avatar_identifier
t.string:avatar_extension
t.integer:avatar_size
end
$ c $
如果这是不可能的, create_table:users do | t |
t.string:name
t.attached:avatar
end
<生成多个字段?谢谢!
解决方案虽然Pravin确实指向了正确的方向,但我发现实现它并不简单。我做了以下操作,我在 config / initializers
(名称不相关)中添加了一个文件,其中包含以下内容:
require'active_support'
require'active_record'
class YourApplication
模块SchemaDefinitions
模块ExtraMethod
def附件(* args)
选项= 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}
结束
$ b $结束
结束
ActiveSupport.on_load:active_record do
YourApplication :: SchemaDefinitions.load!
end
然后您可以执行下列操作:
rails g模型人名:字符串标题:字符串头像:附件
这将创建以下迁移:
def self.up
create_table:people do | t |
t.string:name
t.string:title
t.attachment:avatar
t.timestamps
end
end
如果您然后运行迁移, rake db:migrate
它将创建以下 Person
模型:
ruby-1.9。 2-p0>人
=> Person(id:integer,name:string,title:string,avatar_identifier:string,avatar_extension:string,avatar_size:integer,created_at:datetime,updated_at:datetime)
希望这有助于!!
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!
解决方案 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!!
这篇关于Ruby on Rails自定义迁移生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!