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

fix: destroy when using forms and display fields API #2415

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
avo (3.3.1)
avo (3.3.2)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
1 change: 1 addition & 0 deletions app/components/avo/index/resource_controls_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def hidden_params
hidden = {}

hidden[:view_type] = params[:view_type] if params[:view_type]
hidden[:view] = resource.view.to_s

if params[:turbo_frame]
hidden[:turbo_frame] = params[:turbo_frame]
Expand Down
21 changes: 20 additions & 1 deletion app/controllers/avo/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,26 @@ def detect_fields
def set_related_resource
raise Avo::MissingResourceError.new(related_resource_name) if related_resource.nil?

@related_resource = related_resource.new(params: params, view: action_name.to_sym, user: _current_user, record: @related_record).detect_fields
action_view = action_name.to_sym

# Get view from params unless actions is index or show or forms...
# Else, for example for detach action we want the view from params to can fetch the correct fields
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
# This logic avoid the following scenario:
# When a has many field is rendered the action is index and params[:view] is show or edit but we want to
# keep @view as index for the related_resource
# Same do not happen with other actions except the list below.
view = if action_view.in?([:index, :show, :new, :edit, :create])
action_view
else
params[:view] || action_view
end

@related_resource = related_resource.new(
params: params,
view: view,
user: _current_user,
record: @related_record
).detect_fields
end

def set_record
Expand Down
11 changes: 10 additions & 1 deletion spec/dummy/app/avo/resources/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ class Avo::Resources::Course < Avo::BaseResource
self.keep_filters_panel_open = true
self.stimulus_controllers = "city-in-country toggle-fields"

def fields

def display_fields
fields_bag
end

def form_fields
fields_bag
end

def fields_bag
field :id, as: :id
field :name, as: :text, html: {
edit: {
Expand Down
13 changes: 10 additions & 3 deletions spec/features/avo/fields_methods_for_views_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def index_fields
end

it "shows only the specified fields for display views" do
# Store the original method
original_display_fields = Avo::Resources::Course.instance_method(:display_fields)

Avo::Resources::Course.class_eval do
def display_fields
field :id, as: :id
Expand Down Expand Up @@ -102,12 +105,16 @@ def display_fields
expect(page).not_to have_selector 'turbo-frame[id="has_many_field_show_links"]'


# Restore the original method
Avo::Resources::Course.class_eval do
remove_method :display_fields
define_method(:display_fields, original_display_fields)
end
end

it "shows only the specified fields for form views" do
# Store the original method
original_form_fields = Avo::Resources::Course.instance_method(:form_fields)

Avo::Resources::Course.class_eval do
def form_fields
field :name, as: :text
Expand Down Expand Up @@ -138,9 +145,9 @@ def form_fields
expect(page).not_to have_css "[data-field-id='city']"
expect(page).not_to have_selector 'turbo-frame[id="has_many_field_show_links"]'


# Restore the original method
Avo::Resources::Course.class_eval do
remove_method :form_fields
define_method(:form_fields, original_form_fields)
end
end
end
Expand Down