Commit 1d8e273f by Alex Buijs

UTF-8 encoding hack

parent 3fae3291
module AnnotateModels module AnnotateModels
class << self class << self
UTF8 = "-*- encoding: utf-8 -*-"
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info" COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information" PREFIX = "== Schema Information"
...@@ -117,6 +119,7 @@ module AnnotateModels ...@@ -117,6 +119,7 @@ module AnnotateModels
def annotate_one_file(file_name, info_block, options={}) def annotate_one_file(file_name, info_block, options={})
if File.exist?(file_name) if File.exist?(file_name)
old_content = File.read(file_name) old_content = File.read(file_name)
old_content.sub!(/^#.*encoding.*utf-8.*\n/, '')
# Ignore the Schema version line because it changes with each migration # Ignore the Schema version line because it changes with each migration
header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/) header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/)
...@@ -126,12 +129,11 @@ module AnnotateModels ...@@ -126,12 +129,11 @@ module AnnotateModels
if old_header == new_header if old_header == new_header
false false
else else
# Replace the old schema info with the new schema info # Remove old schema info
new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, info_block) old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
# But, if there *was* no old schema info, we simply need to insert it
if new_content == old_content # Write it back
new_content = options[:position] == 'before' ? (info_block + old_content) : (old_content + "\n" + info_block) new_content = options[:position] == 'before' ? (info_block + old_content) : (old_content + "\n" + info_block)
end
File.open(file_name, "wb") { |f| f.puts new_content } File.open(file_name, "wb") { |f| f.puts new_content }
true true
...@@ -249,7 +251,7 @@ module AnnotateModels ...@@ -249,7 +251,7 @@ module AnnotateModels
end end
end end
header = PREFIX.dup header = "#{UTF8.dup}\n# #{PREFIX.dup}"
if options[:include_version] if options[:include_version]
version = ActiveRecord::Migrator.current_version rescue 0 version = ActiveRecord::Migrator.current_version rescue 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment