Skip to content

Support rails 8 #1044

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ name: CI
on:
pull_request:
branches:
- '*'
- "*"

push:
branches:
- '*'
- "*"

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby: ['2.7']
ruby: ["3.4.4"]

steps:
- name: Checkout
Expand Down
16 changes: 11 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ require:

AllCops:
Exclude:
- 'vendor/**/*'
- 'spec/fixtures/**/*'
- 'tmp/**/*'
- 'spec/integration/**/*'
- "vendor/**/*"
- "spec/fixtures/**/*"
- "tmp/**/*"
- "spec/integration/**/*"
NewCops: enable

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
- "spec/**/*.rb"
Style/FetchEnvVar:
Enabled: false
Metrics/ClassLength:
Enabled: false
RSpec/RemoveConst:
Enabled: false
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 2.7.3
ruby 3.4.4
10 changes: 4 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
source 'https://rubygems.org'

ruby '>= 2.4.0'
ruby '>= 3.0.0'

gem 'activerecord', '>= 4.2.5', '< 6', require: false
gem 'activerecord', '>= 4.2.5', '< 9', 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
Expand All @@ -19,17 +18,16 @@ group :development, :test do
gem 'guard-rspec', require: false
gem 'rspec', require: false

gem 'rubocop', '~> 1.12.0', require: false
gem 'rubocop', '~> 1.76.1', require: false
gem 'rubocop-rake', require: false
gem 'rubocop-rspec', '~> 2.2.0', require: false
gem 'rubocop-rspec', '~> 3.6.0', require: false
gem 'simplecov', require: false
gem 'terminal-notifier-guard', require: false

gem 'codeclimate-test-reporter'
gem 'coveralls'

gem 'overcommit'
gem 'ruby_dep', '1.5.0'

platforms :mri, :mingw do
gem 'pry', require: false
Expand Down
11 changes: 5 additions & 6 deletions annotate.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Gem::Specification.new do |s|
s.name = 'annotate'
s.version = Annotate.version

s.required_ruby_version = '>= 2.4.0'
s.required_ruby_version = '>= 3.0.0'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Setting s.required_ruby_version to >= 3.0.0 is a significant change. While this may be necessary for future Rails versions like Rails 8, it will make new versions of annotate_models incompatible for projects using older Ruby versions (e.g., 2.5-2.7), even if those projects are on Rails versions (like Rails 5.x, 6.x) that are otherwise within the activerecord dependency range of ['>= 3.2', '< 9.0'].

For instance, a project on Rails 6.0 with Ruby 2.7 would no longer be able to update annotate_models to this version or newer without also upgrading their Ruby environment.

This effectively means dropping support for Ruby versions older than 3.0. Please consider the following:

  1. Is this breaking change intentional and its impact assessed?
  2. If so, this change should ideally be part of a major version bump for the gem (e.g., from 3.x.x to 4.x.x) and be clearly communicated in the changelog.
  3. The activerecord lower bound of >= 3.2 might also need reconsideration or clarification, as these very old ActiveRecord versions are not practically usable with Ruby 3.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.'
Expand All @@ -18,15 +18,14 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/ctran/annotate_models'
s.licenses = ['Ruby']
s.require_paths = ['lib']
s.rubygems_version = '2.1.11'
s.summary = 'Annotates Rails Models, routes, fixtures, and others based on the database schema.'

s.specification_version = 4 if s.respond_to? :specification_version
s.add_runtime_dependency(%q<rake>, '>= 10.4', '< 14.0')
s.add_runtime_dependency(%q<activerecord>, ['>= 3.2', '< 8.0'])
s.add_dependency(%q<rake>, '>= 10.4', '< 14.0')
s.add_dependency(%q<activerecord>, ['>= 3.2', '< 9.0'])

s.metadata = {
"bug_tracker_uri" => "https://github.com/ctran/annotate_models/issues/",
"source_code_uri" => "https://github.com/ctran/annotate_models.git"
"source_code_uri" => "https://github.com/ctran/annotate_models.git",
'rubygems_mfa_required' => 'true'
}
end
4 changes: 2 additions & 2 deletions lib/annotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.set_defaults(options = {})
end

default_value = ENV[key.to_s] unless ENV[key.to_s].blank?
ENV[key.to_s] = default_value.nil? ? nil : default_value.to_s
ENV[key.to_s] = default_value&.to_s
end
end

Expand Down Expand Up @@ -89,7 +89,7 @@ def self.eager_load(options)
if Rails.version.split('.').first.to_i < 3
Rails.configuration.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
Dir.glob("#{load_path}/**/*.rb").each do |file|
require_dependency file.sub(matcher, '\1')
end
end
Expand Down
30 changes: 15 additions & 15 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module AnnotateModels
}
}.freeze

MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/).freeze
MAGIC_COMMENT_MATCHER = /(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/

class << self
def annotate_pattern(options = {})
Expand Down Expand Up @@ -155,8 +155,8 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
with_comments_column = with_comments_column?(klass, options)

# Precalculate Values
cols_meta = cols.map do |col|
col_comment = with_comments || with_comments_column ? col.comment&.gsub(/\n/, "\\n") : nil
cols_meta = cols.to_h do |col|
col_comment = with_comments || with_comments_column ? col.comment&.gsub("\n", "\\n") : nil
col_type = get_col_type(col)
attrs = get_attributes(col, col_type, klass, options)
col_name = if with_comments && col_comment
Expand All @@ -166,7 +166,7 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
end
simple_formatted_attrs = attrs.join(", ")
[col.name, { col_type: col_type, attrs: attrs, col_name: col_name, simple_formatted_attrs: simple_formatted_attrs, col_comment: col_comment }]
end.to_h
end

# Output annotation
bare_max_attrs_length = cols_meta.map { |_, m| m[:simple_formatted_attrs].length }.max
Expand All @@ -179,15 +179,15 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
col_comment = cols_meta[col.name][:col_comment]

if options[:format_rdoc]
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
info << (sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n")
elsif options[:format_yard]
info << sprintf("# @!attribute #{col_name}") + "\n"
info << ("# @!attribute #{col_name}" + "\n")
ruby_class = col.respond_to?(:array) && col.array ? "Array<#{map_col_type_to_ruby_classes(col_type)}>": map_col_type_to_ruby_classes(col_type)
info << sprintf("# @return [#{ruby_class}]") + "\n"
info << ("# @return [#{ruby_class}]" + "\n")
elsif options[:format_markdown]
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
type_remainder = (md_type_allowance - 2) - col_type.length
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
info << ((sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n")
elsif with_comments_column
info << format_default(col_name, max_size, col_type, bare_type_allowance, simple_formatted_attrs, bare_max_attrs_length, col_comment)
else
Expand Down Expand Up @@ -423,7 +423,7 @@ def get_check_constraint_info(klass, options = {})
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
# :before, :top, :after or :bottom. Default is :before.
#
def annotate_one_file(file_name, info_block, position, options = {})
def annotate_one_file?(file_name, info_block, position, options = {})
return false unless File.exist?(file_name)
old_content = File.read(file_name)
return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
Expand Down Expand Up @@ -486,7 +486,7 @@ def magic_comments_as_string(content)
end
end

def remove_annotation_of_file(file_name, options = {})
def remove_annotation_of_file?(file_name, options = {})
if File.exist?(file_name)
content = File.read(file_name)
return false if content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
Expand Down Expand Up @@ -542,7 +542,7 @@ def annotate(klass, file, header, options = {})
model_file_name = File.join(file)
annotated = []

if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class))
if annotate_one_file?(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class))
annotated << model_file_name
end

Expand All @@ -563,7 +563,7 @@ def annotate(klass, file, header, options = {})
.map { |f| expand_glob_into_files(f) }
.flatten
.each do |f|
if annotate_one_file(f, info, position_key, options_with_position(options, position_key))
if annotate_one_file?(f, info, position_key, options_with_position(options, position_key))
annotated << f
end
end
Expand Down Expand Up @@ -694,7 +694,7 @@ def parse_options(options = {})
end

def split_model_dir(option_value)
option_value = option_value.is_a?(Array) ? option_value : option_value.split(',')
option_value = option_value.split(',') unless option_value.is_a?(Array)
option_value.map(&:strip).reject(&:empty?)
end

Expand Down Expand Up @@ -762,13 +762,13 @@ def remove_annotations(options = {})
model_name = klass.name.underscore
table_name = klass.table_name
model_file_name = file
deannotated_klass = true if remove_annotation_of_file(model_file_name, options)
deannotated_klass = true if remove_annotation_of_file?(model_file_name, options)

get_patterns(options, matched_types(options))
.map { |f| resolve_filename(f, model_name, table_name) }
.each do |f|
if File.exist?(f)
remove_annotation_of_file(f, options)
remove_annotation_of_file?(f, options)
deannotated_klass = true
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# Released under the same license as Ruby. No Support. No Warranty.
#

require_relative './annotate_routes/helpers'
require_relative './annotate_routes/header_generator'
require_relative 'annotate_routes/helpers'
require_relative 'annotate_routes/header_generator'

module AnnotateRoutes
class << self
Expand Down Expand Up @@ -95,7 +95,7 @@ def rewrite_contents(existing_text, new_text, frozen)
def annotate_routes(header, content, header_position, options = {})
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
if %w(before top).include?(options[:position_in_routes])
header = header << '' if content.first != ''
header <<= '' if content.first != ''
magic_comments_map << '' if magic_comments_map.any?
new_content = magic_comments_map + header + content
else
Expand Down
16 changes: 7 additions & 9 deletions lib/annotate/annotate_routes/header_generator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative './helpers'
require_relative 'helpers'

module AnnotateRoutes
class HeaderGenerator
Expand All @@ -16,7 +16,7 @@ def generate(options = {})
private

def routes_map(options)
result = `rake routes`.chomp("\n").split(/\n/, -1)
result = `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
Expand All @@ -29,7 +29,7 @@ def routes_map(options)
# Skip routes which match given regex
# Note: it matches the complete line (route_name, path, controller/action)
if regexp_for_ignoring_routes
result.reject { |line| line =~ regexp_for_ignoring_routes }
result.grep_v(regexp_for_ignoring_routes)
else
result
end
Expand All @@ -44,18 +44,16 @@ def initialize(options, routes_map)
def generate
magic_comments_map, contents_without_magic_comments = Helpers.extract_magic_comments_from_array(routes_map)

out = []

magic_comments_map.each do |magic_comment|
out << magic_comment
out = magic_comments_map.map do |magic_comment|
magic_comment
end
out << '' if magic_comments_map.any?

out << comment(options[:wrapper_open]) if options[:wrapper_open]

out << comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required
out << (comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required)
out << comment
return out if contents_without_magic_comments.size.zero?
return out if contents_without_magic_comments.empty?

maxs = [HEADER_ROW.map(&:size)] + contents_without_magic_comments[1..-1].map { |line| line.split.map(&:size) }

Expand Down
4 changes: 2 additions & 2 deletions lib/annotate/annotate_routes/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AnnotateRoutes
module Helpers
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
MAGIC_COMMENT_MATCHER = /(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/

class << self
# TODO: write the method doc using ruby rdoc formats
Expand All @@ -15,7 +15,7 @@ def strip_annotations(content)
mode = :content
header_position = 0

content.split(/\n/, -1).each_with_index do |line, line_number|
content.split("\n", -1).each_with_index do |line, line_number|
if mode == :header && line !~ /\s*#/
mode = :content
real_content << line unless line.blank?
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate/constants.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Annotate
module Constants
TRUE_RE = /^(true|t|yes|y|1)$/i.freeze
TRUE_RE = /^(true|t|yes|y|1)$/i

##
# The set of available options to customize the behavior of Annotate.
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Annotate
# Class for handling command line arguments
class Parser # rubocop:disable Metrics/ClassLength
class Parser
def self.parse(args, env = {})
new(args, env).parse
end
Expand Down
Loading
Loading