This repository has been archived by the owner on Jul 7, 2019. It is now read-only.
forked from linucs/has_foreign_language
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |