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:
To annotate just your models:
annotate --exclude tests,fixtures,factories
annotate --exclude tests,fixtures,factories,serializers
To annotate routes.rb:
annotate --routes
To remove model/test/fixture/factory annotations:
To remove model/test/fixture/factory/serializer annotations:
annotate --delete
......@@ -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
--pr, --position-in-routes [before|after]
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'
-v, --version Show the current version of this gem
-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
--ignore-model-subdirects Ignore subdirectories of the models directory
--sort Sort columns alphabetically, rather than in creation order
-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
--exclude
-e [tests,fixtures,factories,serializers],
--exclude Do not annotate fixtures, test files, factories, and/or serializers
-f [bare|rdoc|markdown], Render Schema Infomation as plain/RDoc/Markdown
--format
--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.
--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
......
......@@ -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|
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|
ENV[key] = p unless(has_set_position[key])
end
......@@ -72,6 +72,12 @@ OptionParser.new do |opts|
has_set_position['position_in_routes'] = true
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',
"Annotate routes.rb with the output of 'rake routes'") do
target = {
......@@ -124,7 +130,7 @@ OptionParser.new do |opts|
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.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
end
......
......@@ -18,11 +18,13 @@ module Annotate
POSITION_OPTIONS=[
:position_in_routes, :position_in_class, :position_in_test,
:position_in_fixture, :position_in_factory, :position,
:position_in_serializer,
]
FLAG_OPTIONS=[
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
: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=[
:model_dir, :ignore_columns
......
......@@ -31,6 +31,12 @@ module AnnotateModels
FABRICATORS_TEST_DIR = File.join("test", "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 = [
File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
File.join(MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"),
......@@ -55,6 +61,12 @@ module AnnotateModels
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
# Example: show "integer" instead of "integer(4)"
NO_LIMIT_COL_TYPES = ["integer", "boolean"]
......@@ -302,25 +314,17 @@ module AnnotateModels
did_annotate = true
end
unless options[:exclude_tests]
did_annotate = TEST_PATTERNS.
map { |file| resolve_filename(file, model_name, table_name) }.
map { |file| annotate_one_file(file, info, :position_in_test, options_with_position(options, :position_in_test)) }.
detect { |result| result } || did_annotate
end
%w(test fixture factory serializer).each do |key|
exclusion_key = "exclude_#{key.pluralize}".to_sym
patterns_constant = "#{key.upcase}_PATTERNS".to_sym
position_key = "position_in_#{key}".to_sym
unless options[:exclude_fixtures]
did_annotate = FIXTURE_PATTERNS.
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)) }.
detect { |result| result } || did_annotate
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
unless options[exclusion_key]
did_annotate = self.const_get(patterns_constant).
map { |file| resolve_filename(file, model_name, table_name) }.
map { |file| annotate_one_file(file, info, position_key, options_with_position(options, position_key)) }.
detect { |result| result } || did_annotate
end
end
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