Skip to content

Commit c680745

Browse files
committed
Add frozen option
1 parent 6337db0 commit c680745

File tree

7 files changed

+32
-2
lines changed

7 files changed

+32
-2
lines changed

README.rdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ you can do so with a simple environment variable, instead of editing the
203203
-f [bare|rdoc|markdown], Render Schema Infomation as plain/RDoc/Markdown
204204
--format
205205
--force Force new annotations even if there are no changes.
206+
--frozen Do not allow to change annotations. Exits non-zero if there are going to be changes to files.
206207
--timestamp Include timestamp in (routes) annotation
207208
--trace If unable to annotate a file, print the full stack trace, not just the exception message.
208209
-I, --ignore-columns REGEX don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`

bin/annotate

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ has_set_position = {}
2222
target_action = :do_annotations
2323
positions = %w(before top after bottom)
2424

25+
# rubocop:disable Metrics/BlockLength
2526
OptionParser.new do |opts|
2627
opts.banner = 'Usage: annotate [options] [model_file]*'
2728

@@ -170,6 +171,10 @@ OptionParser.new do |opts|
170171
ENV['force'] = 'yes'
171172
end
172173

174+
opts.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
175+
ENV['frozen'] = 'yes'
176+
end
177+
173178
opts.on('--timestamp', 'Include timestamp in (routes) annotation') do
174179
ENV['timestamp'] = 'true'
175180
end
@@ -202,6 +207,7 @@ OptionParser.new do |opts|
202207
ENV['with_comment'] = 'true'
203208
end
204209
end.parse!
210+
# rubocop:enable Metrics/BlockLength
205211

206212
options = Annotate.setup_options(
207213
is_rake: ENV['is_rake'] && !ENV['is_rake'].empty?

lib/annotate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ module Annotate
2929
FLAG_OPTIONS = [
3030
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
3131
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
32-
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
33-
:timestamp, :exclude_serializers, :classified_sort,
32+
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen,
33+
:trace, :timestamp, :exclude_serializers, :classified_sort,
3434
:show_foreign_keys, :show_complete_foreign_keys,
3535
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
3636
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment

lib/annotate/annotate_models.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ def annotate_one_file(file_name, info_block, position, options = {})
513513

514514
return false if old_columns == new_columns && !options[:force]
515515

516+
abort "annotate error. #{file_name} needs to be updated, but annotate was run with `--frozen`." if options[:frozen]
517+
516518
# Replace inline the old schema info with the new schema info
517519
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
518520
wrapper_close = options[:wrapper_close] ? "# #{options[:wrapper_close]}\n" : ""

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ if Rails.env.development?
4242
'format_markdown' => 'false',
4343
'sort' => 'false',
4444
'force' => 'false',
45+
'frozen' => 'false',
4546
'classified_sort' => 'true',
4647
'trace' => 'false',
4748
'wrapper_open' => nil,

lib/tasks/annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ task annotate_models: :environment do
3939
options[:format_markdown] = Annotate.true?(ENV['format_markdown'])
4040
options[:sort] = Annotate.true?(ENV['sort'])
4141
options[:force] = Annotate.true?(ENV['force'])
42+
options[:frozen] = Annotate.true?(ENV['frozen'])
4243
options[:classified_sort] = Annotate.true?(ENV['classified_sort'])
4344
options[:trace] = Annotate.true?(ENV['trace'])
4445
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])

spec/annotate/annotate_models_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,25 @@ class User < ActiveRecord::Base
17681768
expect(error_output).to include("/user.rb:2:in `<class:User>'")
17691769
end
17701770
end
1771+
1772+
describe 'frozen option' do
1773+
it "should abort without existing annotation when frozen: true " do
1774+
expect { annotate_one_file frozen: true }.to raise_error SystemExit, /user.rb needs to be updated, but annotate was run with `--frozen`./
1775+
end
1776+
1777+
it "should abort with different annotation when frozen: true " do
1778+
annotate_one_file
1779+
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer)]), '== Schema Info')
1780+
@schema_info = another_schema_info
1781+
1782+
expect { annotate_one_file frozen: true }.to raise_error SystemExit, /user.rb needs to be updated, but annotate was run with `--frozen`./
1783+
end
1784+
1785+
it "should NOT abort with same annotation when frozen: true " do
1786+
annotate_one_file
1787+
expect { annotate_one_file frozen: true }.not_to raise_error
1788+
end
1789+
end
17711790
end
17721791

17731792
describe '.annotate_model_file' do

0 commit comments

Comments
 (0)