Skip to content

Commit ed327d5

Browse files
author
Cuong Tran
committed
Merge branch 'release/2.6.2'
* release/2.6.2: v2.6.2 Add known changes for 2.6.2. Retain existing position unless :force is passed Temporarily ignore integration tests Revert "Replacing old block with new one." Change email address Add .travis.yml switch deprecated "mock" for "double" in annotate specs use general file resolution for test files as well Replacing old block with new one. Fixed gemspec files attribute migrate.rake should live in lib/tasks Always load models, since they may not be autoloaded by Rails Handle the "--exclude" option correctly, #152 and #160 FIX #157 Pump version to 2.6.1 which fixes #22
2 parents eb633bc + 52181b3 commit ed327d5

11 files changed

+99
-64
lines changed

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: ruby
2+
rvm:
3+
- 1.9.3
4+
5+
before_install:
6+
- rvm @global do gem install bundler

CHANGELOG.rdoc

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
== 2.6.2
2+
* Retain the current annotate block unless --force is specified
3+
* Always load models, since they may not be autoloaded by Rails
4+
* The pg array type is now detected (see #158)
5+
16
== 2.6.0.beta2
27

38
* support for composite_primary_keys (garysweaver)

Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ namespace :gem do
4444
gem.license = "Ruby"
4545
gem.summary = %q{Annotates Rails Models, routes, fixtures, and others based on the database schema.}
4646
gem.description = %q{Annotates Rails/ActiveRecord Models, routes, fixtures, and others based on the database schema.}
47-
gem.email = ["[email protected]", "ctran@pragmaquest.com", "[email protected]", "[email protected]", "[email protected]"]
48-
gem.authors = ["Cuong Tran", "Alex Chaffee", "Marcos Piccinini", "Turadg Aleahmad", "Jon Frisby"]
47+
gem.email = ["[email protected]", "cuong@gmail.com", "[email protected]", "[email protected]", "[email protected]"]
48+
gem.authors = ["Alex Chaffee", "Cuong Tran", "Marcos Piccinini", "Turadg Aleahmad", "Jon Frisby"]
4949
gem.require_paths = ["lib"]
5050
# gem.rdoc_options = ["--charset=UTF-8"]
5151
# gem.required_ruby_version = "> 1.9.2"

annotate.gemspec

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
# This file is auto-generated!
2-
# DO NOT EDIT THIS FILE DIRECTLY!
3-
# Instead, edit the Rakefile and run 'rake gems:gemspec'.# -*- encoding: utf-8 -*-
1+
# -*- encoding: utf-8 -*-
42
# stub: annotate 2.6.1 ruby lib
53

64
Gem::Specification.new do |s|
75
s.name = "annotate"
8-
s.version = "2.6.1"
6+
s.version = "2.6.2"
97

108
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
119
s.authors = ["Cuong Tran", "Alex Chaffee", "Marcos Piccinini", "Turadg Aleahmad", "Jon Frisby"]
12-
s.date = "2013-12-21"
10+
s.date = "2014-02-26"
1311
s.description = "Annotates Rails/ActiveRecord Models, routes, fixtures, and others based on the database schema."
14-
s.email = ["[email protected]", "ctran@pragmaquest.com", "[email protected]", "[email protected]", "[email protected]"]
12+
s.email = ["[email protected]", "cuong.tran@gmail.com", "[email protected]", "[email protected]", "[email protected]"]
1513
s.executables = ["annotate"]
1614
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "TODO.rdoc"]
17-
s.files = ["AUTHORS.rdoc", "CHANGELOG.rdoc", "LICENSE.txt", "README.rdoc", "TODO.rdoc", "annotate.gemspec", "bin/annotate", "lib/annotate.rb", "lib/annotate/active_record_patch.rb", "lib/annotate/annotate_models.rb", "lib/annotate/annotate_routes.rb", "lib/annotate/tasks.rb", "lib/annotate/version.rb", "lib/generators/annotate/USAGE", "lib/generators/annotate/install_generator.rb", "lib/generators/annotate/templates/auto_annotate_models.rake", "lib/tasks/annotate_models.rake", "lib/tasks/annotate_routes.rake", "tasks/migrate.rake"]
15+
s.files = ["AUTHORS.rdoc", "CHANGELOG.rdoc", "LICENSE.txt", "README.rdoc", "TODO.rdoc", "annotate.gemspec", "bin/annotate", "lib/annotate.rb", "lib/annotate/active_record_patch.rb", "lib/annotate/annotate_models.rb", "lib/annotate/annotate_routes.rb", "lib/annotate/tasks.rb", "lib/annotate/version.rb", "lib/generators/annotate/USAGE", "lib/generators/annotate/install_generator.rb", "lib/generators/annotate/templates/auto_annotate_models.rake", "lib/tasks/annotate_models.rake", "lib/tasks/annotate_routes.rake", "lib/tasks/migrate.rake"]
1816
s.homepage = "http://github.com/ctran/annotate_models"
1917
s.licenses = ["Ruby"]
2018
s.require_paths = ["lib"]

bin/annotate

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ OptionParser.new do |opts|
119119
end
120120
end
121121

122-
opts.on('-e', '--exclude [DIRS]', ['tests','fixtures','factories'], "Do not annotate fixtures, test files, and/or factories") do |exclusions|
123-
exclusions ||= %w(tests fixtures factories)
122+
opts.on('-e', '--exclude [tests,fixtures,factories]', Array, "Do not annotate fixtures, test files, and/or factories") do |exclusions|
123+
exclusions ||= %w(tests fixtures factories)
124124
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
125125
end
126126

lib/annotate/annotate_models.rb

+26-39
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ module AnnotateModels
3131
FABRICATORS_SPEC_DIR = File.join("spec", "fabricators")
3232

3333
TEST_PATTERNS = [
34-
[UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"],
35-
[SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"],
34+
File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
35+
File.join(SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"),
3636
]
3737

3838
FIXTURE_PATTERNS = [
@@ -131,6 +131,11 @@ def get_schema_info(klass, header, options = {})
131131
end
132132
end
133133

134+
# Check out if we got an array column
135+
if col.respond_to?(:array) && col.array
136+
attrs << "is an Array"
137+
end
138+
134139
# Check out if we got a geometric column
135140
# and print the type and SRID
136141
if col.respond_to?(:geometry_type)
@@ -227,27 +232,23 @@ def annotate_one_file(file_name, info_block, position, options={})
227232
if old_columns == new_columns && !options[:force]
228233
return false
229234
else
235+
# Replace inline the old schema info with the new schema info
236+
new_content = old_content.sub(PATTERN, info_block + "\n")
230237

231-
# todo: figure out if we need to extract any logic from this merge chunk
232-
# <<<<<<< HEAD
233-
# # Replace the old schema info with the new schema info
234-
# new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n*/, info_block)
235-
# # But, if there *was* no old schema info, we simply need to insert it
236-
# if new_content == old_content
237-
# old_content.sub!(encoding, '')
238-
# new_content = options[:position] == 'after' ?
239-
# (encoding_header + (old_content =~ /\n$/ ? old_content : old_content + "\n") + info_block) :
240-
# (encoding_header + info_block + old_content)
241-
# end
242-
# =======
243-
244-
# Strip the old schema info, and insert new schema info.
245-
old_content.sub!(encoding, '')
246-
old_content.sub!(PATTERN, '')
247-
248-
new_content = options[position].to_s == 'after' ?
249-
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
250-
(encoding_header + info_block + "\n" + old_content)
238+
if new_content.end_with? (info_block + "\n")
239+
new_content = old_content.sub(PATTERN, "\n" + info_block)
240+
end
241+
242+
# if there *was* no old schema info (no substitution happened) or :force was passed,
243+
# we simply need to insert it in correct position
244+
if new_content == old_content || options[:force]
245+
old_content.sub!(encoding, '')
246+
old_content.sub!(PATTERN, '')
247+
248+
new_content = options[position].to_s == 'after' ?
249+
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
250+
(encoding_header + info_block + "\n" + old_content)
251+
end
251252

252253
File.open(file_name, "wb") { |f| f.puts new_content }
253254
return true
@@ -301,8 +302,7 @@ def annotate(klass, file, header, options={})
301302

302303
unless options[:exclude_tests]
303304
did_annotate = TEST_PATTERNS.
304-
map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)] }.
305-
map { |pat| find_test_file(*pat) }.
305+
map { |file| resolve_filename(file, model_name, table_name) }.
306306
map { |file| annotate_one_file(file, info, :position_in_test, options_with_position(options, :position_in_test)) }.
307307
detect { |result| result } || did_annotate
308308
end
@@ -370,7 +370,7 @@ def get_model_files(options)
370370
# in subdirectories without namespacing.
371371
def get_model_class(file)
372372
# this is for non-rails projects, which don't get Rails auto-require magic
373-
require File.expand_path("#{model_dir}/#{file}") unless Module.const_defined?(:Rails)
373+
require File.expand_path("#{model_dir}/#{file}")
374374
model_path = file.gsub(/\.rb$/, '')
375375
get_loaded_model(model_path) || get_loaded_model(model_path.split('/').last)
376376
end
@@ -441,16 +441,7 @@ def remove_annotations(options={})
441441
model_file_name = File.join(model_dir, file)
442442
deannotated_klass = true if(remove_annotation_of_file(model_file_name))
443443

444-
TEST_PATTERNS.
445-
map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)]}.
446-
map { |pat| find_test_file(*pat) }.each do |file|
447-
if(File.exist?(file))
448-
remove_annotation_of_file(file)
449-
deannotated_klass = true
450-
end
451-
end
452-
453-
(FIXTURE_PATTERNS + FACTORY_PATTERNS).
444+
(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS).
454445
map { |file| resolve_filename(file, model_name, table_name) }.
455446
each do |file|
456447
if File.exist?(file)
@@ -468,10 +459,6 @@ def remove_annotations(options={})
468459
puts "Removed annotations from: #{deannotated.join(', ')}"
469460
end
470461

471-
def find_test_file(dir, file_name)
472-
Dir.glob(File.join(dir, "**", file_name)).first || File.join(dir, file_name)
473-
end
474-
475462
def resolve_filename(filename_template, model_name, table_name)
476463
return filename_template.
477464
gsub('%MODEL_NAME%', model_name).

lib/annotate/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Annotate
22
def self.version
3-
'2.6.1'
3+
'2.6.2'
44
end
55
end
File renamed without changes.

spec/annotate/annotate_models_spec.rb

+48-12
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
describe AnnotateModels do
77
def mock_class(table_name, primary_key, columns)
88
options = {
9-
:connection => mock("Conn", :indexes => []),
9+
:connection => double("Conn", :indexes => []),
1010
:table_name => table_name,
1111
:primary_key => primary_key,
1212
:column_names => columns.map { |col| col.name.to_s },
1313
:columns => columns
1414
}
1515

16-
mock("An ActiveRecord class", options)
16+
double("An ActiveRecord class", options)
1717
end
1818

1919
def mock_column(name, type, options={})
@@ -27,7 +27,7 @@ def mock_column(name, type, options={})
2727
stubs.merge!(options)
2828
stubs.merge!(:name => name, :type => type)
2929

30-
mock("Column", stubs)
30+
double("Column", stubs)
3131
end
3232

3333
it { AnnotateModels.quote(nil).should eql("NULL") }
@@ -368,31 +368,67 @@ def encoding_comments_list_each
368368
].each{|encoding_comment| yield encoding_comment }
369369
end
370370

371-
it "should annotate the file before the model if position == 'before'" do
371+
it "should put annotation before class if :position == 'before'" do
372372
annotate_one_file :position => "before"
373373
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
374374
end
375375

376-
it "should annotate before if given :position => :before" do
376+
it "should put annotation before class if :position => :before" do
377377
annotate_one_file :position => :before
378378
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
379379
end
380380

381-
it "should annotate after if given :position => :after" do
381+
it "should put annotation after class if :position => :after" do
382382
annotate_one_file :position => :after
383383
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
384384
end
385385

386-
it "should update annotate position" do
387-
annotate_one_file :position => :before
386+
describe "with existing annotation => :before" do
387+
before do
388+
annotate_one_file :position => :before
389+
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
390+
"== Schema Info")
391+
@schema_info = another_schema_info
392+
end
393+
394+
it "should retain current position" do
395+
annotate_one_file
396+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
397+
end
398+
399+
it "should retain current position even when :position is changed to :after" do
400+
annotate_one_file :position => :after
401+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
402+
end
403+
404+
it "should change position to :after when :force => true" do
405+
annotate_one_file :position => :after, :force => true
406+
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
407+
end
408+
end
388409

389-
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
410+
describe "with existing annotation => :after" do
411+
before do
412+
annotate_one_file :position => :after
413+
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
390414
"== Schema Info")
415+
@schema_info = another_schema_info
416+
end
391417

392-
@schema_info = another_schema_info
393-
annotate_one_file :position => :after
418+
it "should retain current position" do
419+
annotate_one_file
420+
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
421+
end
394422

395-
File.read(@model_file_name).should == "#{@file_content}\n#{another_schema_info}"
423+
it "should retain current position even when :position is changed to :before" do
424+
annotate_one_file :position => :before
425+
File.read(@model_file_name).should == "#{@file_content}\n#{@schema_info}"
426+
end
427+
428+
it "should change position to :before when :force => true" do
429+
annotate_one_file :position => :before, :force => true
430+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
431+
end
396432
end
397433

398434
it 'should skip columns with option[:ignore_columns] set' do

spec/annotate/annotate_routes_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
describe AnnotateRoutes do
55

66
def mock_file(stubs={})
7-
@mock_file ||= mock(File, stubs)
7+
@mock_file ||= double(File, stubs)
88
end
99

1010
it "should check if routes.rb exists" do

spec/integration/integration_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ENV['BUNDLE_GEMFILE'] = './Gemfile'
1717

1818
describe "annotate inside Rails, using #{CURRENT_RUBY}" do
19+
1920
here = File.expand_path('..', __FILE__)
2021
chosen_scenario = nil
2122
if(!ENV['SCENARIO'].blank?)
@@ -33,6 +34,8 @@
3334
# Don't proceed if the working copy is dirty!
3435
Annotate::Integration.is_clean?(test_rig).should == true
3536

37+
pending "temporarily ignored until Travis can run them"
38+
3639
Bundler.with_clean_env do
3740
dir base_dir do
3841
temp_dir = Dir.pwd

0 commit comments

Comments
 (0)