From 504c2674885fc99de2688e6bedb27a1b2fb74a4f Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Mon, 24 Nov 2014 18:03:16 -0700 Subject: [PATCH 1/2] Annotate conventionally named serializers --- bin/annotate | 10 +++++++-- lib/annotate.rb | 4 +++- lib/annotate/annotate_models.rb | 40 ++++++++++++++++++--------------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/bin/annotate b/bin/annotate index 035160369..de2f55118 100755 --- a/bin/annotate +++ b/bin/annotate @@ -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 diff --git a/lib/annotate.rb b/lib/annotate.rb index 72b19022d..cb4ac19ac 100644 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -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 diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 9dc79eea2..8ab707bed 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -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 @@ 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 From e32aae623b0bf91f61057a551285b33f8ce893ca Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Tue, 25 Nov 2014 11:38:29 -0700 Subject: [PATCH 2/2] Update README to reflect serializer updates --- README.rdoc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/README.rdoc b/README.rdoc index d4d6ef1b7..7a994ba9e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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