Skip to content
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

Nested module models and unexpected annotations #106

Closed
lparry opened this issue Apr 11, 2024 · 6 comments
Closed

Nested module models and unexpected annotations #106

lparry opened this issue Apr 11, 2024 · 6 comments

Comments

@lparry
Copy link

lparry commented Apr 11, 2024

Hi, I'm seeing some odd behaviour for a model nested inside a name space with annotations previously generated by annotate_models

eg, we have (roughly, excuse any errors anonymising stuff)

# == Schema Information
#
# Table name: my_models
#
#  id         :bigint(8)        not null, primary key
#  name       :string           not null
#
# Indexes
#
#  index_my_models_on_name  (name) UNIQUE
module MyModule
  # a long comment
  # over a few lines
  # more explanation
  class MyModel < ApplicationModel
  end
end

after running annotaterb I end up with a duplicate index + newline like this:

# == Schema Information
#
# Table name: my_models
#
#  id         :bigint(8)        not null, primary key
#  name       :string           not null
#
# Indexes
#
#  index_my_models_on_name  (name) UNIQUE
#
#  index_my_models_on_name  (name) UNIQUE
module MyModule
  # a long comment
  # over a few lines
  # more explanation
  class MyModel < ApplicationModel
  end
end

removing the annotations and starting from scratch letting annotaterb pick where it thinks they belong, I end up with

module MyModule
  # a long comment
  # over a few lines
# == Schema Information
#
# Table name: my_models
#
#  id         :bigint(8)        not null, primary key
#  name       :string           not null
#
# Indexes
#
#  index_my_models_on_name  (name) UNIQUE
  # more explanation
  class MyModel < ApplicationModel
  end
end

with the annotations inserted midway through the comment, and with an outdent as above. Any pointers which config options might help here?

@drwl
Copy link
Owner

drwl commented Apr 11, 2024

@lparry thanks for reporting. I'll try and play around with this locally and see what's going on. I suspect the change in behavior is related to changes made for identifying the model annotations, done here #72

In short, I changed from using a regex to identify annotation comment block to using Ripper (Ruby file parser) to improve it. The additional indexes line seems interesting though.

@drwl
Copy link
Owner

drwl commented Apr 22, 2024

@lparry just checking - is the actual annotation formatted like that? I think there should be an empty comment line break after the index and I believe it should look like:

# == Schema Information
#
# Table name: my_models
#
#  id         :bigint(8)        not null, primary key
#  name       :string           not null
#
# Indexes
#
#  index_my_models_on_name  (name) UNIQUE
# 
module MyModule
  # a long comment
  # over a few lines
  # more explanation
  class MyModel < ApplicationModel
  end
end

I'm able to reproduce the issue using the annotation you have (with the missing comment line break) but I think that's not expected with this gem.

@drwl
Copy link
Owner

drwl commented Apr 22, 2024

For reference, I added a basic index to a dummy model and ran annotaterb and this is the result of the file:

# frozen_string_literal: true

# == Schema Information
#
# Table name: test_defaults
#
#  id         :integer          not null, primary key
#  boolean    :boolean          default(FALSE)
#  date       :date             default(Tue, 04 Jul 2023)
#  datetime   :datetime         default(Tue, 04 Jul 2023 12:34:56.000000000 UTC +00:00)
#  decimal    :decimal(14, 2)   default(43.21)
#  float      :float            default(12.34)
#  integer    :integer          default(99)
#  string     :string           default("hello world!")
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
# Indexes
#
#  index_test_defaults_on_string  (string)
#
class TestDefault < ApplicationRecord
end

@drwl
Copy link
Owner

drwl commented May 4, 2024

@lparry just checking in, was this able to be resolved on your side?

@drwl
Copy link
Owner

drwl commented May 14, 2024

@lparry I'm going to close this but feel free to re-open if the issue wasn't resolved.

@drwl drwl closed this as completed May 14, 2024
@yskkin
Copy link
Contributor

yskkin commented Jan 19, 2025

I have same issue.
A model class nested in parent module gives annotation whose indent is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants