Skip to content

A platform for community discussion. Free, open, simple.

FIX: Handle house ad category selection (#44992)

Samfaab522135f5
diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ad-form.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ad-form.gjs
index 26c635e0314..6a0a227965f 100644
--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ad-form.gjs
+++ b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ad-form.gjs
@@ -6,9 +6,9 @@ import Form from "discourse/components/form";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { removeValueFromArray } from "discourse/lib/array-tools";
+import CategorySelector from "discourse/select-kit/components/category-selector";
import GroupChooser from "discourse/select-kit/components/group-chooser";
import { i18n } from "discourse-i18n";
-import HouseAdsCategorySelector from "./house-ads-category-selector.gjs";
import HouseAdsRouteSelector from "./house-ads-route-selector.gjs";
import Preview from "./modal/preview.gjs";
@@ -218,10 +218,9 @@ export default class HouseAdForm extends Component {
as |field|
>
<field.Control>
- <HouseAdsCategorySelector
- @categories={{this.site.categories}}
+ <CategorySelector
+ @categories={{field.value}}
@onChange={{field.set}}
- @selectedCategories={{field.value}}
/>
</field.Control>
</form.Field>
diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-category-selector.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-category-selector.gjs
deleted file mode 100644
index fe5b0b0c9b1..00000000000
--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-category-selector.gjs
+++ /dev/null
@@ -1,7 +0,0 @@
-import CategorySelector from "discourse/select-kit/components/category-selector";
-
-export default class HouseAdsCategorySelector extends CategorySelector {
- get value() {
- return this.selectedCategories.map((c) => c.id);
- }
-}
diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/show.js b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/show.js
index d5f85848c36..e2984652be4 100644
--- a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/show.js
+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/show.js
@@ -1,8 +1,9 @@
import { trackedObject } from "@ember/reactive/collections";
+import Category from "discourse/models/category";
import DiscourseRoute from "discourse/routes/discourse";
export default class AdminPluginsShowHouseAdsShow extends DiscourseRoute {
- model(params) {
+ async model(params) {
const parentModel = this.modelFor("adminPlugins.show.houseAds");
if (params.id === "new") {
@@ -21,6 +22,10 @@ export default class AdminPluginsShowHouseAdsShow extends DiscourseRoute {
return this.replaceWith("adminPlugins.show.houseAds.index");
}
+ houseAd.categories = await Category.asyncFindByIds(
+ (houseAd.categories || []).map((category) => category.id)
+ );
+
if (houseAd.groups && Array.isArray(houseAd.groups)) {
houseAd.groups = houseAd.groups.map((g) =>
typeof g === "object" && g !== null ? g.id : g
diff --git a/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rb b/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rb
index 6de876962ab..8a515df2330 100644
--- a/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rb
+++ b/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rb
@@ -31,7 +31,7 @@ module AdPlugin
end
def create
- ad = HouseAd.new(house_ad_params)
+ ad = HouseAd.new(house_ad_params.except(:routes))
if ad.save
sync_routes(ad, house_ad_params[:routes])
render_json_dump(
diff --git a/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rb b/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rb
index 145802bca3d..d5687ef6e09 100644
--- a/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rb
+++ b/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rb
@@ -23,6 +23,24 @@ describe AdPlugin::HouseAdsController do
context "when used by admins" do
before { sign_in(admin) }
+ it "creates an ad targeted to a route" do
+ post "/admin/plugins/pluginad/house_creatives.json",
+ params: {
+ name: "Latest ad",
+ html: "<p>Latest discussions</p>",
+ visible_to_anons: "true",
+ visible_to_logged_in_users: "true",
+ routes: ["discovery.latest"],
+ }
+
+ expect(response.status).to eq(200)
+ house_ad_response = response.parsed_body["house_ad"]
+ expect(house_ad_response["routes"]).to eq(["discovery.latest"])
+ expect(AdPlugin::HouseAd.find(house_ad_response["id"]).route_names).to eq(
+ ["discovery.latest"],
+ )
+ end
+
it "strips script tags from html on create" do
post "/admin/plugins/pluginad/house_creatives.json",
params: {
diff --git a/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rb b/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rb
index a19cd5cc832..1e4debf786c 100644
--- a/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rb
+++ b/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rb
@@ -23,6 +23,35 @@ describe "Admin House Ad" do
expect(page).to have_no_css(".alert-error")
end
+ [false, true].each do |lazy_load_categories|
+ it "lets an admin search for a category and save it with lazy loading #{lazy_load_categories ? "enabled" : "disabled"}" do
+ SiteSetting.lazy_load_categories_groups =
+ lazy_load_categories ? Group::AUTO_GROUPS[:admins].to_s : ""
+ category = Fabricate(:category, name: "OpenPanel")
+ another_category = Fabricate(:category, name: "Security summary")
+ Fabricate(:category_sidebar_section_link, user: admin, linkable: category)
+ house_ads_page.visit_new
+
+ form.field("name").fill_in("Category ad")
+ form.field("html").fill_in("<p>Category ad</p>")
+ form.field("categories").select(category.name)
+ form.submit
+
+ expect(toasts).to have_success(I18n.t("js.saved"))
+ page.refresh
+ expect(form.field("categories")).to have_value(category.id.to_s)
+ expect(house_ads_page).to have_selected_category(category)
+
+ form.field("categories").select(another_category.name)
+ form.submit
+
+ expect(toasts).to have_success(I18n.t("js.saved"))
+ page.refresh
+ expect(house_ads_page).to have_selected_category(category)
+ expect(house_ads_page).to have_selected_category(another_category)
+ end
+ end
+
it "supports the full house ad lifecycle" do
house_ads_page.visit_page
expect(house_ads_page).to have_empty_state
diff --git a/plugins/discourse-adplugin/spec/system/page_objects/pages/admin_house_ads.rb b/plugins/discourse-adplugin/spec/system/page_objects/pages/admin_house_ads.rb
index 2b495805704..bd14b066350 100644
--- a/plugins/discourse-adplugin/spec/system/page_objects/pages/admin_house_ads.rb
+++ b/plugins/discourse-adplugin/spec/system/page_objects/pages/admin_house_ads.rb
@@ -36,6 +36,10 @@ module PageObjects
has_no_css?(".house-ads-table tr.d-table__row", text: name)
end
+ def has_selected_category?(category)
+ has_css?(".house-ad-form .category-selector .formatted-selection", text: category.name)
+ end
+
def click_delete
find(".house-ad-form .btn-danger").click
self