diff --git a/ChangeLog b/ChangeLog index 6c810f7..884fe90 100755 --- a/ChangeLog +++ b/ChangeLog @@ -1,38 +1,41 @@ -2008-10-01: Marcos Piccinini +2008-10-02 Marcos Piccinini + * annotates object daddy exemplar files. + +2008-10-01 Marcos Piccinini * annotates factory file. -2007-03-05: Dave Thomas +2007-03-05 Dave Thomas * Forgot to call the quote method - -2007-03-02: Dave Thomas + +2007-03-02 Dave Thomas * Allow non-printing characters in column defaults (suggested by Ben Booth) -2007-02-28: Dave Thomas +2007-02-28 Dave Thomas * Report errors loading model classes better. Change suggested by Niels Knacke - -2007-02-22: Dave Thomas + +2007-02-22 Dave Thomas * Ignore models with no underlying database table (based on patch from Jamie van Dyke) * Handle case where database has no session_info table (patch by David Vrensk) - -2006-07-13: Dave Thomas + +2006-07-13 Dave Thomas * Support :scale for decimal columns - -2006-07-13: Wes Gamble + +2006-07-10 Wes Gamble * Don't annotate abstract models - -2006-06-13: Dave Thomas + +2006-06-13 Dave Thomas * Fix bug where we corrupted the PREFIX string and therefore duplicated the header * No longer include the datetime, so we don't trigger a commit back into repos - + -- NOTE -- just this once, you'll get a duplicate header after you run a_m on an already-annotated model. Sorry.... Dave - + 2006-06-11 Dave Thomas * lib/annotate_models.rb: At Kian Wright's suggestion, document the table name and primary key. Also make the timestamp prettier - + 2006-04-17 Dave Thomas * lib/annnotate_models.rb: Include Bruce William's patch to allow @@ -43,4 +46,4 @@ * lib/annotate_models.rb: Use camelize, not classify, to construct class names (Grant Hollingworth) -3/3/06 Now annotates fixture files too (thanks to Josha Susser) \ No newline at end of file +3/3/06 Now annotates fixture files too (thanks to Josha Susser) diff --git a/README.txt b/README.txt index 8be1e5d..0fdf076 100755 --- a/README.txt +++ b/README.txt @@ -3,8 +3,8 @@ ( |/)/)()/(//(- / |()(/(-(_) ________________________________ -Add a comment summarizing the current schema to the -bottom of each ActiveRecord model, Test File, +Add a comment summarizing the current schema to the +bottom of each ActiveRecord model, Test File, Exemplar, Fixture and Factory source file: # == Schema Info @@ -15,50 +15,51 @@ Exemplar, Fixture and Factory source file: # id :integer(11) not null, primary key # quantity :integer(11) not null # product_id :integer(11) not null - # unit_price :float - # order_id :integer(11) + # unit_price :float + # order_id :integer(11) # - class LineItem < ActiveRecord::Base - belongs_to :product + class LineItem < ActiveRecord::Base + belongs_to :product . . . - -Note that this code will blow away the initial/final comment block in your models -if it looks like it was previously added by annotate models, so you don't want to -add additional text to an automatically created comment block. - * * Back up your model files before using... * * +Note that this code will blow away the initial/final comment +block in your models if it looks like it was previously added +by annotate models, so you don't want to add additional text +to an automatically created comment block. + + * * Back up your model files before using... * * == HOW TO USE: -To annotate all your models and factory.rb (if present on spec/ or test/): +To annotate all your models: rake db:annotate - + To migrate & annotate: rake db:update - - + + Options: Annotate on the head of the file: rake db:annotate POSITION='top' - + == LICENSE: Author: Dave Thomas Pragmatic Programmers, LLC - + This fork: Marcos Piccinini http://github.com/nofxx/annotate_models - + Forked from: Rotuka http://github.com/rotuka/annotate_models - -Released under the same license as Ruby. No Support. No Warranty. \ No newline at end of file + +Released under the same license as Ruby. No Support. No Warranty. diff --git a/lib/annotate_models.rb b/lib/annotate_models.rb index ffd15a9..155fe65 100755 --- a/lib/annotate_models.rb +++ b/lib/annotate_models.rb @@ -5,14 +5,16 @@ SPEC_MODEL_DIR = File.join(RAILS_ROOT, "spec/models") FIXTURES_DIR = File.join(RAILS_ROOT, "test/fixtures") SPEC_FIXTURES_DIR = File.join(RAILS_ROOT, "spec/fixtures") -FACTORY_FILE = File.join(RAILS_ROOT, "spec/factory.rb") || File.join(RAILS_ROOT, "test/factory.rb") EXEMPLARS_DIR = File.join(RAILS_ROOT, "spec/exemplars") SORT_COLUMNS = ENV['SORT'] ? ENV['SORT'] != 'no' : true +FACTORY_FILE = File. + join(RAILS_ROOT, "spec/factory.rb") || File. + join(RAILS_ROOT, "test/factory.rb") rescue nil module AnnotateModels PREFIX = "== Schema Info" - + # Simple quoting for the default column value def self.quote(value) case value @@ -33,7 +35,7 @@ def self.quote(value) # the type (and length), and any optional attributes def self.get_schema_info(klass, header) info = "# Table name: #{klass.table_name}\n#\n" - + max_size = klass.column_names.collect{|name| name.size}.max + 1 if SORT_COLUMNS pk = klass.columns.find_all { |col| col.name == klass.primary_key }.flatten @@ -49,19 +51,19 @@ def self.get_schema_info(klass, header) info << "\n" info = "# #{header}\n#\n" + info end - + def self.annotate_column(col, klass, max_size) attrs = [] - attrs << "not null" unless col.null - attrs << "default(#{quote(col.default)})" if col.default + attrs << "not null" unless col.null + attrs << "default(#{quote(col.default)})" if col.default attrs << "primary key" if col.name == klass.primary_key - + col_type = col.type.to_s if col_type == "decimal" col_type << "(#{col.precision}, #{col.scale})" else col_type << "(#{col.limit})" if col.limit - end + end sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip << "\n" end @@ -76,14 +78,14 @@ def self.annotate_one_file(file_name, info_block) # Remove old schema info content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '') - # Write it back + # Write it back - File.open(file_name, "w") do |f| - f.puts ENV['POSITION'] == 'top' ? info_block + content : content + info_block + File.open(file_name, "w") do |f| + f.puts ENV['POSITION'] == 'top' ? info_block + content : content + info_block end end end - + # Given the name of an ActiveRecord class, create a schema # info block (basically a comment containing information # on the columns and their types) and put it at the front @@ -100,27 +102,27 @@ def self.annotate(klass, header) File.join(FIXTURES_DIR, fixtures_name), # fixture File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec File.join(SPEC_FIXTURES_DIR, fixtures_name), # spec fixture - File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb"), + File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb"), ].each { |file| annotate_one_file(file, info) } end - # Return a list of the model files to annotate. If we have + # Return a list of the model files to annotate. If we have # command line arguments, they're assumed to be either # the underscore or CamelCase versions of model names. - # Otherwise we take all the model files in the + # Otherwise we take all the model files in the # app/models directory. def self.get_model_names models = ENV['MODELS'] ? ENV['MODELS'].split(',') : [] - + if models.empty? - Dir.chdir(MODEL_DIR) do + Dir.chdir(MODEL_DIR) do models = Dir["**/*.rb"] end end models end - # We're passed a name of things that might be + # We're passed a name of things that might be # ActiveRecord models. If we can find the class, and # if its a subclass of ActiveRecord::Base, # then pas it to the associated block @@ -144,24 +146,24 @@ def self.do_annotations write_factory if FACTORY_FILE puts "Annotated #{annotated.join(', ')}" end - + def self.get_schema_version version = ActiveRecord::Migrator.current_version rescue 0 version > 0 ? "\n# Schema version: #{version}" : '' end - + def self.anotate_factory(info) @all ||= [] @all << "#\n# = - - - - - - - - - -\n#\n#{info}" end - + def self.write_factory content = File.read(FACTORY_FILE) prefix = '== Annotate Models:' @all = "# #{prefix}\n##{get_schema_version}\n#{@all}\n" content.sub!(/^# #{prefix}\n(#.*\n)*/, '') - File.open(FACTORY_FILE, "w") do |f| + File.open(FACTORY_FILE, "w") do |f| f.puts ENV['POSITION'] == 'top' ? @all + content : content + @all end end -end \ No newline at end of file +end