diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index fc503839b..644589ea4 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -463,6 +463,7 @@ def matched_types(options) # :position_in_fixture:: where to place the annotated section in fixture file # :position_in_factory:: where to place the annotated section in factory file # :position_in_serializer:: where to place the annotated section in serializer file + # :position_in_additional_file_patterns:: where to place the annotated section in files that match additional patterns # :exclude_tests:: whether to skip modification of test/spec files # :exclude_fixtures:: whether to skip modification of fixture files # :exclude_factories:: whether to skip modification of factory files diff --git a/lib/annotate/constants.rb b/lib/annotate/constants.rb index cd2148f39..9a78cb37d 100644 --- a/lib/annotate/constants.rb +++ b/lib/annotate/constants.rb @@ -8,7 +8,7 @@ module Constants POSITION_OPTIONS = [ :position_in_routes, :position_in_class, :position_in_test, :position_in_fixture, :position_in_factory, :position, - :position_in_serializer + :position_in_serializer, :position_in_additional_file_patterns ].freeze FLAG_OPTIONS = [ diff --git a/lib/annotate/parser.rb b/lib/annotate/parser.rb index cb27b8b5d..66f3537ec 100644 --- a/lib/annotate/parser.rb +++ b/lib/annotate/parser.rb @@ -15,7 +15,7 @@ def self.parse(args, env = {}) }.freeze ANNOTATION_POSITIONS = %w[before top after bottom].freeze - FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze + FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer position_in_additional_file_patterns].freeze EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze FORMAT_TYPES = %w[bare rdoc yard markdown].freeze @@ -124,6 +124,14 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength has_set_position['position_in_serializer'] = true end + option_parser.on('--pa', + '--position-in-additional-file-patterns [before|top|after|bottom]', + ANNOTATION_POSITIONS, + 'Place the annotations at the top (before) or the bottom (after) of files that match additional patterns') do |position_in_additional_file_patterns| + env['position_in_additional_file_patterns'] = position_in_additional_file_patterns + has_set_position['position_in_additional_file_patterns'] = true + end + option_parser.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.', diff --git a/lib/generators/annotate/templates/auto_annotate_models.rake b/lib/generators/annotate/templates/auto_annotate_models.rake index 1f355249b..3dfed2d4e 100644 --- a/lib/generators/annotate/templates/auto_annotate_models.rake +++ b/lib/generators/annotate/templates/auto_annotate_models.rake @@ -7,51 +7,52 @@ if Rails.env.development? # You can override any of these by setting an environment variable of the # same name. Annotate.set_defaults( - 'active_admin' => 'false', - 'additional_file_patterns' => [], - 'routes' => 'false', - 'models' => 'true', - 'position_in_routes' => 'before', - 'position_in_class' => 'before', - 'position_in_test' => 'before', - 'position_in_fixture' => 'before', - 'position_in_factory' => 'before', - 'position_in_serializer' => 'before', - 'show_foreign_keys' => 'true', - 'show_complete_foreign_keys' => 'false', - 'show_indexes' => 'true', - 'simple_indexes' => 'false', - 'model_dir' => 'app/models', - 'root_dir' => '', - 'include_version' => 'false', - 'require' => '', - 'exclude_tests' => 'false', - 'exclude_fixtures' => 'false', - 'exclude_factories' => 'false', - 'exclude_serializers' => 'false', - 'exclude_scaffolds' => 'true', - 'exclude_controllers' => 'true', - 'exclude_helpers' => 'true', - 'exclude_sti_subclasses' => 'false', - 'ignore_model_sub_dir' => 'false', - 'ignore_columns' => nil, - 'ignore_routes' => nil, - 'ignore_unknown_models' => 'false', - 'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(",") %>', - 'hide_default_column_types' => '<%= AnnotateModels::NO_DEFAULT_COL_TYPES.join(",") %>', - 'skip_on_db_migrate' => 'false', - 'format_bare' => 'true', - 'format_rdoc' => 'false', - 'format_yard' => 'false', - 'format_markdown' => 'false', - 'sort' => 'false', - 'force' => 'false', - 'frozen' => 'false', - 'classified_sort' => 'true', - 'trace' => 'false', - 'wrapper_open' => nil, - 'wrapper_close' => nil, - 'with_comment' => 'true' + 'active_admin' => 'false', + 'additional_file_patterns' => [], + 'routes' => 'false', + 'models' => 'true', + 'position_in_routes' => 'before', + 'position_in_class' => 'before', + 'position_in_test' => 'before', + 'position_in_fixture' => 'before', + 'position_in_factory' => 'before', + 'position_in_serializer' => 'before', + 'position_in_additional_file_patterns' => 'before', + 'show_foreign_keys' => 'true', + 'show_complete_foreign_keys' => 'false', + 'show_indexes' => 'true', + 'simple_indexes' => 'false', + 'model_dir' => 'app/models', + 'root_dir' => '', + 'include_version' => 'false', + 'require' => '', + 'exclude_tests' => 'false', + 'exclude_fixtures' => 'false', + 'exclude_factories' => 'false', + 'exclude_serializers' => 'false', + 'exclude_scaffolds' => 'true', + 'exclude_controllers' => 'true', + 'exclude_helpers' => 'true', + 'exclude_sti_subclasses' => 'false', + 'ignore_model_sub_dir' => 'false', + 'ignore_columns' => nil, + 'ignore_routes' => nil, + 'ignore_unknown_models' => 'false', + 'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(",") %>', + 'hide_default_column_types' => '<%= AnnotateModels::NO_DEFAULT_COL_TYPES.join(",") %>', + 'skip_on_db_migrate' => 'false', + 'format_bare' => 'true', + 'format_rdoc' => 'false', + 'format_yard' => 'false', + 'format_markdown' => 'false', + 'sort' => 'false', + 'force' => 'false', + 'frozen' => 'false', + 'classified_sort' => 'true', + 'trace' => 'false', + 'wrapper_open' => nil, + 'wrapper_close' => nil, + 'with_comment' => 'true' ) end diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index 76d8cfe63..23935bf99 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -18,6 +18,7 @@ task annotate_models: :environment do options[:position_in_factory] = Annotate::Helpers.fallback(ENV['position_in_factory'], ENV['position']) options[:position_in_test] = Annotate::Helpers.fallback(ENV['position_in_test'], ENV['position']) options[:position_in_serializer] = Annotate::Helpers.fallback(ENV['position_in_serializer'], ENV['position']) + options[:position_in_additional_file_patterns] = Annotate::Helpers.fallback(ENV['position_in_additional_file_patterns'], ENV['position']) options[:show_foreign_keys] = Annotate::Helpers.true?(ENV['show_foreign_keys']) options[:show_complete_foreign_keys] = Annotate::Helpers.true?(ENV['show_complete_foreign_keys']) options[:show_indexes] = Annotate::Helpers.true?(ENV['show_indexes']) diff --git a/spec/lib/annotate/parser_spec.rb b/spec/lib/annotate/parser_spec.rb index 176e453e3..e32c76d27 100644 --- a/spec/lib/annotate/parser_spec.rb +++ b/spec/lib/annotate/parser_spec.rb @@ -174,6 +174,22 @@ module Annotate # rubocop:disable Metrics/ModuleLength end end + %w[--pa --position-in-additional-file-patterns].each do |option| + describe option do + let(:env_key) { 'position_in_additional_file_patterns' } + + Parser::ANNOTATION_POSITIONS.each do |position| + context "when specifying #{position}" do + it "sets the ENV variable to #{position}" do + allow(ENV).to receive(:[]=) + Parser.parse([option, position]) + expect(ENV).to have_received(:[]=).with(env_key, position) + end + end + end + end + end + %w[--w --wrapper].each do |option| describe option do let(:env_key) { 'wrapper' }