Commit 3ec604a1 by Dmitry Lihachev

translate model and attributes if possible

parent ace84ab4
...@@ -44,10 +44,12 @@ module AnnotateModels ...@@ -44,10 +44,12 @@ module AnnotateModels
def get_schema_info(klass, header, options = {}) def get_schema_info(klass, header, options = {})
info = "# #{header}\n#\n" info = "# #{header}\n#\n"
info << "# Table name: #{klass.table_name}\n#\n" info << "# Table name: #{klass.table_name}\n#\n"
info << "# Human name: #{klass.model_name.human}\n" unless klass.model_name.human(:default => "").blank?
max_size = klass.column_names.collect{|name| name.size}.max + 1 max_size = klass.column_names.collect{|name| name.size}.max + 1
klass.columns.each do |col| klass.columns.each do |col|
attrs = [] attrs = []
attrs << "'#{klass.human_attribute_name(col.name)}'" unless klass.human_attribute_name(col.name, :default => "").blank?
attrs << "default(#{quote(col.default)})" unless col.default.nil? attrs << "default(#{quote(col.default)})" unless col.default.nil?
attrs << "not null" unless col.null attrs << "not null" unless col.null
attrs << "primary key" if col.name == klass.primary_key attrs << "primary key" if col.name == klass.primary_key
...@@ -163,12 +165,12 @@ module AnnotateModels ...@@ -163,12 +165,12 @@ module AnnotateModels
if annotate_one_file(model_file_name, info, options_with_position(options, :position_in_class)) if annotate_one_file(model_file_name, info, options_with_position(options, :position_in_class))
annotated = true annotated = true
end end
unless ENV['exclude_tests'] unless ENV['exclude_tests']
[ [
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
].each do |file| ].each do |file|
# todo: add an option "position_in_test" -- or maybe just ask if anyone ever wants different positions for model vs. test vs. fixture # todo: add an option "position_in_test" -- or maybe just ask if anyone ever wants different positions for model vs. test vs. fixture
annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
end end
...@@ -179,21 +181,21 @@ module AnnotateModels ...@@ -179,21 +181,21 @@ module AnnotateModels
File.join(EXEMPLARS_TEST_DIR, "#{model_name}_exemplar.rb"), # Object Daddy File.join(EXEMPLARS_TEST_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
File.join(EXEMPLARS_SPEC_DIR, "#{model_name}_exemplar.rb"), # Object Daddy File.join(EXEMPLARS_SPEC_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
File.join(BLUEPRINTS_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints File.join(BLUEPRINTS_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints
].each do |file| ].each do |file|
annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
end end
FIXTURE_DIRS.each do |dir| FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir,klass.table_name + ".yml") fixture_file_name = File.join(dir,klass.table_name + ".yml")
if File.exist?(fixture_file_name) if File.exist?(fixture_file_name)
annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture)) annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture))
end end
end end
end end
annotated annotated
end end
# position = :position_in_fixture or :position_in_class # position = :position_in_fixture or :position_in_class
def options_with_position(options, position_in) def options_with_position(options, position_in)
options.merge(:position=>(options[position_in] || options[:position])) options.merge(:position=>(options[position_in] || options[:position]))
...@@ -274,7 +276,7 @@ module AnnotateModels ...@@ -274,7 +276,7 @@ module AnnotateModels
puts "Unable to annotate #{file}: #{e.inspect}" puts "Unable to annotate #{file}: #{e.inspect}"
puts "" puts ""
# todo: check if all backtrace lines are in "gems" -- if so, it's an annotate bug, so print the whole stack trace. # todo: check if all backtrace lines are in "gems" -- if so, it's an annotate bug, so print the whole stack trace.
# puts e.backtrace.join("\n\t") # puts e.backtrace.join("\n\t")
end end
end end
if annotated.empty? if annotated.empty?
...@@ -303,12 +305,12 @@ module AnnotateModels ...@@ -303,12 +305,12 @@ module AnnotateModels
fixture_file_name = File.join(dir,klass.table_name + ".yml") fixture_file_name = File.join(dir,klass.table_name + ".yml")
remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name) remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
end end
[ File.join(UNIT_TEST_DIR, "#{klass.name.underscore}_test.rb"), [ File.join(UNIT_TEST_DIR, "#{klass.name.underscore}_test.rb"),
File.join(SPEC_MODEL_DIR,"#{klass.name.underscore}_spec.rb")].each do |file| File.join(SPEC_MODEL_DIR,"#{klass.name.underscore}_spec.rb")].each do |file|
remove_annotation_of_file(file) if File.exist?(file) remove_annotation_of_file(file) if File.exist?(file)
end end
end end
rescue Exception => e rescue Exception => e
puts "Unable to annotate #{file}: #{e.message}" puts "Unable to annotate #{file}: #{e.message}"
......
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