Skip to content
This repository has been archived by the owner on Jul 7, 2019. It is now read-only.

Commit

Permalink
Added migration(s) generator
Browse files Browse the repository at this point in the history
  • Loading branch information
linucs committed Sep 1, 2010
1 parent 3cedf1f commit 818047a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/generators/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Description:
Generates the appropriate database migration to add i18n fields

Example:
rails generate has_foreign_language <language> [Model]

This will create:
db/migrate/yyyyMMddssssss_add_foreign_language_<language>_to_<model>.rb
42 changes: 42 additions & 0 deletions lib/generators/has_foreign_language_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class HasForeignLanguageGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :lang, :type => :string, :optional => false, :desc => 'Language identifier'
argument :name, :type => :string, :optional => true, :desc => 'Model name'

def generate_migration
if list_of_models.size > 0
template "migration.rb", "db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_#{migration_name.underscore}.rb"
else
puts "No need to migrate!"
end
end

private

def list_of_models(force = false)
all_models = Dir.glob( File.join( Rails.root, 'app', 'models', '*.rb') ).map { |path| path[/.+\/(.+).rb/,1] }
all_models = all_models.select { |m| m.classify == name } if !name.blank?
ar_models = all_models.select { |m| m.classify.constantize < ActiveRecord::Base }
ar_models = ar_models.select { |m| list_of_columns(m, force).length > 0 }
ar_models
end

def list_of_columns(model, force = false, log = false)
c = []
k = model.classify.constantize
k.instance_methods(false).each do |m|
if m.to_s.match("^has_foreign_language_(.+)\\?$")
if !k.columns_hash[$1].nil?
c << k.columns_hash[$1] if !k.columns_hash.has_key? "#{$1}_#{lang.downcase}" || force
else
puts "Warning: #{k}##{$1} has not been migrated!" if log
end
end
end
c
end

def migration_name
"AddForeignLanguage#{lang.capitalize}To#{list_of_models.map { |m| m.classify }.join('And')}".slice(0, 127)
end
end
24 changes: 24 additions & 0 deletions lib/generators/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class <%= migration_name %> < ActiveRecord::Migration
<% list_of_models.each do |model| -%>
class <%= model.classify %> < ActiveRecord::Base
end
<% end -%>

def self.up
<% list_of_models.each do |model| -%>
change_table(:<%= model.classify.constantize.table_name %>) do |t|
<% list_of_columns(model, false, true).each do |c| -%>
t.<%= c.type %> :<%= "#{c.name}_#{lang}" %><%= ", :null => #{c.null}" %><%= ", :default => '#{c.default}'" unless c.default.blank? %><%= ", :limit => #{c.limit}" unless c.limit.blank? %><%= ", :sql_type => '#{c.sql_type}'" unless c.sql_type.blank? %><%= ", :precision => #{c.precision}" unless c.precision.blank? %><%= ", :scale => #{c.scale}" unless c.scale.blank? %>
<% end -%>
end
<% end -%>
end

def self.down
<% list_of_models.each do |model| -%>
<% list_of_columns(model, false).each do |c| -%>
remove_column :<%= model.classify.constantize.table_name %>, :<%= "#{c.name}_#{lang}" %>
<% end -%><% end -%>
end
end

0 comments on commit 818047a

Please sign in to comment.