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

enhancement: Detect polymorphic associations in generator #3645

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
167a173
Chceck potential polymorphic association
Nevelito Feb 6, 2025
c238a19
Fix issues in recource generator
Nevelito Feb 6, 2025
7f13454
Change generator option
Nevelito Feb 6, 2025
4c50d1e
small changes
Nevelito Feb 6, 2025
87126a8
few changes
Nevelito Feb 7, 2025
4c2a88a
Merge branch 'main' into detect_associations
Nevelito Feb 7, 2025
b0c824f
fix some rubocop request changes
Nevelito Feb 7, 2025
d82593c
next changes
Nevelito Feb 7, 2025
b3ea027
request changes
Nevelito Feb 7, 2025
a61cf12
Change generator
Nevelito Feb 7, 2025
d370bc2
Next attempt to fix finding polymorphic associations
Nevelito Feb 7, 2025
15c97fb
fix standardrb
Nevelito Feb 7, 2025
33ba42d
small changes
Nevelito Feb 7, 2025
6da8b54
delete debugger
Nevelito Feb 7, 2025
7fd71a3
change order in generate_fields
Nevelito Feb 7, 2025
71edde5
optymalize code
Nevelito Feb 7, 2025
6c17503
test spec
Nevelito Feb 7, 2025
f9e388d
Add comments
Nevelito Feb 7, 2025
216f486
final changes
Nevelito Feb 7, 2025
1606c2c
fix standardrb
Nevelito Feb 7, 2025
0007f80
fix spec and optymalize code
Nevelito Feb 10, 2025
3a09267
fix spec
Nevelito Feb 10, 2025
553750e
back changes from review
Nevelito Feb 10, 2025
a3880d1
fix
Nevelito Feb 10, 2025
a8e55b9
final request changes
Nevelito Feb 11, 2025
c200f99
optymalize code
Nevelito Feb 11, 2025
5304baf
add tyes to spec and try to reduce complexity
Nevelito Feb 11, 2025
85e1fa8
Fix spec
Nevelito Feb 11, 2025
7434c50
fix field options
Nevelito Feb 11, 2025
f0ca28d
fix field options
Nevelito Feb 11, 2025
2cde6aa
Request changes
Nevelito Feb 11, 2025
055578b
fix bug with types
Nevelito Feb 11, 2025
a2a78b9
Delete changes in application_record and create new class in resource…
Nevelito Feb 12, 2025
8f0b440
fix standardrb fail
Nevelito Feb 12, 2025
e094037
fix whitespace
Nevelito Feb 12, 2025
d7adb9a
Add comments
Nevelito Feb 12, 2025
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
33 changes: 29 additions & 4 deletions lib/generators/avo/resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,26 @@
end
end

def fields_from_model_tags
tags.each do |name, _|
fields[(remove_last_word_from name).pluralize] = {field: "tags"}
def detect_polymorphic_associations
Copy link

Choose a reason for hiding this comment

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

Method detect_polymorphic_associations has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.

polymorphic_fields = model_db_columns.keys.group_by { |col| col.gsub(/_type|_id$/, '') }

Check failure on line 181 in lib/generators/avo/resource_generator.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. Raw Output: lib/generators/avo/resource_generator.rb:181:94: C: [Corrected] Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. polymorphic_fields = model_db_columns.keys.group_by { |col| col.gsub(/_type|_id$/, '') } ^^
.select { |_field, group| group.size == 2 }

Check failure on line 182 in lib/generators/avo/resource_generator.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 37) spaces for indenting an expression in an assignment spanning multiple lines. Raw Output: lib/generators/avo/resource_generator.rb:182:48: C: [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 37) spaces for indenting an expression in an assignment spanning multiple lines. .select { |_field, group| group.size == 2 } ^^^^^^^

polymorphic_fields.each_key do |field|
# Check if the model has a reflection for the polymorphic association
if model.reflections.key?(field)
reflection = model.reflections[field]
associated_classes = reflection.polymorphic? ? reflection.class_name : nil
else
associated_classes = nil
end

fields[field] = {
field: "polymorphic",
options: {
types: associated_classes ? [associated_classes] : [],
comment: associated_classes ? nil : "This is a potential polymorphic association, but the associated classes could not be determined. Please configure manually."
}
}
end
end

Expand Down Expand Up @@ -256,6 +273,7 @@
fields_from_model_associations
fields_from_model_rich_texts
fields_from_model_tags
detect_polymorphic_associations

generated_fields_template
end
Expand All @@ -272,7 +290,14 @@
options = ""
field_options[:options].each { |k, v| options += ", #{k}: #{v}" } if field_options[:options].present?

fields_string += "\n #{field_string field_name, field_options[:field], options}"
if field_options[:field] == "polymorphic"
types = field_options[:options][:types] || []
comment = field_options[:options][:comment]
fields_string += "\n # #{comment}" if comment
fields_string += "\n # field :#{field_name}, as: :polymorphic, types: #{types.inspect}#{options}"
else
fields_string += "\n #{field_string field_name, field_options[:field], options}"
end
end

fields_string
Expand Down
22 changes: 22 additions & 0 deletions spec/features/avo/generators/resource_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@
end
end
end

context "when generating resources for Comment model" do
it "generates fields for polymorphic associations" do
files = [
Rails.root.join("app", "avo", "resources", "comment.rb").to_s,
Rails.root.join("app", "controllers", "avo", "comments_controller.rb").to_s
]

keeping_original_files(files) do
Rails::Generators.invoke("avo:resource", ["comment", "-q"], {destination_root: Rails.root})

expect(File.exist?(files[0])).to be true

generated_content = File.read(files[0])

expect(generated_content).to include("# This is a potential polymorphic association, but the associated classes could not be determined. Please configure manually.")
expect(generated_content).to include("field :commentable, as: :polymorphic")

check_files_and_clean_up files
end
end
end
end

def keeping_original_files(files)
Expand Down
Loading