Skip to content

Commit

Permalink
fix: invalid date_time (#2279)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Paul-Bob and adrianthedev authored Jan 4, 2024
1 parent 93b6e7b commit 94f6e77
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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 %>
7 changes: 1 addition & 6 deletions app/components/avo/fields/has_many_field/show_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if @field.value %>
<turbo-frame id="<%= @field.turbo_frame %>" src="<%= @field.frame_url %>" target="_top" class="block">
<turbo-frame id="<%= @field.turbo_frame %>" src="<%= @field.frame_url %>" loading=<%= turbo_frame_loading %> target="_top" class="block">
<%= render(Avo::LoadingComponent.new(title: @field.name)) %>
</turbo-frame>
<% else %>
Expand Down
1 change: 1 addition & 0 deletions app/components/avo/fields/has_one_field/show_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions lib/avo/fields/concerns/frame_loading.rb
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions spec/system/avo/tabs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/system/avo/test_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down

0 comments on commit 94f6e77

Please sign in to comment.