Commit 6dd28f70 by Cuong Tran

Merge pull request #219 from danielevans/feature/active_model_serializers

Active Model Serializers
parents 1e563a3f e32aae62
...@@ -86,13 +86,13 @@ To annotate just your models, tests, and factories: ...@@ -86,13 +86,13 @@ To annotate just your models, tests, and factories:
To annotate just your models: To annotate just your models:
annotate --exclude tests,fixtures,factories annotate --exclude tests,fixtures,factories,serializers
To annotate routes.rb: To annotate routes.rb:
annotate --routes annotate --routes
To remove model/test/fixture/factory annotations: To remove model/test/fixture/factory/serializer annotations:
annotate --delete annotate --delete
...@@ -165,6 +165,8 @@ you can do so with a simple environment variable, instead of editing the ...@@ -165,6 +165,8 @@ you can do so with a simple environment variable, instead of editing the
Place the annotations at the top (before) or the bottom (after) of any test files Place the annotations at the top (before) or the bottom (after) of any test files
--pr, --position-in-routes [before|after] --pr, --position-in-routes [before|after]
Place the annotations at the top (before) or the bottom (after) of the routes.rb file Place the annotations at the top (before) or the bottom (after) of the routes.rb file
--ps, --position-in-serializer [before|after]
Place the annotations at the top (before) or the bottom (after) of the serializer files
-r, --routes Annotate routes.rb with the output of 'rake routes' -r, --routes Annotate routes.rb with the output of 'rake routes'
-v, --version Show the current version of this gem -v, --version Show the current version of this gem
-m, --show-migration Include the migration version number in the annotation -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 ...@@ -174,13 +176,15 @@ you can do so with a simple environment variable, instead of editing the
--ignore-model-subdirects Ignore subdirectories of the models directory --ignore-model-subdirects Ignore subdirectories of the models directory
--sort Sort columns alphabetically, rather than in creation order --sort Sort columns alphabetically, rather than in creation order
-R, --require path Additional file to require before loading models, may be used multiple times -R, --require path Additional file to require before loading models, may be used multiple times
-e [tests,fixtures,factories], Do not annotate fixtures, test files, and/or factories -e [tests,fixtures,factories,serializers],
--exclude --exclude Do not annotate fixtures, test files, factories, and/or serializers
-f [bare|rdoc|markdown], Render Schema Infomation as plain/RDoc/Markdown -f [bare|rdoc|markdown], Render Schema Infomation as plain/RDoc/Markdown
--format --format
--force Force new annotations even if there are no changes. --force Force new annotations even if there are no changes.
--timestamp Include timestamp in (routes) annotation
--trace If unable to annotate a file, print the full stack trace, not just the exception message. --trace If unable to annotate a file, print the full stack trace, not just the exception message.
--timestamp Include an updated time in routes.rb -I, --ignore-columns REGEX don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`
== Sorting == Sorting
......
...@@ -36,7 +36,7 @@ OptionParser.new do |opts| ...@@ -36,7 +36,7 @@ OptionParser.new do |opts|
"Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)") do |p| "Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)") do |p|
ENV['position'] = p ENV['position'] = p
[ [
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes' 'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes', 'position_in_serializer'
].each do |key| ].each do |key|
ENV[key] = p unless(has_set_position[key]) ENV[key] = p unless(has_set_position[key])
end end
...@@ -72,6 +72,12 @@ OptionParser.new do |opts| ...@@ -72,6 +72,12 @@ OptionParser.new do |opts|
has_set_position['position_in_routes'] = true has_set_position['position_in_routes'] = true
end end
opts.on('--ps', '--position-in-serializer [before|after]', ['before', 'after'],
"Place the annotations at the top (before) or the bottom (after) of the serializer files") do |p|
ENV['position_in_serializer'] = p
has_set_position['position_in_serializer'] = true
end
opts.on('-r', '--routes', opts.on('-r', '--routes',
"Annotate routes.rb with the output of 'rake routes'") do "Annotate routes.rb with the output of 'rake routes'") do
target = { target = {
...@@ -124,7 +130,7 @@ OptionParser.new do |opts| ...@@ -124,7 +130,7 @@ OptionParser.new do |opts|
end end
end end
opts.on('-e', '--exclude [tests,fixtures,factories]', Array, "Do not annotate fixtures, test files, and/or factories") do |exclusions| opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
exclusions ||= %w(tests fixtures factories) exclusions ||= %w(tests fixtures factories)
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" } exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
end end
......
...@@ -18,11 +18,13 @@ module Annotate ...@@ -18,11 +18,13 @@ module Annotate
POSITION_OPTIONS=[ POSITION_OPTIONS=[
:position_in_routes, :position_in_class, :position_in_test, :position_in_routes, :position_in_class, :position_in_test,
:position_in_fixture, :position_in_factory, :position, :position_in_fixture, :position_in_factory, :position,
:position_in_serializer,
] ]
FLAG_OPTIONS=[ FLAG_OPTIONS=[
:show_indexes, :simple_indexes, :include_version, :exclude_tests, :show_indexes, :simple_indexes, :include_version, :exclude_tests,
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace, :timestamp :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
:timestamp, :exclude_serializers
] ]
OTHER_OPTIONS=[ OTHER_OPTIONS=[
:model_dir, :ignore_columns :model_dir, :ignore_columns
......
...@@ -31,6 +31,12 @@ module AnnotateModels ...@@ -31,6 +31,12 @@ module AnnotateModels
FABRICATORS_TEST_DIR = File.join("test", "fabricators") FABRICATORS_TEST_DIR = File.join("test", "fabricators")
FABRICATORS_SPEC_DIR = File.join("spec", "fabricators") FABRICATORS_SPEC_DIR = File.join("spec", "fabricators")
# Serializers https://github.com/rails-api/active_model_serializers
SERIALIZERS_DIR = File.join("app", "serializers")
SERIALIZERS_TEST_DIR = File.join("test", "serializers")
SERIALIZERS_SPEC_DIR = File.join("spec", "serializers")
TEST_PATTERNS = [ TEST_PATTERNS = [
File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"), File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
File.join(MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"), File.join(MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"),
...@@ -55,6 +61,12 @@ module AnnotateModels ...@@ -55,6 +61,12 @@ module AnnotateModels
File.join(FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb"), File.join(FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb"),
] ]
SERIALIZER_PATTERNS = [
File.join(SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"),
File.join(SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"),
File.join(SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb")
]
# Don't show limit (#) on these column types # Don't show limit (#) on these column types
# Example: show "integer" instead of "integer(4)" # Example: show "integer" instead of "integer(4)"
NO_LIMIT_COL_TYPES = ["integer", "boolean"] NO_LIMIT_COL_TYPES = ["integer", "boolean"]
...@@ -302,25 +314,17 @@ module AnnotateModels ...@@ -302,25 +314,17 @@ module AnnotateModels
did_annotate = true did_annotate = true
end end
unless options[:exclude_tests] %w(test fixture factory serializer).each do |key|
did_annotate = TEST_PATTERNS. exclusion_key = "exclude_#{key.pluralize}".to_sym
map { |file| resolve_filename(file, model_name, table_name) }. patterns_constant = "#{key.upcase}_PATTERNS".to_sym
map { |file| annotate_one_file(file, info, :position_in_test, options_with_position(options, :position_in_test)) }. position_key = "position_in_#{key}".to_sym
detect { |result| result } || did_annotate
end
unless options[:exclude_fixtures] unless options[exclusion_key]
did_annotate = FIXTURE_PATTERNS. did_annotate = self.const_get(patterns_constant).
map { |file| resolve_filename(file, model_name, table_name) }. map { |file| resolve_filename(file, model_name, table_name) }.
map { |file| annotate_one_file(file, info, :position_in_fixture, options_with_position(options, :position_in_fixture)) }. map { |file| annotate_one_file(file, info, position_key, options_with_position(options, position_key)) }.
detect { |result| result } || did_annotate detect { |result| result } || did_annotate
end end
unless options[:exclude_factories]
did_annotate = FACTORY_PATTERNS.
map { |file| resolve_filename(file, model_name, table_name) }.
map { |file| annotate_one_file(file, info, :position_in_factory, options_with_position(options, :position_in_factory)) }.
detect { |result| result } || did_annotate
end end
return did_annotate return did_annotate
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment