Module: AnnotateModels

Defined in:
lib/annotate/annotate_models.rb

Constant Summary collapse

COMPAT_PREFIX =

Annotate Models plugin use this header

"== Schema Info"
PREFIX =
"== Schema Information"
MODEL_DIR =
"app/models"
FIXTURE_DIRS =
["test/fixtures","spec/fixtures"]
UNIT_TEST_DIR =

File.join for windows reverse bar compat? I dont use windows, can`t test

File.join("test", "unit"  )
SPEC_MODEL_DIR =
File.join("spec", "models")
EXEMPLARS_DIR =
File.join("spec", "exemplars")

Class Method Summary collapse

Class Method Details

.annotate(klass, file, header, options = {}) ⇒ Object

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 of the model and fixture source files. Returns true or false depending on whether the source files were modified.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/annotate/annotate_models.rb', line 118

def annotate(klass, file, header,options={})
  info = get_schema_info(klass, header)
  annotated = false
  model_name = klass.name.underscore
  model_file_name = File.join(MODEL_DIR, file)
  if annotate_one_file(model_file_name, info, options.merge(
          :position=>(options[:position_in_class] || options[:position])))
    annotated = true
  end

  [
    File.join(UNIT_TEST_DIR,      "#{model_name}_test.rb"), # test
    File.join(SPEC_MODEL_DIR,     "#{model_name}_spec.rb"), # spec
    File.join(EXEMPLARS_DIR,      "#{model_name}_exemplar.rb"),   # Object Daddy     
  ].each { |file| annotate_one_file(file, info) }

  FIXTURE_DIRS.each do |dir|
    fixture_file_name = File.join(dir,klass.table_name + ".yml")
    annotate_one_file(fixture_file_name, info, options.merge(:position=>(options[:position_in_fixture] || options[:position]))) if File.exist?(fixture_file_name)
  end
  annotated
end

.annotate_one_file(file_name, info_block, options = {}) ⇒ Object

Add a schema block to a file. If the file already contains a schema info block (a comment starting with “== Schema Information”), check if it matches the block that is already there. If so, leave it be. If not, remove the old info block and write a new one. Returns true or false depending on whether the file was modified.

Options (opts)

:position<Symbol>:: where to place the annotated section in fixture or model file, 
                    "before" or "after". Default is "before".
:position_in_class<Symbol>:: where to place the annotated section in model file
:position_in_fixture<Symbol>:: where to place the annotated section in fixture file


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/annotate/annotate_models.rb', line 77

def annotate_one_file(file_name, info_block, options={})
  if File.exist?(file_name)
    old_content = File.read(file_name)

    # Ignore the Schema version line because it changes with each migration
    header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/)
    old_header = old_content.match(header).to_s
    new_header = info_block.match(header).to_s
    
    if old_header == new_header
      false
    else
      # Remove old schema info
      old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')

      # Write it back
      new_content = ((options[:position] || :before).to_sym == :before) ?  (info_block + old_content) : (old_content + "\n" + info_block)

      File.open(file_name, "wb") { |f| f.puts new_content }
      true
    end
  end
end

.do_annotations(options = {}) ⇒ Object

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/annotate/annotate_models.rb', line 169

def do_annotations(options={})
  header = PREFIX.dup

  if options[:include_version]
    version = ActiveRecord::Migrator.current_version rescue 0
    if version > 0
      header << "\n# Schema version: #{version}"
    end        
  end

  annotated = []
  get_model_files.each do |file|
    begin
      klass = get_model_class(file)
      if klass < ActiveRecord::Base && !klass.abstract_class?
        if annotate(klass, file, header,options)
          annotated << klass
        end
      end
    rescue Exception => e
      puts "Unable to annotate #{file}: #{e.message} (#{e.backtrace.first})"
    end
  end
  if annotated.empty?
    puts "Nothing annotated!"
  else
    puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
  end
end

.get_model_class(file) ⇒ Object

Retrieve the classes belonging to the model names we’re asked to process Check for namespaced models in subdirectories as well as models in subdirectories without namespacing.



161
162
163
# File 'lib/annotate/annotate_models.rb', line 161

def get_model_class(file)
  file.gsub(/\.rb$/, '').camelize.constantize
end

.get_model_filesObject

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 app/models directory.



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/annotate/annotate_models.rb', line 146

def get_model_files
  models = ARGV.dup
  models.shift
  models.reject!{|m| m.starts_with?("position=")}
  if models.empty?
    Dir.chdir(MODEL_DIR) do
      models = Dir["**/*.rb"]
    end
  end
  models
end

.get_schema_info(klass, header) ⇒ Object

Use the column information in an ActiveRecord class to create a comment block containing a line for each column. The line contains the column name, the type (and length), and any optional attributes



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/annotate/annotate_models.rb', line 35

def get_schema_info(klass, header)
  info = "# #{header}\n#\n"
  info << "# Table name: #{klass.table_name}\n#\n"

  max_size = klass.column_names.collect{|name| name.size}.max + 1
  klass.columns.each do |col|
    attrs = []
    attrs << "default(#{quote(col.default)})" if col.default
    attrs << "not null" unless col.null
    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
   
    # Check out if we got a geometric column
    # and print the type and SRID
    if col.respond_to?(:geometry_type)
      attrs << "#{col.geometry_type}, #{col.srid}"
    end  
    
    info << sprintf("#  %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
  end

  info << "#\n\n"
end

.quote(value) ⇒ Object

Simple quoting for the default column value



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/annotate/annotate_models.rb', line 18

def quote(value)
  case value
    when NilClass                 then "NULL"
    when TrueClass                then "TRUE"
    when FalseClass               then "FALSE"
    when Float, Fixnum, Bignum    then value.to_s
    # BigDecimals need to be output in a non-normalized form and quoted.
    when BigDecimal               then value.to_s('F')
    else
      value.inspect
  end
end

.remove_annotation_of_file(file_name) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/annotate/annotate_models.rb', line 101

def remove_annotation_of_file(file_name)
  if File.exist?(file_name)
    content = File.read(file_name)

    content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
    
    File.open(file_name, "wb") { |f| f.puts content }
  end
end

.remove_annotationsObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/annotate/annotate_models.rb', line 199

def remove_annotations
  deannotated = []
  get_model_files.each do |file|
    begin
      klass = get_model_class(file)
      if klass < ActiveRecord::Base && !klass.abstract_class?
        deannotated << klass
        
        model_file_name = File.join(MODEL_DIR, file)
        remove_annotation_of_file(model_file_name)
        
        FIXTURE_DIRS.each do |dir|
          fixture_file_name = File.join(dir,klass.table_name + ".yml")
          remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
        end
      end
    rescue Exception => e
      puts "Unable to annotate #{file}: #{e.message}"
    end
  end
  puts "Removed annotation from: #{deannotated.join(', ')}"
end