Skip to content

Commit

Permalink
[2.x] Remove last usage of _reflections in favor of reflect_on_associ…
Browse files Browse the repository at this point in the history
…ation. (#3205)

* Remove last usage of _reflections in favor of reflect_on_association.

- better compatible with newer Rails, since it casts to needed type on its own

* improve @reflection class check

* update actions/upload-artifact to v4

* fix breadcrumbs spec

* dry

* last touches

* fix tabs spec

* fix user <-> team association

* appraisal

* try dashboard_spec fix

* revert dashboards spec

* ensure the content is loaded before making assertions

---------

Co-authored-by: Paul Bob <[email protected]>
  • Loading branch information
simi and Paul-Bob authored Sep 11, 2024
1 parent 1fa48c0 commit bbf1d4d
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
id: run_tests
run: bundle exec rspec spec/features

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: always() && steps.run_tests.outcome == 'failure'
with:
name: rspec_failed_screenshots
Expand Down Expand Up @@ -197,7 +197,7 @@ jobs:
id: run_tests
run: bundle exec rspec spec/system

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: always() && steps.run_tests.outcome == 'failure'
with:
name: rspec_failed_screenshots
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def field_value
end

def reflection_class
has_polymorphic_association? ? polymorphic_class : @resource.model_class._reflections[@field.id.to_s].klass
has_polymorphic_association? ? polymorphic_class : @resource.model_class.reflect_on_association(@field.id).klass
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/components/avo/fields/has_one_field/show_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def can_see_the_create_button?
end

def create_path
association_id = @field.resource.model_class._reflections[@field.id.to_s].inverse_of.name
association_id = @field.resource.model_class.reflect_on_association(@field.id).inverse_of.name

args = {
via_relation: association_id,
Expand Down
6 changes: 5 additions & 1 deletion app/components/avo/index/resource_controls_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ def parent_resource
end

def is_has_many_association?
@reflection.is_a?(::ActiveRecord::Reflection::HasManyReflection) || @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
@reflection.class.in? [
ActiveRecord::Reflection::HasManyReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection,
ActiveRecord::Reflection::ThroughReflection
]
end

def referrer_path
Expand Down
16 changes: 13 additions & 3 deletions app/components/avo/views/resource_index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,20 @@ def can_see_the_create_button?
end

def can_attach?
klass = @reflection
klass = @reflection.through_reflection if klass.is_a? ::ActiveRecord::Reflection::ThroughReflection
return false if has_reflection_and_is_read_only

@reflection.present? && klass.is_a?(::ActiveRecord::Reflection::HasManyReflection) && !has_reflection_and_is_read_only && authorize_association_for(:attach)
reflection_class = if @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
@reflection.through_reflection.class
else
@reflection.class
end

return false unless reflection_class.in? [
ActiveRecord::Reflection::HasManyReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection
]

authorize_association_for(:attach)
end

def create_path
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def related_resource

return field.use_resource if field&.use_resource.present?

reflection = @model._reflections[params[:related_name]]
reflection = @model.class.reflect_on_association(params[:related_name])

reflected_model = reflection.klass

Expand Down
26 changes: 15 additions & 11 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def new
def create
association_name = BaseResource.valid_association_name(@model, params[:related_name])

if reflection_class == "HasManyReflection"
if has_many_reflection?
@model.send(association_name) << @attachment_model
else
@model.send("#{association_name}=", @attachment_model)
Expand All @@ -80,7 +80,7 @@ def create
def destroy
association_name = BaseResource.valid_association_name(@model, params[:related_name])

if reflection_class == "HasManyReflection"
if has_many_reflection?
@model.send(association_name).delete @attachment_model
else
@model.send("#{association_name}=", nil)
Expand All @@ -101,7 +101,7 @@ def order
private

def set_reflection
@reflection = @model._reflections[params[:related_name].to_s]
@reflection = @model.class.reflect_on_association(params[:related_name])
end

def set_attachment_class
Expand All @@ -127,12 +127,11 @@ def attachment_id
end

def reflection_class
reflection = @model._reflections[params[:related_name]]

klass = reflection.class.name.demodulize.to_s
klass = reflection.through_reflection.class.name.demodulize.to_s if klass == "ThroughReflection"

klass
if @reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
@reflection.through_reflection.class
else
@reflection.class
end
end

def authorize_if_defined(method)
Expand All @@ -155,14 +154,19 @@ def authorize_detach_action
authorize_if_defined "detach_#{@field.id}?"
end

private

def set_related_authorization
@related_authorization = if related_resource
related_resource.authorization(user: _current_user)
else
Services::AuthorizationService.new _current_user
end
end

def has_many_reflection?
reflection_class.in? [
ActiveRecord::Reflection::HasManyReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection
]
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def create

# This means that the record has been created through another parent record and we need to attach it somehow.
if params[:via_resource_id].present? && params[:via_belongs_to_resource_class].nil?
@reflection = @model._reflections[params[:via_relation]]
@reflection = @model.class.reflect_on_association(params[:via_relation])
# Figure out what kind of association does the record have with the parent record

# Fills in the required infor for belongs_to and has_many
Expand All @@ -148,7 +148,7 @@ def create
end

# For when working with has_one, has_one_through, has_many_through, has_and_belongs_to_many, polymorphic
if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection
if @reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) || @reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
# find the record
via_resource = ::Avo::App.get_resource_by_model_name(params[:via_relation_class]).dup
@related_record = via_resource.find_record params[:via_resource_id], params: params
Expand Down
8 changes: 4 additions & 4 deletions gemfiles/rails_6.0_ruby_3.0.3.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
avo (2.46.0)
avo (2.52.0)
actionview (>= 6.0)
active_link_to
activerecord (>= 6.0)
Expand Down Expand Up @@ -214,7 +214,7 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inline_svg (1.9.0)
inline_svg (1.10.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
iso (0.4.0)
Expand Down Expand Up @@ -266,7 +266,7 @@ GEM
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (6.0.4)
pagy (6.5.0)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -420,7 +420,7 @@ GEM
thread_safe (~> 0.1)
unaccent (0.4.0)
unicode-display_width (2.4.2)
view_component (3.5.0)
view_component (3.14.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand Down
8 changes: 4 additions & 4 deletions gemfiles/rails_6.0_ruby_3.2.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
avo (2.46.0)
avo (2.52.0)
actionview (>= 6.0)
active_link_to
activerecord (>= 6.0)
Expand Down Expand Up @@ -214,7 +214,7 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inline_svg (1.9.0)
inline_svg (1.10.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
iso (0.4.0)
Expand Down Expand Up @@ -266,7 +266,7 @@ GEM
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (6.0.4)
pagy (6.5.0)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -420,7 +420,7 @@ GEM
thread_safe (~> 0.1)
unaccent (0.4.0)
unicode-display_width (2.4.2)
view_component (3.5.0)
view_component (3.14.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand Down
8 changes: 4 additions & 4 deletions gemfiles/rails_6.1_ruby_3.0.3.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
avo (2.46.0)
avo (2.52.0)
actionview (>= 6.0)
active_link_to
activerecord (>= 6.0)
Expand Down Expand Up @@ -218,7 +218,7 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inline_svg (1.9.0)
inline_svg (1.10.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
iso (0.4.0)
Expand Down Expand Up @@ -270,7 +270,7 @@ GEM
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (6.0.4)
pagy (6.5.0)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -423,7 +423,7 @@ GEM
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
unicode-display_width (2.4.2)
view_component (3.5.0)
view_component (3.14.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand Down
8 changes: 4 additions & 4 deletions gemfiles/rails_6.1_ruby_3.2.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
avo (2.46.0)
avo (2.52.0)
actionview (>= 6.0)
active_link_to
activerecord (>= 6.0)
Expand Down Expand Up @@ -218,7 +218,7 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inline_svg (1.9.0)
inline_svg (1.10.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
iso (0.4.0)
Expand Down Expand Up @@ -270,7 +270,7 @@ GEM
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (6.0.4)
pagy (6.5.0)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -423,7 +423,7 @@ GEM
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
unicode-display_width (2.4.2)
view_component (3.5.0)
view_component (3.14.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand Down
8 changes: 4 additions & 4 deletions gemfiles/rails_7.0_ruby_3.0.3.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
avo (2.46.0)
avo (2.52.0)
actionview (>= 6.0)
active_link_to
activerecord (>= 6.0)
Expand Down Expand Up @@ -224,7 +224,7 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inline_svg (1.9.0)
inline_svg (1.10.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
iso (0.4.0)
Expand Down Expand Up @@ -276,7 +276,7 @@ GEM
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (6.0.4)
pagy (6.5.0)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -429,7 +429,7 @@ GEM
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
unicode-display_width (2.4.2)
view_component (3.5.0)
view_component (3.14.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand Down
8 changes: 4 additions & 4 deletions gemfiles/rails_7.0_ruby_3.2.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
avo (2.46.0)
avo (2.52.0)
actionview (>= 6.0)
active_link_to
activerecord (>= 6.0)
Expand Down Expand Up @@ -224,7 +224,7 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inline_svg (1.9.0)
inline_svg (1.10.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
iso (0.4.0)
Expand Down Expand Up @@ -276,7 +276,7 @@ GEM
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (6.0.4)
pagy (6.5.0)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -429,7 +429,7 @@ GEM
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
unicode-display_width (2.4.2)
view_component (3.5.0)
view_component (3.14.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class User < ApplicationRecord
has_many :comments
has_many :team_memberships
has_and_belongs_to_many :projects, inverse_of: :users
has_and_belongs_to_many :teams, join_table: :team_memberships, inverse_of: :admin
has_many :teams, through: :team_memberships, inverse_of: :admin

has_one_attached :cv

Expand Down
Loading

0 comments on commit bbf1d4d

Please sign in to comment.