Skip to content

Commit 504c267

Browse files
committed
Annotate conventionally named serializers
1 parent a268860 commit 504c267

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

bin/annotate

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ OptionParser.new do |opts|
3636
"Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)") do |p|
3737
ENV['position'] = p
3838
[
39-
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes'
39+
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes', 'position_in_serializer'
4040
].each do |key|
4141
ENV[key] = p unless(has_set_position[key])
4242
end
@@ -72,6 +72,12 @@ OptionParser.new do |opts|
7272
has_set_position['position_in_routes'] = true
7373
end
7474

75+
opts.on('--ps', '--position-in-serializer [before|after]', ['before', 'after'],
76+
"Place the annotations at the top (before) or the bottom (after) of the serializer files") do |p|
77+
ENV['position_in_serializer'] = p
78+
has_set_position['position_in_serializer'] = true
79+
end
80+
7581
opts.on('-r', '--routes',
7682
"Annotate routes.rb with the output of 'rake routes'") do
7783
target = {
@@ -124,7 +130,7 @@ OptionParser.new do |opts|
124130
end
125131
end
126132

127-
opts.on('-e', '--exclude [tests,fixtures,factories]', Array, "Do not annotate fixtures, test files, and/or factories") do |exclusions|
133+
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
128134
exclusions ||= %w(tests fixtures factories)
129135
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
130136
end

lib/annotate.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ module Annotate
1818
POSITION_OPTIONS=[
1919
:position_in_routes, :position_in_class, :position_in_test,
2020
:position_in_fixture, :position_in_factory, :position,
21+
:position_in_serializer,
2122
]
2223
FLAG_OPTIONS=[
2324
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
2425
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
25-
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace, :timestamp
26+
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
27+
:timestamp, :exclude_serializers
2628
]
2729
OTHER_OPTIONS=[
2830
:model_dir, :ignore_columns

lib/annotate/annotate_models.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ module AnnotateModels
3131
FABRICATORS_TEST_DIR = File.join("test", "fabricators")
3232
FABRICATORS_SPEC_DIR = File.join("spec", "fabricators")
3333

34+
# Serializers https://github.com/rails-api/active_model_serializers
35+
SERIALIZERS_DIR = File.join("app", "serializers")
36+
SERIALIZERS_TEST_DIR = File.join("test", "serializers")
37+
SERIALIZERS_SPEC_DIR = File.join("spec", "serializers")
38+
39+
3440
TEST_PATTERNS = [
3541
File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
3642
File.join(MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"),
@@ -55,6 +61,12 @@ module AnnotateModels
5561
File.join(FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb"),
5662
]
5763

64+
SERIALIZER_PATTERNS = [
65+
File.join(SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"),
66+
File.join(SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"),
67+
File.join(SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb")
68+
]
69+
5870
# Don't show limit (#) on these column types
5971
# Example: show "integer" instead of "integer(4)"
6072
NO_LIMIT_COL_TYPES = ["integer", "boolean"]
@@ -302,25 +314,17 @@ def annotate(klass, file, header, options={})
302314
did_annotate = true
303315
end
304316

305-
unless options[:exclude_tests]
306-
did_annotate = TEST_PATTERNS.
307-
map { |file| resolve_filename(file, model_name, table_name) }.
308-
map { |file| annotate_one_file(file, info, :position_in_test, options_with_position(options, :position_in_test)) }.
309-
detect { |result| result } || did_annotate
310-
end
317+
%w(test fixture factory serializer).each do |key|
318+
exclusion_key = "exclude_#{key.pluralize}".to_sym
319+
patterns_constant = "#{key.upcase}_PATTERNS".to_sym
320+
position_key = "position_in_#{key}".to_sym
311321

312-
unless options[:exclude_fixtures]
313-
did_annotate = FIXTURE_PATTERNS.
314-
map { |file| resolve_filename(file, model_name, table_name) }.
315-
map { |file| annotate_one_file(file, info, :position_in_fixture, options_with_position(options, :position_in_fixture)) }.
316-
detect { |result| result } || did_annotate
317-
end
318-
319-
unless options[:exclude_factories]
320-
did_annotate = FACTORY_PATTERNS.
321-
map { |file| resolve_filename(file, model_name, table_name) }.
322-
map { |file| annotate_one_file(file, info, :position_in_factory, options_with_position(options, :position_in_factory)) }.
323-
detect { |result| result } || did_annotate
322+
unless options[exclusion_key]
323+
did_annotate = self.const_get(patterns_constant).
324+
map { |file| resolve_filename(file, model_name, table_name) }.
325+
map { |file| annotate_one_file(file, info, position_key, options_with_position(options, position_key)) }.
326+
detect { |result| result } || did_annotate
327+
end
324328
end
325329

326330
return did_annotate

0 commit comments

Comments
 (0)