From 94f6e77e61ed7ad48dce766830f2e338380cbc4c Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:00:20 +0200 Subject: [PATCH] fix: invalid date_time (#2279) * fix: invalid date_time * Update lib/avo/fields/concerns/frame_loading.rb * rm wrong include * revert frames helper include * add use case * change use case * spec * fix unrelated spec * JS fix * Rename loading variable * apply rename loading on `app/components/avo/fields/has_many_field/show_component.html.erb` --------- Co-authored-by: Adrian Marin --- .../fields/has_many_field/show_component.html.erb | 2 +- .../avo/fields/has_many_field/show_component.rb | 7 +------ .../fields/has_one_field/show_component.html.erb | 2 +- .../avo/fields/has_one_field/show_component.rb | 1 + .../js/controllers/fields/date_field_controller.js | 2 +- lib/avo/fields/concerns/frame_loading.rb | 14 ++++++++++++++ spec/dummy/app/avo/resources/post.rb | 1 + spec/dummy/app/avo/resources/user.rb | 6 ++++++ spec/dummy/app/models/user.rb | 1 + spec/system/avo/tabs_spec.rb | 14 ++++++++++++-- spec/system/avo/test_helpers_spec.rb | 2 +- 11 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 lib/avo/fields/concerns/frame_loading.rb diff --git a/app/components/avo/fields/has_many_field/show_component.html.erb b/app/components/avo/fields/has_many_field/show_component.html.erb index aa10c54efd..1dd1c054f0 100644 --- a/app/components/avo/fields/has_many_field/show_component.html.erb +++ b/app/components/avo/fields/has_many_field/show_component.html.erb @@ -1,3 +1,3 @@ -<%= turbo_frame_tag @field.turbo_frame, src: @field.frame_url, loading: loading, target: :_top, class: "block" do %> +<%= turbo_frame_tag @field.turbo_frame, src: @field.frame_url, loading: turbo_frame_loading, target: :_top, class: "block" do %> <%= render(Avo::LoadingComponent.new(title: @field.plural_name)) %> <% end %> diff --git a/app/components/avo/fields/has_many_field/show_component.rb b/app/components/avo/fields/has_many_field/show_component.rb index 2f2d45246d..116308bb71 100644 --- a/app/components/avo/fields/has_many_field/show_component.rb +++ b/app/components/avo/fields/has_many_field/show_component.rb @@ -2,10 +2,5 @@ class Avo::Fields::HasManyField::ShowComponent < Avo::Fields::ShowComponent include Turbo::FramesHelper - - def turbo_frame_loading = kwargs[:turbo_frame_loading] - - def loading - turbo_frame_loading || params[:turbo_frame_loading] || "eager" - end + include Avo::Fields::Concerns::FrameLoading end diff --git a/app/components/avo/fields/has_one_field/show_component.html.erb b/app/components/avo/fields/has_one_field/show_component.html.erb index 98a36626f6..45b7f42377 100644 --- a/app/components/avo/fields/has_one_field/show_component.html.erb +++ b/app/components/avo/fields/has_one_field/show_component.html.erb @@ -1,5 +1,5 @@ <% if @field.value %> - + target="_top" class="block"> <%= render(Avo::LoadingComponent.new(title: @field.name)) %> <% else %> diff --git a/app/components/avo/fields/has_one_field/show_component.rb b/app/components/avo/fields/has_one_field/show_component.rb index e7127bb5ad..15bb9fbdcc 100644 --- a/app/components/avo/fields/has_one_field/show_component.rb +++ b/app/components/avo/fields/has_one_field/show_component.rb @@ -2,6 +2,7 @@ class Avo::Fields::HasOneField::ShowComponent < Avo::Fields::ShowComponent include Avo::ApplicationHelper + include Avo::Fields::Concerns::FrameLoading def can_attach? policy_result = true diff --git a/app/javascript/js/controllers/fields/date_field_controller.js b/app/javascript/js/controllers/fields/date_field_controller.js index 6a204b0dff..182144c54a 100644 --- a/app/javascript/js/controllers/fields/date_field_controller.js +++ b/app/javascript/js/controllers/fields/date_field_controller.js @@ -78,7 +78,7 @@ export default class extends Controller { // Parse the time as if it were UTC get parsedValue() { - return DateTime.fromISO(this.initialValue, { zone: 'UTC' }) + return DateTime.fromISO(this.initialValue.trim(), { zone: 'UTC' }) } get displayTimezone() { diff --git a/lib/avo/fields/concerns/frame_loading.rb b/lib/avo/fields/concerns/frame_loading.rb new file mode 100644 index 0000000000..dcbbc1242c --- /dev/null +++ b/lib/avo/fields/concerns/frame_loading.rb @@ -0,0 +1,14 @@ +module Avo + module Fields + module Concerns + module FrameLoading + extend ActiveSupport::Concern + + def turbo_frame_loading + kwargs[:turbo_frame_loading] || params[:turbo_frame_loading] || "eager" + end + end + end + end +end + diff --git a/spec/dummy/app/avo/resources/post.rb b/spec/dummy/app/avo/resources/post.rb index 3eb5db4642..f5d07853d1 100644 --- a/spec/dummy/app/avo/resources/post.rb +++ b/spec/dummy/app/avo/resources/post.rb @@ -25,6 +25,7 @@ class Avo::Resources::Post < Avo::BaseResource def fields field :id, as: :id field :name, required: true, sortable: true + field :created_at, as: :date_time field :body, as: :trix, placeholder: "Enter text", diff --git a/spec/dummy/app/avo/resources/user.rb b/spec/dummy/app/avo/resources/user.rb index 3b0a792192..0876dc6211 100644 --- a/spec/dummy/app/avo/resources/user.rb +++ b/spec/dummy/app/avo/resources/user.rb @@ -222,6 +222,11 @@ def first_tabs_group test_tab test_field("Inside tabs") first_tabs_group_fields + tab "Created at" do + panel do + field :created_at, as: :date_time + end + end end end @@ -240,6 +245,7 @@ def second_tabs_group # show_on: :edit, scope: -> { query.starts_with parent.first_name[0].downcase }, description: "The comments listed in the attach modal all start with the name of the parent user." + field :comment, as: :has_one, name: "Main comment" end end diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index 59b78331a3..cec09cc7fe 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -30,6 +30,7 @@ class User < ApplicationRecord validates :last_name, presence: true has_one :post + has_one :comment has_one :fish has_many :posts, inverse_of: :user has_many :people diff --git a/spec/system/avo/tabs_spec.rb b/spec/system/avo/tabs_spec.rb index c4de9ce7f3..914012bdb5 100644 --- a/spec/system/avo/tabs_spec.rb +++ b/spec/system/avo/tabs_spec.rb @@ -48,7 +48,7 @@ visit avo.resources_user_path user within first_tab_group do - expect(find('[data-tabs-target="tabSwitcher"]').text).to eq "Fish\nTeams\nPeople\nSpouses\nProjects\nTeam memberships" + expect(find('[data-tabs-target="tabSwitcher"]').text).to eq "Fish\nTeams\nPeople\nSpouses\nProjects\nTeam memberships\nCreated at" end end end @@ -101,7 +101,7 @@ scroll_to first_tab_group within first_tab_group do - expect(find('[data-tabs-target="tabSwitcher"]')).to have_text "Fish\nTeams\nPeople\nSpouses\nProjects\nTeam memberships", exact: true + expect(find('[data-tabs-target="tabSwitcher"]')).to have_text "Fish\nTeams\nPeople\nSpouses\nProjects\nTeam memberships\nCreated at", exact: true end end end @@ -162,4 +162,14 @@ find('a[data-selected="true"][data-tabs-tab-name-param="Team memberships"]') end end + + it "date_time field works on tabs" do + visit avo.resources_user_path user + + find('a[data-selected="false"][data-tabs-tab-name-param="Main comment"]').click + expect(page).not_to have_text 'Invalid DateTime' + + find('a[data-selected="false"][data-tabs-tab-name-param="Created at"]').click + expect(page).not_to have_text 'Invalid DateTime' + end end diff --git a/spec/system/avo/test_helpers_spec.rb b/spec/system/avo/test_helpers_spec.rb index 8d37dc5dc0..4c82c86ecf 100644 --- a/spec/system/avo/test_helpers_spec.rb +++ b/spec/system/avo/test_helpers_spec.rb @@ -73,7 +73,7 @@ it "finds the wrapper" do users.each do |user| visit "admin/resources/users/#{user.id}" - has_one_post = has_one_field_wrapper(id: :post) + scroll_to(has_one_post = has_one_field_wrapper(id: :post)) name_wrapper = within(has_one_post) { show_field_wrapper(id: "name", type: "text") } name_wrapper_without_type = within(has_one_post) { show_field_wrapper(id: "name") }