Skip to content

Active Model Serializers #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions bin/annotate
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/annotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 22 additions & 18 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"]
Expand Down Expand Up @@ -302,25 +314,17 @@ def annotate(klass, file, header, options={})
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
Expand Down