Skip to content

Commit

Permalink
fix: detach when using view *_fields api (#2436)
Browse files Browse the repository at this point in the history
* fix: detach when using view *_fields api

* fix: attach

* fix tests

* fix discreet pagination test

* test
  • Loading branch information
Paul-Bob authored Feb 6, 2024
1 parent b5f0efa commit a29d3dd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/components/avo/index/resource_controls_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def hidden_params
hidden = {}

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

if params[:turbo_frame]
hidden[:turbo_frame] = params[:turbo_frame]
Expand Down
2 changes: 1 addition & 1 deletion app/components/avo/views/resource_index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def create_path
def attach_path
current_path = CGI.unescape(request.env["PATH_INFO"]).split("/").select(&:present?)

Avo.root_path(paths: [*current_path, "new"])
Avo.root_path(paths: [*current_path, "new"], query: { view: @parent_resource&.view&.to_s })
end

def singular_resource_name
Expand Down
10 changes: 6 additions & 4 deletions spec/dummy/app/avo/resources/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ class Avo::Resources::Course < Avo::BaseResource
self.keep_filters_panel_open = true
self.stimulus_controllers = "city-in-country toggle-fields"

def show_fields
fields_bag
field :links, as: :has_many, searchable: true, placeholder: "Click to choose a link",
discreet_pagination: true
end

def display_fields
def index_fields
fields_bag
end

Expand Down Expand Up @@ -94,9 +99,6 @@ def fields_bag
options: Course.cities.values.flatten.map { |city| [city, city] }.to_h,
display_value: false

field :links, as: :has_many, searchable: true, placeholder: "Click to choose a link",
discreet_pagination: true

if params[:show_location_field] == '1'
# Example for error message when resource is missing
field :locations, as: :has_and_belongs_to_many
Expand Down
4 changes: 2 additions & 2 deletions spec/features/avo/discreet_pagination_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

describe "with one page" do
it "should not have pagination" do
visit "/admin/resources/courses/#{course_with_one_page_of_links.id}/links?turbo_frame=has_many_field_show_links"
visit "/admin/resources/courses/#{course_with_one_page_of_links.id}/links?turbo_frame=has_many_field_show_links&view=show"
wait_for_loaded

expect(current_path).to eql "/admin/resources/courses/#{course_with_one_page_of_links.id}/links"
Expand All @@ -22,7 +22,7 @@

describe "with two pages" do
it "should have pagination" do
visit "/admin/resources/courses/#{course_with_two_pages_of_links.id}/links?turbo_frame=has_many_field_show_links"
visit "/admin/resources/courses/#{course_with_two_pages_of_links.id}/links?turbo_frame=has_many_field_show_links&view=show"
wait_for_loaded

expect(current_path).to eql "/admin/resources/courses/#{course_with_two_pages_of_links.id}/links"
Expand Down
28 changes: 23 additions & 5 deletions spec/features/avo/fields_methods_for_views_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'rails_helper'

RSpec.feature "Fields methods for each view", type: :feature do
let!(:course) { create :course }
let!(:links) { create_list :course_link, 3 }
let!(:course) { create :course, links: links }

context "with `fields` declaration" do
it "shows all the fields" do
Expand All @@ -20,6 +21,8 @@

context "with `view_fields` declaration" do
it "shows only the specified fields for show view" do
original_show_fields = Avo::Resources::Course.instance_method(:show_fields)

Avo::Resources::Course.class_eval do
def show_fields
field :name, as: :text
Expand All @@ -40,11 +43,13 @@ def show_fields
expect(page).not_to have_selector 'turbo-frame[id="has_many_field_show_links"]'

Avo::Resources::Course.class_eval do
remove_method :show_fields
define_method(:show_fields, original_show_fields)
end
end

it "shows only the specified fields for index view" do
original_index_fields = Avo::Resources::Course.instance_method(:index_fields)

Avo::Resources::Course.class_eval do
def index_fields
field :id, as: :id
Expand All @@ -64,15 +69,19 @@ def index_fields
expect(page).not_to have_selector 'turbo-frame[id="has_many_field_show_links"]'

Avo::Resources::Course.class_eval do
remove_method :index_fields
define_method(:index_fields, original_index_fields)
end
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)
original_show_fields = Avo::Resources::Course.instance_method(:show_fields)
original_index_fields = Avo::Resources::Course.instance_method(:index_fields)

Avo::Resources::Course.class_eval do
remove_method :show_fields
remove_method :index_fields

def display_fields
field :id, as: :id
field :name, as: :text
Expand Down Expand Up @@ -107,7 +116,8 @@ def display_fields

# Restore the original method
Avo::Resources::Course.class_eval do
define_method(:display_fields, original_display_fields)
define_method(:show_fields, original_show_fields)
define_method(:index_fields, original_index_fields)
end
end

Expand Down Expand Up @@ -150,5 +160,13 @@ def form_fields
define_method(:form_fields, original_form_fields)
end
end

it "detach works using show and index fields api" do
visit "#{Avo::Engine.routes.url_helpers.resources_course_path(course)}/links?turbo_frame=has_many_field_links&view=show"

expect {
find("tr[data-resource-id='#{course.links.first.to_param}'] [data-control='detach']").click
}.to change(course.links, :count).by(-1)
end
end
end

0 comments on commit a29d3dd

Please sign in to comment.