Skip to content

Commit 97539cd

Browse files
author
Ethan Bresler
committed
Merge branch 'develop' of https://github.com/ctran/annotate_models into develop
2 parents 6c3bdd5 + 5745034 commit 97539cd

File tree

14 files changed

+122
-128
lines changed

14 files changed

+122
-128
lines changed

.rubocop_todo.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-02-13 20:05:34 +0900 using RuboCop version 0.68.1.
3+
# on 2020-03-01 03:15:48 +0900 using RuboCop version 0.68.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -266,14 +266,16 @@ Style/Dir:
266266
Exclude:
267267
- 'bin/annotate'
268268

269-
# Offense count: 7
269+
# Offense count: 9
270270
Style/Documentation:
271271
Exclude:
272272
- 'spec/**/*'
273273
- 'test/**/*'
274274
- 'lib/annotate.rb'
275275
- 'lib/annotate/active_record_patch.rb'
276276
- 'lib/annotate/annotate_models.rb'
277+
- 'lib/annotate/annotate_routes.rb'
278+
- 'lib/annotate/annotate_routes/helpers.rb'
277279
- 'lib/annotate/version.rb'
278280
- 'lib/generators/annotate/install_generator.rb'
279281
- 'lib/tasks/annotate_models_migrate.rake'
@@ -307,7 +309,7 @@ Style/FormatStringToken:
307309
Exclude:
308310
- 'lib/annotate/annotate_models.rb'
309311

310-
# Offense count: 26
312+
# Offense count: 27
311313
# Cop supports --auto-correct.
312314
# Configuration parameters: EnforcedStyle.
313315
# SupportedStyles: when_needed, always, never
@@ -422,7 +424,7 @@ Style/RedundantParentheses:
422424
# Configuration parameters: AllowMultipleReturnValues.
423425
Style/RedundantReturn:
424426
Exclude:
425-
- 'lib/annotate/annotate_routes.rb'
427+
- 'lib/annotate/annotate_routes/helpers.rb'
426428

427429
# Offense count: 2
428430
# Cop supports --auto-correct.

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
## 3.1.1
2+
Changes
3+
- Bump required ruby version to >= 2.4 [#772](https://github.com/ctran/annotate_models/pull/772)
4+
- [Revert #677] Fix column default annotations [#768](https://github.com/ctran/annotate_models/pull/768)
5+
6+
Project Improvements
7+
- Refactor by adding AnnotateRoutes::Helpers [#770](https://github.com/ctran/annotate_models/pull/770)
8+
- Bump puma from 4.3.1 to 4.3.3 in /spec/integration/rails_6.0.2.1 [#771](https://github.com/ctran/annotate_models/pull/771)
9+
- Bump puma from 3.12.2 to 4.3.3 in /spec/integration/rails_5.2.4.1 [#769](https://github.com/ctran/annotate_models/pull/769)
10+
- Bump nokogiri from 1.10.7 to 1.10.8 in /spec/integration/rails_5.2.4.1 [#766](https://github.com/ctran/annotate_models/pull/766)
11+
- Bump nokogiri from 1.10.7 to 1.10.8 in /spec/integration/rails_6.0.2.1 [#765](https://github.com/ctran/annotate_models/pull/765)
12+
- Refactor test cases of AnnotateRoutes [#760](https://github.com/ctran/annotate_models/pull/760)
13+
- Rename FactoryGirl -> FactoryBot comment [#759](https://github.com/ctran/annotate_models/pull/759)
14+
115
## 3.1.0
216
Changes
317
- Fix new lines after comments for rubocop compatibility [#757](https://github.com/ctran/annotate_models/pull/757)

annotate.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
77
s.name = 'annotate'
88
s.version = Annotate.version
99

10-
s.required_ruby_version = '>= 2.2.0'
10+
s.required_ruby_version = '>= 2.4.0'
1111
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
1212
s.authors = ['Alex Chaffee', 'Cuong Tran', 'Marcos Piccinini', 'Turadg Aleahmad', 'Jon Frisby']
1313
s.description = 'Annotates Rails/ActiveRecord Models, routes, fixtures, and others based on the database schema.'

lib/annotate/annotate_models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def quote(value)
226226
end
227227

228228
def schema_default(klass, column)
229-
quote(klass.columns.find { |x| x.name.to_s == column.name.to_s }.try(:default))
229+
quote(klass.column_defaults[column.name])
230230
end
231231

232232
def retrieve_indexes_from_table(klass)

lib/annotate/annotate_routes.rb

Lines changed: 7 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
#
2020
# Released under the same license as Ruby. No Support. No Warranty.
2121
#
22+
23+
require_relative './annotate_routes/helpers'
24+
2225
module AnnotateRoutes
2326
PREFIX = '== Route Map'.freeze
2427
PREFIX_MD = '## Route Map'.freeze
2528
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze
2629

27-
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
28-
2930
class << self
3031
def do_annotations(options = {})
3132
if routes_file_exist?
3233
existing_text = File.read(routes_file)
33-
content, header_position = strip_annotations(existing_text)
34+
content, header_position = Helpers.strip_annotations(existing_text)
3435
new_content = annotate_routes(header(options), content, header_position, options)
3536
new_text = new_content.join("\n")
3637

@@ -47,7 +48,7 @@ def do_annotations(options = {})
4748
def remove_annotations(_options={})
4849
if routes_file_exist?
4950
existing_text = File.read(routes_file)
50-
content, header_position = strip_annotations(existing_text)
51+
content, header_position = Helpers.strip_annotations(existing_text)
5152
new_content = strip_on_removal(content, header_position)
5253
new_text = new_content.join("\n")
5354
if rewrite_contents(existing_text, new_text)
@@ -73,7 +74,7 @@ def routes_file
7374
def header(options = {})
7475
routes_map = app_routes_map(options)
7576

76-
magic_comments_map, routes_map = extract_magic_comments_from_array(routes_map)
77+
magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map)
7778

7879
out = []
7980

@@ -113,35 +114,6 @@ def comment(row = '')
113114
end
114115
end
115116

116-
# TODO: write the method doc using ruby rdoc formats
117-
# This method returns an array of 'real_content' and 'header_position'.
118-
# 'header_position' will either be :before, :after, or
119-
# a number. If the number is > 0, the
120-
# annotation was found somewhere in the
121-
# middle of the file. If the number is
122-
# zero, no annotation was found.
123-
def strip_annotations(content)
124-
real_content = []
125-
mode = :content
126-
header_position = 0
127-
128-
content.split(/\n/, -1).each_with_index do |line, line_number|
129-
if mode == :header && line !~ /\s*#/
130-
mode = :content
131-
real_content << line unless line.blank?
132-
elsif mode == :content
133-
if line =~ /^\s*#\s*== Route.*$/
134-
header_position = line_number + 1 # index start's at 0
135-
mode = :header
136-
else
137-
real_content << line
138-
end
139-
end
140-
end
141-
142-
real_content_and_header_position(real_content, header_position)
143-
end
144-
145117
def strip_on_removal(content, header_position)
146118
if header_position == :before
147119
content.shift while content.first == ''
@@ -168,7 +140,7 @@ def rewrite_contents(existing_text, new_text)
168140
end
169141

170142
def annotate_routes(header, content, header_position, options = {})
171-
magic_comments_map, content = extract_magic_comments_from_array(content)
143+
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
172144
if %w(before top).include?(options[:position_in_routes])
173145
header = header << '' if content.first != ''
174146
magic_comments_map << '' if magic_comments_map.any?
@@ -208,24 +180,6 @@ def app_routes_map(options)
208180
routes_map
209181
end
210182

211-
# @param [Array<String>] content
212-
# @return [Array<String>] all found magic comments
213-
# @return [Array<String>] content without magic comments
214-
def extract_magic_comments_from_array(content_array)
215-
magic_comments = []
216-
new_content = []
217-
218-
content_array.each do |row|
219-
if row =~ MAGIC_COMMENT_MATCHER
220-
magic_comments << row.strip
221-
else
222-
new_content << row
223-
end
224-
end
225-
226-
[magic_comments, new_content]
227-
end
228-
229183
def content(line, maxs, options = {})
230184
return line.rstrip unless options[:format_markdown]
231185

@@ -235,18 +189,5 @@ def content(line, maxs, options = {})
235189
sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
236190
end.join(' | ')
237191
end
238-
239-
def real_content_and_header_position(real_content, header_position)
240-
# By default assume the annotation was found in the middle of the file
241-
242-
# ... unless we have evidence it was at the beginning ...
243-
return real_content, :before if header_position == 1
244-
245-
# ... or that it was at the end.
246-
return real_content, :after if header_position >= real_content.count
247-
248-
# and the default
249-
return real_content, header_position
250-
end
251192
end
252193
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module AnnotateRoutes
2+
module Helpers
3+
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
4+
5+
class << self
6+
# TODO: write the method doc using ruby rdoc formats
7+
# This method returns an array of 'real_content' and 'header_position'.
8+
# 'header_position' will either be :before, :after, or
9+
# a number. If the number is > 0, the
10+
# annotation was found somewhere in the
11+
# middle of the file. If the number is
12+
# zero, no annotation was found.
13+
def strip_annotations(content)
14+
real_content = []
15+
mode = :content
16+
header_position = 0
17+
18+
content.split(/\n/, -1).each_with_index do |line, line_number|
19+
if mode == :header && line !~ /\s*#/
20+
mode = :content
21+
real_content << line unless line.blank?
22+
elsif mode == :content
23+
if line =~ /^\s*#\s*== Route.*$/
24+
header_position = line_number + 1 # index start's at 0
25+
mode = :header
26+
else
27+
real_content << line
28+
end
29+
end
30+
end
31+
32+
real_content_and_header_position(real_content, header_position)
33+
end
34+
35+
# @param [Array<String>] content
36+
# @return [Array<String>] all found magic comments
37+
# @return [Array<String>] content without magic comments
38+
def extract_magic_comments_from_array(content_array)
39+
magic_comments = []
40+
new_content = []
41+
42+
content_array.each do |row|
43+
if row =~ MAGIC_COMMENT_MATCHER
44+
magic_comments << row.strip
45+
else
46+
new_content << row
47+
end
48+
end
49+
50+
[magic_comments, new_content]
51+
end
52+
53+
private
54+
55+
def real_content_and_header_position(real_content, header_position)
56+
# By default assume the annotation was found in the middle of the file
57+
58+
# ... unless we have evidence it was at the beginning ...
59+
return real_content, :before if header_position == 1
60+
61+
# ... or that it was at the end.
62+
return real_content, :after if header_position >= real_content.count
63+
64+
# and the default
65+
return real_content, header_position
66+
end
67+
end
68+
end
69+
end

lib/annotate/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Annotate
22
def self.version
3-
'3.1.0'
3+
'3.1.1'
44
end
55
end

spec/integration/rails_5.2.4.1/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ gem 'rails', '~> 5.2.4', '>= 5.2.4.1'
66
# Use sqlite3 as the database for Active Record
77
gem 'sqlite3'
88
# Use Puma as the app server
9-
gem 'puma', '~> 3.11'
9+
gem 'puma', '~> 4.3'
1010
# Use SCSS for stylesheets
1111
gem 'sass-rails', '~> 5.0'
1212
# Use Uglifier as compressor for JavaScript assets

spec/integration/rails_5.2.4.1/Gemfile.lock

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ../../..
33
specs:
4-
annotate (3.1.0)
4+
annotate (3.1.1)
55
activerecord (>= 3.2, < 7.0)
66
rake (>= 10.4, < 14.0)
77

@@ -108,10 +108,11 @@ GEM
108108
minitest (5.14.0)
109109
msgpack (1.3.2)
110110
nio4r (2.5.2)
111-
nokogiri (1.10.7)
111+
nokogiri (1.10.8)
112112
mini_portile2 (~> 2.4.0)
113113
public_suffix (4.0.3)
114-
puma (3.12.2)
114+
puma (4.3.3)
115+
nio4r (~> 2.0)
115116
rack (2.1.2)
116117
rack-test (1.1.0)
117118
rack (>= 1.0, < 3)
@@ -201,7 +202,7 @@ DEPENDENCIES
201202
coffee-rails (~> 4.2)
202203
jbuilder (~> 2.5)
203204
listen (>= 3.0.5, < 3.2)
204-
puma (~> 3.11)
205+
puma (~> 4.3)
205206
rails (~> 5.2.4, >= 5.2.4.1)
206207
sass-rails (~> 5.0)
207208
selenium-webdriver

spec/integration/rails_5.2.4.1_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
+#
3535
+# id :integer not null, primary key
3636
+# content :string
37-
+# count :integer default("0")
38-
+# status :boolean default("0")
37+
+# count :integer default(0)
38+
+# status :boolean default(FALSE)
3939
+# created_at :datetime not null
4040
+# updated_at :datetime not null
4141
+#
@@ -55,8 +55,8 @@
5555
+#
5656
+# id :integer not null, primary key
5757
+# content :string
58-
+# count :integer default("0")
59-
+# status :boolean default("0")
58+
+# count :integer default(0)
59+
+# status :boolean default(FALSE)
6060
+# created_at :datetime not null
6161
+# updated_at :datetime not null
6262
+#
@@ -76,8 +76,8 @@
7676
+#
7777
+# id :integer not null, primary key
7878
+# content :string
79-
+# count :integer default("0")
80-
+# status :boolean default("0")
79+
+# count :integer default(0)
80+
+# status :boolean default(FALSE)
8181
+# created_at :datetime not null
8282
+# updated_at :datetime not null
8383
+#

spec/integration/rails_6.0.2.1/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ gem 'rails', '~> 6.0.2', '>= 6.0.2.1'
66
# Use sqlite3 as the database for Active Record
77
gem 'sqlite3', '~> 1.4'
88
# Use Puma as the app server
9-
gem 'puma', '~> 4.1'
9+
gem 'puma', '~> 4.3'
1010
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
1111
gem 'jbuilder', '~> 2.7'
1212
# Use Redis adapter to run Action Cable in production

0 commit comments

Comments
 (0)