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 all commits
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
48 changes: 43 additions & 5 deletions lib/generators/avo/resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,34 @@ def fields_from_model_tags

def fields_from_model_associations
associations.each do |name, association|
fields[name] = if association.is_a? ActiveRecord::Reflection::ThroughReflection
field_from_through_association(association)
else
::Avo::Mappings::ASSOCIATIONS_MAPPING[association.class]
end
fields[name] =
if association.polymorphic?
field_with_polymorphic_association(association)
elsif association.is_a?(ActiveRecord::Reflection::ThroughReflection)
field_from_through_association(association)
else
::Avo::Mappings::ASSOCIATIONS_MAPPING[association.class]
end
end
end

def field_with_polymorphic_association(association)
Nevelito marked this conversation as resolved.
Show resolved Hide resolved
Rails.application.eager_load! unless Rails.application.config.eager_load

types = polymorphic_association_types(association)

{
field: "belongs_to",
options: {
polymorphic_as: ":#{association.name}",
types: types.presence || "[] # Types weren't computed correctly. Please configure them."
}
}
end

def polymorphic_association_types(association)
ActiveRecord::Base.descendants.filter_map do |model|
Inspector.new(model.name) if model.reflect_on_all_associations(:has_many).any? { |assoc| assoc.options[:as] == association.name }
end
end

Expand Down Expand Up @@ -293,5 +316,20 @@ def field(name, type)
end
end
end

# This class modifies the inspect function to correctly handle polymorphic associations.
# It is used in the polymorphic_association_types function.
# Without modification: Model(id: integer, name: string)
# After modification: Model
class Inspector
attr_accessor :name
def initialize(name)
@name = name
end

def inspect
name
end
end
end
end
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 with polymorphic associations" do
it "generates commented polymorphic fields" do
files = [
Rails.root.join("app", "avo", "resources", "review.rb").to_s,
Rails.root.join("app", "controllers", "avo", "reviews_controller.rb").to_s
]

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

# Types load in different order every time
expect(File.read(files[0])).to include("field :reviewable, as: :belongs_to, polymorphic_as: :reviewable, types:")
expect(File.read(files[0])).to include("Team")
expect(File.read(files[0])).to include("Project")
expect(File.read(files[0])).to include("Post")
expect(File.read(files[0])).to include("Fish")

check_files_and_clean_up files
end
end
end
end

def keeping_original_files(files)
Expand Down
Loading