Skip to content

Commit

Permalink
fix: persist values after validation errors on tags fields that acts …
Browse files Browse the repository at this point in the history
…as taggable on (#2868)

* fix: persist values after validation errors on tags fields that acts as taggable on

* lint

* test

* fix test
  • Loading branch information
Paul-Bob authored Jun 19, 2024
1 parent 267344c commit 6453b22
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/avo/fields/tags_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ def field_value
end

def json_value
value.map do |item|
{
value: item.name
}
end.as_json
acts_as_taggable_on_values.map { |value| {value:} }.as_json
end

def acts_as_taggable_on_values
# When record is DB persistent the values are fetched from the DB
# Else the array values are fetched from the record using the tag_list_on helper
# values_array examples: ["1", "2"]
# ["example suggestion","example tag"]
if record.persisted?
value.map { |item| item.name }
else
record.tag_list_on(acts_as_taggable_on)
end
end

def fill_field(model, key, value, params)
Expand Down
15 changes: 15 additions & 0 deletions spec/system/avo/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@
expect(post.reload.tag_list.sort).to eq ["1", "2"].sort
end
end

context "new" do
it "keeps acts as taggable tags on validation errors" do
visit avo.new_resources_post_path

add_tag(field: :tags, tag: "one")
add_tag(field: :tags, tag: "two")
add_tag(field: :tags, tag: "five")

save

expect(page).to have_text("Validation failed: Name can't be blank")
expect(page.all(".tagify__tag-text").map(&:text)).to eq ["one", "two"]
end
end
end

describe 'without acts_as_taggable' do
Expand Down

0 comments on commit 6453b22

Please sign in to comment.