From 278ba3fccef42211c7e8d1f7d764ba1e3043c265 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Tue, 25 Jun 2024 12:16:40 +0300 Subject: [PATCH 01/19] drop `fetch_labels` in favor of `format_using` --- lib/avo/fields/base_field.rb | 34 +++++++++++++++------------------- lib/avo/fields/tags_field.rb | 19 ++++++++----------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/lib/avo/fields/base_field.rb b/lib/avo/fields/base_field.rb index 675aaffa65..a9ca40b858 100644 --- a/lib/avo/fields/base_field.rb +++ b/lib/avo/fields/base_field.rb @@ -167,20 +167,7 @@ def value(property = nil) final_value = execute_block end - # Run the value through resolver if present - if format_using.present? - final_value = Avo::ExecutionContext.new( - target: format_using, - value: final_value, - record: record, - resource: resource, - view: view, - field: self, - include: self.class.included_modules - ).handle - end - - final_value + apply_format_using final_value end def execute_block @@ -204,17 +191,26 @@ def fill_field(record, key, value, params) record end + def apply_format_using(value) + apply_formatter @format_using, value, record, resource + end + def apply_update_using(record, key, value, resource) - return value if @update_using.nil? + apply_formatter @update_using, value, record, resource, key: + end + + def apply_formatter(target, value, record, resource, **kwargs) + return value if target.nil? Avo::ExecutionContext.new( - target: @update_using, - record:, - key:, + target:, value:, + record:, resource:, + view:, field: self, - include: self.class.included_modules + include: self.class.included_modules, + **kwargs ).handle end diff --git a/lib/avo/fields/tags_field.rb b/lib/avo/fields/tags_field.rb index c547a244f8..7bb2111a85 100644 --- a/lib/avo/fields/tags_field.rb +++ b/lib/avo/fields/tags_field.rb @@ -20,12 +20,17 @@ def initialize(id, **args, &block) add_string_prop args, :suggestions_max_items add_string_prop args, :mode, nil add_string_prop args, :fetch_values_from - add_string_prop args, :fetch_labels + + @format_using ||= args[:fetch_labels] + + unless Rails.env.production? + if args[:fetch_labels].present? + puts "[Avo DEPRECATION WARNING]: The `fetch_labels` field configuration option is no longer supported and will be removed in future versions. Please discontinue its use and solely utilize the `format_using` instead." + end + end end def field_value - return fetched_labels if @fetch_labels.present? - return json_value if acts_as_taggable_on.present? value || [] @@ -81,14 +86,6 @@ def fetch_values_from private - def fetched_labels - if @fetch_labels.respond_to?(:call) - Avo::ExecutionContext.new(target: @fetch_labels, resource: resource, record: record).handle - else - @fetch_labels - end - end - def act_as_taggable_attribute(key) "#{key.singularize}_list=" end From e703208b2a6aaa3ddc4c7595b86cc77db4054d1b Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Tue, 25 Jun 2024 12:43:43 +0300 Subject: [PATCH 02/19] fix use label if item is a hash --- app/components/avo/fields/concerns/item_labels.rb | 2 ++ lib/avo/fields/tags_field.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/components/avo/fields/concerns/item_labels.rb b/app/components/avo/fields/concerns/item_labels.rb index 6f3a5dab63..ff1cda0fcd 100644 --- a/app/components/avo/fields/concerns/item_labels.rb +++ b/app/components/avo/fields/concerns/item_labels.rb @@ -13,6 +13,8 @@ def value_for_item(item) end def label_from_item(item) + return item[:label] if item.is_a?(Hash) && item[:label].present? + value = value_for_item item if suggestions_are_a_hash? diff --git a/lib/avo/fields/tags_field.rb b/lib/avo/fields/tags_field.rb index 7bb2111a85..6bf2917a40 100644 --- a/lib/avo/fields/tags_field.rb +++ b/lib/avo/fields/tags_field.rb @@ -73,7 +73,7 @@ def fill_acts_as_taggable(record, key, value, params) end def suggestions - Avo::ExecutionContext.new(target: @suggestions, record: record).handle + Avo::ExecutionContext.new(target: @suggestions, record: record).handle + field_value end def disallowed From 0729f78a3b82ea911288993ee2c519a890e21536 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Tue, 25 Jun 2024 13:04:31 +0300 Subject: [PATCH 03/19] lint --- lib/avo/fields/base_field.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/avo/fields/base_field.rb b/lib/avo/fields/base_field.rb index a9ca40b858..2de032fcac 100644 --- a/lib/avo/fields/base_field.rb +++ b/lib/avo/fields/base_field.rb @@ -199,7 +199,7 @@ def apply_update_using(record, key, value, resource) apply_formatter @update_using, value, record, resource, key: end - def apply_formatter(target, value, record, resource, **kwargs) + def apply_formatter(target, value, record, resource, **) return value if target.nil? Avo::ExecutionContext.new( @@ -210,7 +210,7 @@ def apply_formatter(target, value, record, resource, **kwargs) view:, field: self, include: self.class.included_modules, - **kwargs + ** ).handle end From cef902756e9ee7083be97d3e62682d17fcc4e8be Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Tue, 25 Jun 2024 13:08:16 +0300 Subject: [PATCH 04/19] revert format_using extraction --- lib/avo/fields/base_field.rb | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/avo/fields/base_field.rb b/lib/avo/fields/base_field.rb index 2de032fcac..675aaffa65 100644 --- a/lib/avo/fields/base_field.rb +++ b/lib/avo/fields/base_field.rb @@ -167,7 +167,20 @@ def value(property = nil) final_value = execute_block end - apply_format_using final_value + # Run the value through resolver if present + if format_using.present? + final_value = Avo::ExecutionContext.new( + target: format_using, + value: final_value, + record: record, + resource: resource, + view: view, + field: self, + include: self.class.included_modules + ).handle + end + + final_value end def execute_block @@ -191,26 +204,17 @@ def fill_field(record, key, value, params) record end - def apply_format_using(value) - apply_formatter @format_using, value, record, resource - end - def apply_update_using(record, key, value, resource) - apply_formatter @update_using, value, record, resource, key: - end - - def apply_formatter(target, value, record, resource, **) - return value if target.nil? + return value if @update_using.nil? Avo::ExecutionContext.new( - target:, - value:, + target: @update_using, record:, + key:, + value:, resource:, - view:, field: self, - include: self.class.included_modules, - ** + include: self.class.included_modules ).handle end From 5c819d7ee3b9d6504451b40a90e7f666f09b4fff Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Tue, 25 Jun 2024 13:57:22 +0300 Subject: [PATCH 05/19] large whitelist items when suggestions are not enforced --- .../fields/tags_field/edit_component.html.erb | 2 +- lib/avo/fields/tags_field.rb | 29 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/components/avo/fields/tags_field/edit_component.html.erb b/app/components/avo/fields/tags_field/edit_component.html.erb index 257938a22d..e863a636b4 100644 --- a/app/components/avo/fields/tags_field/edit_component.html.erb +++ b/app/components/avo/fields/tags_field/edit_component.html.erb @@ -1,7 +1,7 @@ <%= field_wrapper **field_wrapper_args, data: { controller: "tags-field", tags_field_mode_value: @field.mode, - tags_field_whitelist_items_value: @field.suggestions.to_json, + tags_field_whitelist_items_value: @field.whitelist_items, tags_field_disallowed_items_value: @field.disallowed.to_json, tags_field_enforce_suggestions_value: @field.enforce_suggestions, tags_field_suggestions_max_items_value: @field.suggestions_max_items, diff --git a/lib/avo/fields/tags_field.rb b/lib/avo/fields/tags_field.rb index 6bf2917a40..0c1f64918b 100644 --- a/lib/avo/fields/tags_field.rb +++ b/lib/avo/fields/tags_field.rb @@ -31,13 +31,11 @@ def initialize(id, **args, &block) end def field_value - return json_value if acts_as_taggable_on.present? - - value || [] - end - - def json_value - acts_as_taggable_on_values.map { |value| {value:} }.as_json + @field_value ||= if acts_as_taggable_on.present? + acts_as_taggable_on_values.map { |value| {value:} }.as_json + else + value || [] + end end def acts_as_taggable_on_values @@ -72,8 +70,23 @@ def fill_acts_as_taggable(record, key, value, params) record end + def whitelist_items + return suggestions.to_json if enforce_suggestions + + whitelist_items = case [suggestions, field_value] + in Array, Array + suggestions + field_value + in Hash, Hash + suggestions.merge field_value + else + suggestions + end + + whitelist_items.to_json + end + def suggestions - Avo::ExecutionContext.new(target: @suggestions, record: record).handle + field_value + @fetched_suggestions ||= Avo::ExecutionContext.new(target: @suggestions, record: record).handle end def disallowed From d861ad4779807be17cbc25dafc33dee17bc5652a Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 12:59:01 +0300 Subject: [PATCH 06/19] tweaks --- lib/avo/fields/tags_field.rb | 11 +---------- spec/dummy/app/models/course.rb | 2 +- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/avo/fields/tags_field.rb b/lib/avo/fields/tags_field.rb index 0c1f64918b..35ed86d19d 100644 --- a/lib/avo/fields/tags_field.rb +++ b/lib/avo/fields/tags_field.rb @@ -73,16 +73,7 @@ def fill_acts_as_taggable(record, key, value, params) def whitelist_items return suggestions.to_json if enforce_suggestions - whitelist_items = case [suggestions, field_value] - in Array, Array - suggestions + field_value - in Hash, Hash - suggestions.merge field_value - else - suggestions - end - - whitelist_items.to_json + (suggestions + field_value).to_json end def suggestions diff --git a/spec/dummy/app/models/course.rb b/spec/dummy/app/models/course.rb index 00f38305f2..fff5aed3e8 100644 --- a/spec/dummy/app/models/course.rb +++ b/spec/dummy/app/models/course.rb @@ -29,7 +29,7 @@ def has_skills=(value) end def skill_suggestions - ["example suggestion", "example tag", name] + ["example suggestion", "example tag", name].compact end def skill_disallowed From 9b64e3405a53c2503fdf2267408fa49f0d10f544 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 14:11:19 +0300 Subject: [PATCH 07/19] add test --- spec/dummy/app/avo/resources/course.rb | 36 ++++++++++++++------- spec/system/avo/tags_spec.rb | 45 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/spec/dummy/app/avo/resources/course.rb b/spec/dummy/app/avo/resources/course.rb index d38682094f..22fabaecf7 100644 --- a/spec/dummy/app/avo/resources/course.rb +++ b/spec/dummy/app/avo/resources/course.rb @@ -51,22 +51,36 @@ def fields_bag end end - field :skills, + if ENV["TESTING_TAGS_FORMAT_USING"] == "1" + field :skills, as: :tags, - disallowed: -> { record.skill_disallowed }, - suggestions: -> { record.skill_suggestions }, - html: -> do - edit do - wrapper do - classes do - unless record.has_skills - "hidden" + fetch_values_from: "/admin/resources/users/get_users?hey=you&record_id=1", #{value: 1, label: "Jose"} + format_using: -> { + User.find(value).map do |user| + { + value: user.id, + label: user.name + } + end + } + else + field :skills, + as: :tags, + disallowed: -> { record.skill_disallowed }, + suggestions: -> { record.skill_suggestions }, + html: -> do + edit do + wrapper do + classes do + unless record.has_skills + "hidden" + end end + # classes: "hidden" end - # classes: "hidden" end end - end + end end field :starting_at, diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index abecbbe4ae..1d89865524 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -170,6 +170,51 @@ Avo::Resources::Course.restore_items_from_backup end end + + describe "format_using (same as deprecated fetch_labels) with fetch_values_from" do + let!(:users) { create_list :user, 2, first_name: "Bob" } + let!(:courses) { create_list :course, 2, skills: users.pluck(:id) } + + it "fetches the labels" do + ENV["TESTING_TAGS_FORMAT_USING"] = "1" + + visit avo.resources_course_path(courses.first) + expect(page).to have_text "#{users[0].first_name} #{users[0].last_name}" + + visit avo.resources_course_path(courses.last) + expect(page).to have_text "#{users[1].first_name} #{users[1].last_name}" + + end + + it "keep correct tags on validations error and edit" do + visit avo.new_resources_course_path + + input_element = find('.tagify__input') + input_element.click + input_element.send_keys('Bob') + sleep 1 + input_element.send_keys(:enter) + sleep 1 + save + + expect(page).to have_text "Name can't be blank" + expect(page).to have_text "#{users[0].first_name} #{users[0].last_name}" + + fill_in "course_name", with: "The course" + + input_element = find('.tagify__input') + input_element.click + input_element.send_keys('Bob') + sleep 1 + input_element.send_keys(:enter) + sleep 1 + save + + expect(Course.last.skills.map(&:to_i)).to eql(users.pluck(:id)) + + ENV['TESTING_TAGS_FORMAT_USING'] = nil + end + end end def wait_for_tags_to_load(element, time = Capybara.default_max_wait_time) From 23540a9b78b9bc404905d768dd5b99fd51299384 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 14:47:53 +0300 Subject: [PATCH 08/19] lint --- spec/system/avo/tags_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index 1d89865524..f2967de123 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -212,7 +212,7 @@ expect(Course.last.skills.map(&:to_i)).to eql(users.pluck(:id)) - ENV['TESTING_TAGS_FORMAT_USING'] = nil + ENV["TESTING_TAGS_FORMAT_USING"] = nil end end end From c030fd899cc17b11ac5de264a671dc68d11c871b Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 14:48:54 +0300 Subject: [PATCH 09/19] lint --- spec/system/avo/tags_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index f2967de123..1cf0ab48ea 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -189,9 +189,9 @@ it "keep correct tags on validations error and edit" do visit avo.new_resources_course_path - input_element = find('.tagify__input') + input_element = find(".tagify__input") input_element.click - input_element.send_keys('Bob') + input_element.send_keys("Bob") sleep 1 input_element.send_keys(:enter) sleep 1 @@ -202,9 +202,9 @@ fill_in "course_name", with: "The course" - input_element = find('.tagify__input') + input_element = find(".tagify__input") input_element.click - input_element.send_keys('Bob') + input_element.send_keys("Bob") sleep 1 input_element.send_keys(:enter) sleep 1 From 9a174228c0e45495026f53b9f17afdd7e1358189 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 14:49:52 +0300 Subject: [PATCH 10/19] lint --- spec/dummy/app/avo/resources/course.rb | 20 ++++++++++---------- spec/system/avo/tags_spec.rb | 1 - 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/spec/dummy/app/avo/resources/course.rb b/spec/dummy/app/avo/resources/course.rb index 22fabaecf7..c2879decb3 100644 --- a/spec/dummy/app/avo/resources/course.rb +++ b/spec/dummy/app/avo/resources/course.rb @@ -53,16 +53,16 @@ def fields_bag if ENV["TESTING_TAGS_FORMAT_USING"] == "1" field :skills, - as: :tags, - fetch_values_from: "/admin/resources/users/get_users?hey=you&record_id=1", #{value: 1, label: "Jose"} - format_using: -> { - User.find(value).map do |user| - { - value: user.id, - label: user.name - } - end - } + as: :tags, + fetch_values_from: "/admin/resources/users/get_users?hey=you&record_id=1", #{value: 1, label: "Jose"} + format_using: -> { + User.find(value).map do |user| + { + value: user.id, + label: user.name + } + end + } else field :skills, as: :tags, diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index 1cf0ab48ea..95c6c7a388 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -183,7 +183,6 @@ visit avo.resources_course_path(courses.last) expect(page).to have_text "#{users[1].first_name} #{users[1].last_name}" - end it "keep correct tags on validations error and edit" do From 1cf3a64e2b8a3ea81bf6a3b309f72defc6d999b6 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 14:52:12 +0300 Subject: [PATCH 11/19] lint --- spec/dummy/app/avo/resources/course.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/dummy/app/avo/resources/course.rb b/spec/dummy/app/avo/resources/course.rb index c2879decb3..ecdd6d5c04 100644 --- a/spec/dummy/app/avo/resources/course.rb +++ b/spec/dummy/app/avo/resources/course.rb @@ -54,7 +54,7 @@ def fields_bag if ENV["TESTING_TAGS_FORMAT_USING"] == "1" field :skills, as: :tags, - fetch_values_from: "/admin/resources/users/get_users?hey=you&record_id=1", #{value: 1, label: "Jose"} + fetch_values_from: "/admin/resources/users/get_users?hey=you&record_id=1", # {value: 1, label: "Jose"} format_using: -> { User.find(value).map do |user| { From 7900808a6ab500c877f891cf3874048aaf70cc7b Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 16:03:29 +0300 Subject: [PATCH 12/19] fix test --- spec/system/avo/tags_spec.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index 95c6c7a388..ce2d683d2c 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -191,9 +191,10 @@ input_element = find(".tagify__input") input_element.click input_element.send_keys("Bob") - sleep 1 + wait_until { page.has_text?("#{users[0].first_name} #{users[0].last_name}") } input_element.send_keys(:enter) - sleep 1 + wait_until { page.has_css?('.tagify__tag-text', count: 1) } + sleep 0.5 save expect(page).to have_text "Name can't be blank" @@ -204,9 +205,10 @@ input_element = find(".tagify__input") input_element.click input_element.send_keys("Bob") - sleep 1 + wait_until { page.has_text?("#{users[1].first_name} #{users[1].last_name}") } input_element.send_keys(:enter) - sleep 1 + wait_until { page.has_css?('.tagify__tag-text', count: 2) } + sleep 0.5 save expect(Course.last.skills.map(&:to_i)).to eql(users.pluck(:id)) @@ -226,3 +228,9 @@ def wait_for_tags_to_load(element, time = Capybara.default_max_wait_time) def tags_element(parent) parent.find "tags.tagify" end + +def wait_until + Timeout.timeout(Capybara.default_max_wait_time) do + loop until yield + end +end From fecea206801f461e915647324347e0510435fe27 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 16:06:45 +0300 Subject: [PATCH 13/19] lint --- spec/system/avo/tags_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index ce2d683d2c..3830067bed 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -193,7 +193,7 @@ input_element.send_keys("Bob") wait_until { page.has_text?("#{users[0].first_name} #{users[0].last_name}") } input_element.send_keys(:enter) - wait_until { page.has_css?('.tagify__tag-text', count: 1) } + wait_until { page.has_css?(".tagify__tag-text", count: 1) } sleep 0.5 save @@ -207,7 +207,7 @@ input_element.send_keys("Bob") wait_until { page.has_text?("#{users[1].first_name} #{users[1].last_name}") } input_element.send_keys(:enter) - wait_until { page.has_css?('.tagify__tag-text', count: 2) } + wait_until { page.has_css?(".tagify__tag-text", count: 2) } sleep 0.5 save From fb6902b6f5a6c5d38a4c77adc1d5ea390c997012 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 16:31:59 +0300 Subject: [PATCH 14/19] try fix test --- spec/system/avo/tags_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index 3830067bed..4ed7beb683 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -174,6 +174,8 @@ describe "format_using (same as deprecated fetch_labels) with fetch_values_from" do let!(:users) { create_list :user, 2, first_name: "Bob" } let!(:courses) { create_list :course, 2, skills: users.pluck(:id) } + let(:tag_input) { tags_element(find_field_value_element("skills")) } + it "fetches the labels" do ENV["TESTING_TAGS_FORMAT_USING"] = "1" @@ -188,11 +190,10 @@ it "keep correct tags on validations error and edit" do visit avo.new_resources_course_path - input_element = find(".tagify__input") - input_element.click - input_element.send_keys("Bob") + tag_input.click + tag_input.send_keys("Bob") wait_until { page.has_text?("#{users[0].first_name} #{users[0].last_name}") } - input_element.send_keys(:enter) + tag_input.send_keys(:enter) wait_until { page.has_css?(".tagify__tag-text", count: 1) } sleep 0.5 save @@ -202,11 +203,10 @@ fill_in "course_name", with: "The course" - input_element = find(".tagify__input") - input_element.click - input_element.send_keys("Bob") + tag_input.click + tag_input.send_keys("Bob") wait_until { page.has_text?("#{users[1].first_name} #{users[1].last_name}") } - input_element.send_keys(:enter) + tag_input.send_keys(:enter) wait_until { page.has_css?(".tagify__tag-text", count: 2) } sleep 0.5 save From a3b5dce5b9627ff4d0cfb47a64472135d14cefb6 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 16:40:24 +0300 Subject: [PATCH 15/19] lint --- spec/system/avo/tags_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index 4ed7beb683..c3654d8cfb 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -176,7 +176,6 @@ let!(:courses) { create_list :course, 2, skills: users.pluck(:id) } let(:tag_input) { tags_element(find_field_value_element("skills")) } - it "fetches the labels" do ENV["TESTING_TAGS_FORMAT_USING"] = "1" From 0e13329d940b9c8bff94cf79e7d3e98a049b5733 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 16:46:48 +0300 Subject: [PATCH 16/19] fix test --- spec/system/avo/tags_spec.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index c3654d8cfb..48daa882fa 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -174,7 +174,8 @@ describe "format_using (same as deprecated fetch_labels) with fetch_values_from" do let!(:users) { create_list :user, 2, first_name: "Bob" } let!(:courses) { create_list :course, 2, skills: users.pluck(:id) } - let(:tag_input) { tags_element(find_field_value_element("skills")) } + let(:field_value_slot) { tags_element(find_field_value_element("skills")) } + let(:tags_input) { field_value_slot.find("span[contenteditable]") } it "fetches the labels" do ENV["TESTING_TAGS_FORMAT_USING"] = "1" @@ -189,12 +190,12 @@ it "keep correct tags on validations error and edit" do visit avo.new_resources_course_path - tag_input.click - tag_input.send_keys("Bob") - wait_until { page.has_text?("#{users[0].first_name} #{users[0].last_name}") } - tag_input.send_keys(:enter) - wait_until { page.has_css?(".tagify__tag-text", count: 1) } - sleep 0.5 + tags_input.click + tags_input.set("Bob") + wait_for_tags_to_load(field_value_slot) + type(:return) + + sleep 0.3 save expect(page).to have_text "Name can't be blank" @@ -202,12 +203,12 @@ fill_in "course_name", with: "The course" - tag_input.click - tag_input.send_keys("Bob") - wait_until { page.has_text?("#{users[1].first_name} #{users[1].last_name}") } - tag_input.send_keys(:enter) - wait_until { page.has_css?(".tagify__tag-text", count: 2) } - sleep 0.5 + tags_input.click + tags_input.set("Bob") + wait_for_tags_to_load(field_value_slot) + type(:return) + + sleep 0.3 save expect(Course.last.skills.map(&:to_i)).to eql(users.pluck(:id)) From 25ec966be9afe46b3bd432e6c9f3fac6036e3021 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 16:59:00 +0300 Subject: [PATCH 17/19] fix test --- spec/system/avo/tags_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index 48daa882fa..6c631e543c 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -193,7 +193,8 @@ tags_input.click tags_input.set("Bob") wait_for_tags_to_load(field_value_slot) - type(:return) + type(:down) + type(:down, :return) sleep 0.3 save @@ -206,7 +207,8 @@ tags_input.click tags_input.set("Bob") wait_for_tags_to_load(field_value_slot) - type(:return) + type(:down) + type(:down, :return) sleep 0.3 save From d010e017de695e9930e927da765a8bc7c1488edc Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 17:22:13 +0300 Subject: [PATCH 18/19] fix test --- spec/dummy/app/avo/resources/course.rb | 50 +++++++++++++------------- spec/system/avo/tags_spec.rb | 22 +++++++++--- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/spec/dummy/app/avo/resources/course.rb b/spec/dummy/app/avo/resources/course.rb index ecdd6d5c04..0ce6211998 100644 --- a/spec/dummy/app/avo/resources/course.rb +++ b/spec/dummy/app/avo/resources/course.rb @@ -51,36 +51,34 @@ def fields_bag end end - if ENV["TESTING_TAGS_FORMAT_USING"] == "1" - field :skills, - as: :tags, - fetch_values_from: "/admin/resources/users/get_users?hey=you&record_id=1", # {value: 1, label: "Jose"} - format_using: -> { - User.find(value).map do |user| - { - value: user.id, - label: user.name - } - end - } - else - field :skills, - as: :tags, - disallowed: -> { record.skill_disallowed }, - suggestions: -> { record.skill_suggestions }, - html: -> do - edit do - wrapper do - classes do - unless record.has_skills - "hidden" - end + # field :skills, + # as: :tags, + # fetch_values_from: "/admin/resources/users/get_users?hey=you&record_id=1", # {value: 1, label: "Jose"} + # format_using: -> { + # User.find(value).map do |user| + # { + # value: user.id, + # label: user.name + # } + # end + # } + + field :skills, + as: :tags, + disallowed: -> { record.skill_disallowed }, + suggestions: -> { record.skill_suggestions }, + html: -> do + edit do + wrapper do + classes do + unless record.has_skills + "hidden" end - # classes: "hidden" end + # classes: "hidden" end end - end + end end field :starting_at, diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index 6c631e543c..d0cca8b221 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -178,12 +178,24 @@ let(:tags_input) { field_value_slot.find("span[contenteditable]") } it "fetches the labels" do - ENV["TESTING_TAGS_FORMAT_USING"] = "1" + Avo::Resources::Course.with_temporary_items do + field :name + field :skills, + as: :tags, + fetch_values_from: "/admin/resources/users/get_users", # {value: 1, label: "Jose"} + format_using: -> { + User.find(value).map do |user| + { + value: user.id, + label: user.name + } + end + } + end - visit avo.resources_course_path(courses.first) - expect(page).to have_text "#{users[0].first_name} #{users[0].last_name}" + visit avo.resources_courses_path - visit avo.resources_course_path(courses.last) + expect(page).to have_text "#{users[0].first_name} #{users[0].last_name}" expect(page).to have_text "#{users[1].first_name} #{users[1].last_name}" end @@ -215,7 +227,7 @@ expect(Course.last.skills.map(&:to_i)).to eql(users.pluck(:id)) - ENV["TESTING_TAGS_FORMAT_USING"] = nil + Avo::Resources::Course.restore_items_from_backup end end end From 082a9b5a2ee88bf5d8cf26a34eb3a43ef7326f15 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 27 Jun 2024 17:31:20 +0300 Subject: [PATCH 19/19] lint --- spec/system/avo/tags_spec.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/spec/system/avo/tags_spec.rb b/spec/system/avo/tags_spec.rb index d0cca8b221..d18d5ec429 100644 --- a/spec/system/avo/tags_spec.rb +++ b/spec/system/avo/tags_spec.rb @@ -181,16 +181,16 @@ Avo::Resources::Course.with_temporary_items do field :name field :skills, - as: :tags, - fetch_values_from: "/admin/resources/users/get_users", # {value: 1, label: "Jose"} - format_using: -> { - User.find(value).map do |user| - { - value: user.id, - label: user.name - } - end - } + as: :tags, + fetch_values_from: "/admin/resources/users/get_users", # {value: 1, label: "Jose"} + format_using: -> { + User.find(value).map do |user| + { + value: user.id, + label: user.name + } + end + } end visit avo.resources_courses_path