Skip to content

Commit 6dd28f7

Browse files
committed
Merge pull request #219 from danielevans/feature/active_model_serializers
Active Model Serializers
2 parents 1e563a3 + e32aae6 commit 6dd28f7

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

README.rdoc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ To annotate just your models, tests, and factories:
8686

8787
To annotate just your models:
8888

89-
annotate --exclude tests,fixtures,factories
89+
annotate --exclude tests,fixtures,factories,serializers
9090

9191
To annotate routes.rb:
9292

9393
annotate --routes
9494

95-
To remove model/test/fixture/factory annotations:
95+
To remove model/test/fixture/factory/serializer annotations:
9696

9797
annotate --delete
9898

@@ -165,6 +165,8 @@ you can do so with a simple environment variable, instead of editing the
165165
Place the annotations at the top (before) or the bottom (after) of any test files
166166
--pr, --position-in-routes [before|after]
167167
Place the annotations at the top (before) or the bottom (after) of the routes.rb file
168+
--ps, --position-in-serializer [before|after]
169+
Place the annotations at the top (before) or the bottom (after) of the serializer files
168170
-r, --routes Annotate routes.rb with the output of 'rake routes'
169171
-v, --version Show the current version of this gem
170172
-m, --show-migration Include the migration version number in the annotation
@@ -174,13 +176,15 @@ you can do so with a simple environment variable, instead of editing the
174176
--ignore-model-subdirects Ignore subdirectories of the models directory
175177
--sort Sort columns alphabetically, rather than in creation order
176178
-R, --require path Additional file to require before loading models, may be used multiple times
177-
-e [tests,fixtures,factories], Do not annotate fixtures, test files, and/or factories
178-
--exclude
179+
-e [tests,fixtures,factories,serializers],
180+
--exclude Do not annotate fixtures, test files, factories, and/or serializers
179181
-f [bare|rdoc|markdown], Render Schema Infomation as plain/RDoc/Markdown
180182
--format
181183
--force Force new annotations even if there are no changes.
184+
--timestamp Include timestamp in (routes) annotation
182185
--trace If unable to annotate a file, print the full stack trace, not just the exception message.
183-
--timestamp Include an updated time in routes.rb
186+
-I, --ignore-columns REGEX don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`
187+
184188

185189

186190
== Sorting

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)