Skip to content

Instantly share code, notes, and snippets.

@koki-h
Created February 11, 2020 08:03
Show Gist options
  • Save koki-h/e0839be061bcf2d97f61e546e2612334 to your computer and use it in GitHub Desktop.
Save koki-h/e0839be061bcf2d97f61e546e2612334 to your computer and use it in GitHub Desktop.
コメントを利用してマイグレーションファイルからロケール(yaml)を作成
require 'psych' # to_yamlを使うため
def table_name(line)
line.scan(/create_table :(.+), comment: \"(.+)"/)
$LOCALE["ja"]["activerecord"]["models"][$1] = $2
$LOCALE["ja"]["activerecord"]["attributes"][$1] = {}
$1
end
def column_name(line)
line.scan(/:(.+?),.+comment: \"(.+)\"/)
$LOCALE["ja"]["activerecord"]["attributes"][@t][$1] = $2
end
def timestamps
$LOCALE["ja"]["activerecord"]["attributes"][@t]["created_at"] = "作成日時"
$LOCALE["ja"]["activerecord"]["attributes"][@t]["updated_at"] = "更新日時"
end
$LOCALE = {
"ja" => {
"activerecord" => {
"attributes"=> {},
"models" => {},
}
}
}
files = Dir.glob("db/migrate/*_create_*.rb")
files.each do |file|
migration = open(file).read
column_flg = false
migration.each_line do |line|
case line
when /create_table/
@t = table_name(line)
column_flg = true
when /end/
column_flg = false
when /timestamps/
timestamps if column_flg
else
column_name(line) if column_flg
end
end
end
puts $LOCALE.to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment