Commit 89bce57f by cuong.tran

Merge branch 'release/v2.7.3'

parents 5f37a974 db3eb14c
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-12-17 10:16:05 +0100 using RuboCop version 0.46.0.
# on 2017-10-13 10:01:18 +0200 using RuboCop version 0.46.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
......@@ -65,11 +65,6 @@ Lint/HandleExceptions:
Exclude:
- 'bin/annotate'
# Offense count: 8
Lint/IneffectiveAccessModifier:
Exclude:
- 'lib/annotate/annotate_routes.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
......@@ -88,62 +83,51 @@ Lint/ShadowingOuterLocalVariable:
Exclude:
- 'Rakefile'
# Offense count: 6
# Offense count: 7
# Cop supports --auto-correct.
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
Lint/UnusedBlockArgument:
Exclude:
- 'bin/annotate'
# Offense count: 1
# Configuration parameters: ContextCreatingMethods.
Lint/UselessAccessModifier:
Exclude:
- 'lib/annotate/annotate_routes.rb'
# Offense count: 17
# Offense count: 18
Metrics/AbcSize:
Max: 159
Max: 142
# Offense count: 3
# Configuration parameters: CountComments.
Metrics/BlockLength:
Max: 134
Max: 142
# Offense count: 2
Metrics/BlockNesting:
Max: 4
# Offense count: 8
# Offense count: 9
Metrics/CyclomaticComplexity:
Max: 42
Max: 36
# Offense count: 350
# Offense count: 380
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 543
Max: 276
# Offense count: 24
# Offense count: 26
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 79
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 116
Max: 75
# Offense count: 7
Metrics/PerceivedComplexity:
Max: 48
Max: 42
# Offense count: 1
Style/AccessorMethodName:
Exclude:
- 'lib/annotate.rb'
# Offense count: 3
# Offense count: 2
# Cop supports --auto-correct.
Style/AlignArray:
Exclude:
......@@ -259,7 +243,7 @@ Style/ExtraSpacing:
- 'spec/integration/rails_4.2.0/Gemfile'
- 'spec/integration/rails_4.2.0/config.ru'
# Offense count: 9
# Offense count: 10
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: format, sprintf, percent
Style/FormatString:
......@@ -267,13 +251,6 @@ Style/FormatString:
- 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes.rb'
# Offense count: 181
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: when_needed, always
Style/FrozenStringLiteralComment:
Enabled: false
# Offense count: 7
# Configuration parameters: MinBodyLength.
Style/GuardClause:
......@@ -356,16 +333,6 @@ Style/NumericLiterals:
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles.
# SupportedStyles: predicate, comparison
Style/NumericPredicate:
Exclude:
- 'spec/**/*'
- 'lib/annotate.rb'
- 'lib/annotate/annotate_models.rb'
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters.
Style/PercentLiteralDelimiters:
Exclude:
......@@ -458,16 +425,15 @@ Style/SpaceAroundKeyword:
- 'spec/integration/rails_4.2.0/Gemfile'
- 'spec/integration/standalone/Gemfile'
# Offense count: 6
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment.
Style/SpaceAroundOperators:
Exclude:
- 'lib/annotate/annotate_models.rb'
- 'lib/tasks/annotate_models.rake'
- 'lib/tasks/annotate_routes.rake'
# Offense count: 4
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: space, no_space
......@@ -517,14 +483,14 @@ Style/SpaceInsideStringInterpolation:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 223
# Offense count: 237
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Enabled: false
# Offense count: 2
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: single_quotes, double_quotes
......@@ -581,7 +547,7 @@ Style/UnneededInterpolation:
Exclude:
- 'bin/annotate'
# Offense count: 8
# Offense count: 4
# Cop supports --auto-correct.
Style/UnneededPercentQ:
Exclude:
......
sudo: false
language: ruby
rvm:
- 1.9.3
- 2.0
- 2.1
- 2.2.5
- 2.3.0
- 2.4.0
- ruby-head
- 2.2.7
- 2.3.4
- 2.4.1
- ruby-head
matrix:
allow_failures:
- rvm: ruby-head
- rvm: 1.9.3
- rvm: ruby-head
before_install:
- gem update --system
- gem update bundler
- gem update --system
- gem update bundler
script:
- bundle exec rubocop && bundle exec rspec
- bundle exec rubocop && bundle exec rspec
deploy:
provider: rubygems
api_key:
secure: Y7DUitak26kcRAAkgph/7m6Y1wHeObD0BelSSJbmCfjkRd/qaVy7fz9VvHL9zxlRJtLGVHInyCnwcfzinibY6OFd3MoMYHKv8GFa2LxLJNEVSY46KQYFxfH5JTg1ejh6ldoJRRBoeOx9dcWS80pRNjYMKPGnpSz7yDBl1azibFs=
gem: annotate
on:
tags: true
repo: ctran/annotate_models
== 2.7.3
See https://github.com/ctran/annotate_models/releases/tag/v2.7.3
== 2.7.2
See https://github.com/ctran/annotate_models/releases/tag/v2.7.2
......
source 'https://rubygems.org'
ruby '>= 2.2.0'
gem 'activerecord', '>= 4.2.5', require: false
gem 'rake', require: false
group :development do
gem 'bump'
gem 'mg', require: false
gem 'travis', require: false
platforms :mri, :mingw do
gem 'yard', require: false
end
......
......@@ -17,7 +17,7 @@ your...
- Object Daddy exemplars
- Machinist blueprints
- Fabrication fabricators
- Thoughtbot's factory_girl factories, i.e. the (spec|test)/factories/<model>_factory.rb files
- Thoughtbot's factory_bot factories, i.e. the (spec|test)/factories/<model>_factory.rb files
- routes.rb file (for Rails projects)
The schema comment looks like this:
......@@ -55,11 +55,15 @@ Also, if you pass the -r option, it'll annotate routes.rb with the output of
Into Gemfile from rubygems.org:
gem 'annotate'
group :development do
gem 'annotate'
end
Into Gemfile from Github:
gem 'annotate', git: 'https://github.com/ctran/annotate_models.git'
group :development do
gem 'annotate', git: 'https://github.com/ctran/annotate_models.git'
end
Into environment gems from rubygems.org:
......@@ -154,14 +158,15 @@ If you want to run <code>rake db:migrate</code> as a one-off without running ann
you can do so with a simple environment variable, instead of editing the
+.rake+ file:
skip_on_db_migrate=1 rake db:migrate
ANNOTATE_SKIP_ON_DB_MIGRATE=1 rake db:migrate
== Options
Usage: annotate [options] [model_file]*
-d, --delete Remove annotations from all model files or the routes.rb file
-p, --position [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)
-p [before|top|after|bottom], Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)
--position
--pc, --position-in-class [before|top|after|bottom]
Place the annotations at the top (before) or the bottom (after) of the model file
--pf, --position-in-factory [before|top|after|bottom]
......@@ -179,15 +184,19 @@ you can do so with a simple environment variable, instead of editing the
--wo, --wrapper-open STR Annotation wrapper opening.
--wc, --wrapper-close STR Annotation wrapper closing
-r, --routes Annotate routes.rb with the output of 'rake routes'
-aa, --active-admin Annotate all activeadmin models
-a, --active-admin Annotate active_admin models
-v, --version Show the current version of this gem
-m, --show-migration Include the migration version number in the annotation
-i, --show-indexes List the table's database indexes in the annotation
-k, --show-foreign-keys List the table's foreign key constraints in the annotation
--ck, --complete-foreign-keys
Complete foreign key names in the annotation
-i, --show-indexes List the table's database indexes in the annotation
-s, --simple-indexes Concat the column's related indexes in the annotation
--model-dir dir Annotate model files stored in dir rather than app/models, separate multiple dirs with commas
--root-dir dir Annotate files stored within root dir projects, separate multiple dirs with commas
--ignore-model-subdirects Ignore subdirectories of the models directory
--sort Sort columns alphabetically, rather than in creation order
--classified-sort Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns
-R, --require path Additional file to require before loading models, may be used multiple times
-e [tests,fixtures,factories,serializers],
--exclude Do not annotate fixtures, test files, factories, and/or serializers
......@@ -197,6 +206,13 @@ you can do so with a simple environment variable, instead of editing the
--timestamp Include timestamp in (routes) annotation
--trace If unable to annotate a file, print the full stack trace, not just the exception message.
-I, --ignore-columns REGEX don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`
--ignore-routes REGEX don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`
--hide-limit-column-types VALUES
don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)
--hide-default-column-types VALUES
don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)
--ignore-unknown-models don't display warnings for bad model files
--with-comment include database comments in model annotations
......@@ -242,7 +258,7 @@ extra carefully, and consider using one.
== Links
- Factory Girl: http://github.com/thoughtbot/factory_girl
- Factory Bot: http://github.com/thoughtbot/factory_bot
- Object Daddy: http://github.com/flogic/object_daddy
- Machinist: http://github.com/notahat/machinist
- Fabrication: http://github.com/paulelliott/fabrication
......
......@@ -28,7 +28,7 @@ require 'mg'
begin
MG.new('annotate.gemspec')
rescue Exception
STDERR.puts("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.")
$stderr.puts("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.")
# Gemspec is probably in a broken state, so let's give ourselves a chance to
# build a new one...
end
......
......@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
s.name = 'annotate'
s.version = Annotate.version
s.required_ruby_version = '>= 1.9.3'
s.required_ruby_version = '>= 2.2.0'
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
s.authors = ['Alex Chaffee', 'Cuong Tran', 'Marcos Piccinini', 'Turadg Aleahmad', 'Jon Frisby']
s.description = 'Annotates Rails/ActiveRecord Models, routes, fixtures, and others based on the database schema.'
......
......@@ -90,7 +90,7 @@ OptionParser.new do |opts|
ENV['routes'] = 'true'
end
opts.on('-aa', '--active-admin', 'Annotate active_admin models') do
opts.on('-a', '--active-admin', 'Annotate active_admin models') do
ENV['active_admin'] = 'true'
end
......@@ -107,6 +107,12 @@ OptionParser.new do |opts|
ENV['show_foreign_keys'] = 'yes'
end
opts.on('--ck',
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do
ENV['show_foreign_keys'] = 'yes'
ENV['show_complete_foreign_keys'] = 'yes'
end
opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do
ENV['show_indexes'] = 'yes'
......@@ -191,12 +197,16 @@ OptionParser.new do |opts|
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values|
ENV['ignore_unknown_models'] = 'true'
end
opts.on('--with-comment', "include database comments in model annotations") do |values|
ENV['with_comment'] = 'true'
end
end.parse!
options = Annotate.setup_options(
is_rake: ENV['is_rake'] && !ENV['is_rake'].empty?
)
Annotate.eager_load(options)
Annotate.eager_load(options) if Annotate.include_models?
AnnotateModels.send(target_action, options) if Annotate.include_models?
AnnotateRoutes.send(target_action, options) if Annotate.include_routes?
......@@ -30,9 +30,10 @@ module Annotate
: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, :exclude_serializers, :classified_sort, :show_foreign_keys,
:timestamp, :exclude_serializers, :classified_sort,
:show_foreign_keys, :show_complete_foreign_keys,
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
:exclude_sti_subclasses, :ignore_unknown_models
:exclude_sti_subclasses, :ignore_unknown_models, :with_comment
].freeze
OTHER_OPTIONS = [
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,
......@@ -105,7 +106,7 @@ module Annotate
end
def self.skip_on_migration?
ENV['skip_on_db_migrate'] =~ TRUE_RE
ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ TRUE_RE || ENV['skip_on_db_migrate'] =~ TRUE_RE
end
def self.include_routes?
......@@ -113,7 +114,7 @@ module Annotate
end
def self.include_models?
true
ENV['routes'] !~ TRUE_RE
end
def self.loaded_tasks=(val)
......@@ -168,7 +169,7 @@ module Annotate
require 'rake/dsl_definition'
rescue StandardError => e
# We might just be on an old version of Rake...
puts e.message
$stderr.puts e.message
exit e.status_code
end
require 'rake'
......
# rubocop:disable Metrics/ModuleLength
# == Annotate Routes
#
# Based on:
......@@ -36,7 +38,18 @@ module AnnotateRoutes
def header(options = {})
routes_map = app_routes_map(options)
out = ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
magic_comments_map, routes_map = extract_magic_comments_from_array(routes_map)
out = []
magic_comments_map.each do |magic_comment|
out << magic_comment
end
out << '' if magic_comments_map.any?
out += ["# #{options[:wrapper_open]}"] if options[:wrapper_open]
out += ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
out += ['#']
return out if routes_map.size.zero?
......@@ -51,35 +64,56 @@ module AnnotateRoutes
out += ["# #{content(routes_map[0], maxs, options)}"]
end
out + routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
out += routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
out += ["# #{options[:wrapper_close]}"] if options[:wrapper_close]
out
end
def do_annotations(options = {})
return unless routes_exists?
existing_text = File.read(routes_file)
if write_contents(existing_text, header(options), options)
if rewrite_contents_with_header(existing_text, header(options), options)
puts "#{routes_file} annotated."
end
end
def remove_annotations(options={})
def remove_annotations(_options={})
return unless routes_exists?
existing_text = File.read(routes_file)
content, where_header_found = strip_annotations(existing_text)
content = strip_on_removal(content, where_header_found)
if write_contents(existing_text, content, options)
new_content = strip_on_removal(content, where_header_found)
if rewrite_contents(existing_text, new_content)
puts "Removed annotations from #{routes_file}."
end
end
end
private
def self.magic_comment_matcher
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
end
# @param [Array<String>] content
# @return [Array<String>] all found magic comments
# @return [Array<String>] content without magic comments
def self.extract_magic_comments_from_array(content_array)
magic_comments = []
new_content = []
content_array.map do |row|
if row =~ magic_comment_matcher
magic_comments << row.strip
else
new_content << row
end
end
[magic_comments, new_content]
end
def self.app_routes_map(options)
routes_map = `rake routes`.split(/\n/, -1)
routes_map = `rake routes`.chomp("\n").split(/\n/, -1)
# In old versions of Rake, the first line of output was the cwd. Not so
# much in newer ones. We ditch that line if it exists, and if not, we
......@@ -106,7 +140,22 @@ module AnnotateRoutes
routes_exists
end
def self.write_contents(existing_text, header, options = {})
# @param [String, Array<String>]
def self.rewrite_contents(existing_text, new_content)
# Make sure we end on a trailing newline.
new_content << '' unless new_content.last == ''
new_text = new_content.join("\n")
if existing_text == new_text
puts "#{routes_file} unchanged."
false
else
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
end
end
def self.rewrite_contents_with_header(existing_text, header, options = {})
content, where_header_found = strip_annotations(existing_text)
new_content = annotate_routes(header, content, where_header_found, options)
......@@ -124,9 +173,11 @@ module AnnotateRoutes
end
def self.annotate_routes(header, content, where_header_found, options = {})
magic_comments_map, content = extract_magic_comments_from_array(content)
if %w(before top).include?(options[:position_in_routes])
header = header << '' if content.first != ''
new_content = header + content
magic_comments_map << '' if magic_comments_map.any?
new_content = magic_comments_map + header + content
else
# Ensure we have adequate trailing newlines at the end of the file to
# ensure a blank line separating the content from the annotation.
......@@ -136,7 +187,7 @@ module AnnotateRoutes
# the spacer we put in the first time around.
content.shift if where_header_found == :before && content.first == ''
new_content = content + header
new_content = magic_comments_map + content + header
end
new_content
......@@ -156,7 +207,7 @@ module AnnotateRoutes
content.split(/\n/, -1).each_with_index do |line, line_number|
if mode == :header && line !~ /\s*#/
mode = :content
next unless line == ''
real_content << line unless line.blank?
elsif mode == :content
if line =~ /^\s*#\s*== Route.*$/
header_found_at = line_number + 1 # index start's at 0
......
module Annotate
def self.version
'2.7.2'
'2.7.3'
end
end
require 'annotate'
module Annotate
module Generators
class InstallGenerator < Rails::Generators::Base
desc 'Copy annotate_models rakefiles for automatic annotation'
source_root File.expand_path('../templates', __FILE__)
source_root File.expand_path('templates', __dir__)
# copy rake tasks
def copy_tasks
......
......@@ -42,6 +42,7 @@ if Rails.env.development?
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
......
......@@ -47,6 +47,7 @@ task annotate_models: :environment do
options[:ignore_routes] = ENV.fetch('ignore_routes', nil)
options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '')
options[:hide_default_column_types] = Annotate.fallback(ENV['hide_default_column_types'], '')
options[:with_comment] = Annotate.fallback(ENV['with_comment'], '')
AnnotateModels.do_annotations(options)
end
......
......@@ -8,6 +8,8 @@ task :annotate_routes => :environment do
options[:position_in_routes] = Annotate.fallback(ENV['position_in_routes'], ENV['position'])
options[:ignore_routes] = Annotate.fallback(ENV['ignore_routes'], nil)
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
AnnotateRoutes.do_annotations(options)
end
......
......@@ -63,7 +63,7 @@ module Rails
end
rescue Gem::LoadError => load_error
if load_error.message =~ /Could not find RubyGem rails/
STDERR.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
$stderr.puts "Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed."
exit 1
else
raise
......
class NoNamespace < ActiveRecord::Base
enum foo: [:bar, :baz]
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :no_namespaces do |t|
t.integer :foo
t.timestamps
end
end
end
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