Skip to content

Commit

Permalink
fix: tags field in select mode (#3603)
Browse files Browse the repository at this point in the history
* fix: tags field in select mode

* fix test

* fix test

* fix test

* add test

* lint
  • Loading branch information
Paul-Bob authored Jan 22, 2025
1 parent 1e74a91 commit 565851b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
disabled: disabled?,
placeholder: @field.placeholder,
style: @field.get_html(:style, view: view, element: :input),
value: @field.select_mode? ? @field.field_value.first : @field.field_value.to_json
value: @field.field_value.to_json
%>
<% end %>
4 changes: 2 additions & 2 deletions lib/avo/fields/tags_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def field_value
acts_as_taggable_on_values.map { |value| {value:} }.as_json
else
# Wrap the value on Array to ensure select mode compatibility
Array(value) || []
Array.wrap(value) || []
end
end

Expand Down Expand Up @@ -78,7 +78,7 @@ def fill_acts_as_taggable(record, key, value, params)
def whitelist_items
return suggestions.to_json if enforce_suggestions

(suggestions + field_value).to_json
(suggestions + field_value).uniq.to_json
end

def suggestions
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ class Project < ApplicationRecord
def self.ransackable_attributes(auth_object = nil)
["budget", "country", "created_at", "description", "id", "meta", "name", "progress", "stage", "started_at", "status", "updated_at", "users_required"]
end

# Used to test tags on select mode with {value:,label:}
def dummy_field=(value)
TestBuddy.hi("dummy_field value is '#{value}'")
end
end
2 changes: 1 addition & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
Expand Down
52 changes: 52 additions & 0 deletions spec/system/avo/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,76 @@
it "on index" do
Avo::Resources::Project.with_temporary_items do
field :stage, as: :tags, mode: :select
field :dummy_field, as: :tags,
mode: :select,
close_on_select: true,
format_using: -> { [{value: "a_c2", label: "activity_category2"}] },
suggestions: [
{value: "a_c1", label: "activity_category1"},
{value: "a_c2", label: "activity_category2"},
{value: "a_c3", label: "activity_category3"}
]
end

visit avo.resources_projects_path

expect(page).to have_text("activity_category2")
expect(page).not_to have_text("a_c2")

projects.each do |project|
expect(page).to have_text(project.stage)
end

Avo::Resources::Project.restore_items_from_backup
end

it "on show" do
Avo::Resources::Project.with_temporary_items do
field :stage, as: :tags, mode: :select
field :dummy_field, as: :tags,
mode: :select,
close_on_select: true,
format_using: -> { [{value: "a_c2", label: "activity_category2"}] },
suggestions: [
{value: "a_c1", label: "activity_category1"},
{value: "a_c2", label: "activity_category2"},
{value: "a_c3", label: "activity_category3"}
]
end

visit avo.resources_project_path(projects.first)

expect(page).to have_text("activity_category2")
expect(page).not_to have_text("a_c2")

expect(page).to have_text(projects.first.stage)

Avo::Resources::Project.restore_items_from_backup
end

it "on edit / update" do
expect(TestBuddy).to receive(:hi).with("dummy_field value is 'a_c2'").at_least :once

Avo::Resources::Project.with_temporary_items do
field :stage, as: :tags, mode: :select
field :dummy_field, as: :tags,
mode: :select,
close_on_select: true,
format_using: -> { [{value: "a_c2", label: "activity_category2"}] },
suggestions: [
{value: "a_c1", label: "activity_category1"},
{value: "a_c2", label: "activity_category2"},
{value: "a_c3", label: "activity_category3"}
]
end

visit avo.edit_resources_project_path(projects.first)

sleep 1

expect(page).not_to have_text("a_c2")
expect(page).to have_text("activity_category2")

expect(page).to have_text(projects.first.stage)

click_on "Save"
Expand Down

0 comments on commit 565851b

Please sign in to comment.