Skip to content

Commit 565851b

Browse files
authored
fix: tags field in select mode (#3603)
* fix: tags field in select mode * fix test * fix test * fix test * add test * lint
1 parent 1e74a91 commit 565851b

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

app/components/avo/fields/tags_field/edit_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
disabled: disabled?,
3131
placeholder: @field.placeholder,
3232
style: @field.get_html(:style, view: view, element: :input),
33-
value: @field.select_mode? ? @field.field_value.first : @field.field_value.to_json
33+
value: @field.field_value.to_json
3434
%>
3535
<% end %>

lib/avo/fields/tags_field.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def field_value
3939
acts_as_taggable_on_values.map { |value| {value:} }.as_json
4040
else
4141
# Wrap the value on Array to ensure select mode compatibility
42-
Array(value) || []
42+
Array.wrap(value) || []
4343
end
4444
end
4545

@@ -78,7 +78,7 @@ def fill_acts_as_taggable(record, key, value, params)
7878
def whitelist_items
7979
return suggestions.to_json if enforce_suggestions
8080

81-
(suggestions + field_value).to_json
81+
(suggestions + field_value).uniq.to_json
8282
end
8383

8484
def suggestions

spec/dummy/app/models/project.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ class Project < ApplicationRecord
5151
def self.ransackable_attributes(auth_object = nil)
5252
["budget", "country", "created_at", "description", "id", "meta", "name", "progress", "stage", "started_at", "status", "updated_at", "users_required"]
5353
end
54+
55+
# Used to test tags on select mode with {value:,label:}
56+
def dummy_field=(value)
57+
TestBuddy.hi("dummy_field value is '#{value}'")
58+
end
5459
end

spec/dummy/db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
ActiveRecord::Schema[8.0].define(version: 2024_11_14_165947) do
1414
# These are extensions that must be enabled in order to support this database
15-
enable_extension "plpgsql"
15+
enable_extension "pg_catalog.plpgsql"
1616

1717
create_table "action_text_rich_texts", force: :cascade do |t|
1818
t.string "name", null: false

spec/system/avo/tags_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,24 +241,76 @@
241241
it "on index" do
242242
Avo::Resources::Project.with_temporary_items do
243243
field :stage, as: :tags, mode: :select
244+
field :dummy_field, as: :tags,
245+
mode: :select,
246+
close_on_select: true,
247+
format_using: -> { [{value: "a_c2", label: "activity_category2"}] },
248+
suggestions: [
249+
{value: "a_c1", label: "activity_category1"},
250+
{value: "a_c2", label: "activity_category2"},
251+
{value: "a_c3", label: "activity_category3"}
252+
]
244253
end
245254

246255
visit avo.resources_projects_path
247256

257+
expect(page).to have_text("activity_category2")
258+
expect(page).not_to have_text("a_c2")
259+
248260
projects.each do |project|
249261
expect(page).to have_text(project.stage)
250262
end
263+
264+
Avo::Resources::Project.restore_items_from_backup
251265
end
252266

253267
it "on show" do
268+
Avo::Resources::Project.with_temporary_items do
269+
field :stage, as: :tags, mode: :select
270+
field :dummy_field, as: :tags,
271+
mode: :select,
272+
close_on_select: true,
273+
format_using: -> { [{value: "a_c2", label: "activity_category2"}] },
274+
suggestions: [
275+
{value: "a_c1", label: "activity_category1"},
276+
{value: "a_c2", label: "activity_category2"},
277+
{value: "a_c3", label: "activity_category3"}
278+
]
279+
end
280+
254281
visit avo.resources_project_path(projects.first)
255282

283+
expect(page).to have_text("activity_category2")
284+
expect(page).not_to have_text("a_c2")
285+
256286
expect(page).to have_text(projects.first.stage)
287+
288+
Avo::Resources::Project.restore_items_from_backup
257289
end
258290

259291
it "on edit / update" do
292+
expect(TestBuddy).to receive(:hi).with("dummy_field value is 'a_c2'").at_least :once
293+
294+
Avo::Resources::Project.with_temporary_items do
295+
field :stage, as: :tags, mode: :select
296+
field :dummy_field, as: :tags,
297+
mode: :select,
298+
close_on_select: true,
299+
format_using: -> { [{value: "a_c2", label: "activity_category2"}] },
300+
suggestions: [
301+
{value: "a_c1", label: "activity_category1"},
302+
{value: "a_c2", label: "activity_category2"},
303+
{value: "a_c3", label: "activity_category3"}
304+
]
305+
end
306+
260307
visit avo.edit_resources_project_path(projects.first)
261308

309+
sleep 1
310+
311+
expect(page).not_to have_text("a_c2")
312+
expect(page).to have_text("activity_category2")
313+
262314
expect(page).to have_text(projects.first.stage)
263315

264316
click_on "Save"

0 commit comments

Comments
 (0)