From 2d6b740940be87725ce70edf7ff2154f38bef8d5 Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Mon, 28 Aug 2023 15:09:36 +0300 Subject: [PATCH] Renamed DisplayAd model to Billboard (#19993) * Renamed Resource Admin: DisplayAd to Billboard * Renamed DisplayAd to Billboard * Added a data update script for roles --- .../admin/billboards_controller.rb | 14 +++++----- .../api/v1/audience_segments_controller.rb | 2 +- .../api/v1/billboards_controller.rb | 12 ++++----- .../billboard_events_controller.rb | 2 +- app/controllers/billboards_controller.rb | 2 +- app/controllers/stories_controller.rb | 2 +- app/helpers/billboard_helper.rb | 6 ++--- app/lib/constants/role.rb | 2 +- app/models/{display_ad.rb => billboard.rb} | 6 +++-- app/models/billboard_event.rb | 2 +- app/models/organization.rb | 2 +- app/models/tag.rb | 2 +- app/queries/billboards/filtered_ads_query.rb | 8 +++--- app/services/content_renderer.rb | 6 ++--- app/views/sidebars/_homepage_content.html.erb | 2 +- app/views/stories/_main_stories_feed.html.erb | 6 ++--- .../audience_segment_refresh_all_worker.rb | 2 +- config/environments/development.rb | 2 +- config/locales/views/admin/en.yml | 2 +- config/locales/views/admin/fr.yml | 2 +- db/seeds.rb | 10 +++---- ...0220830153942_generate_display_ad_names.rb | 4 +-- ...8_rename_display_ad_roles_to_billboards.rb | 7 +++++ spec/factories/billboards.rb | 2 +- ...ame_display_ad_roles_to_billboards_spec.rb | 14 ++++++++++ .../{display_ad_spec.rb => billboard_spec.rb} | 12 ++++----- spec/queries/admin/users_query_spec.rb | 6 ++--- .../billboards/filtered_ads_query_spec.rb | 2 +- spec/requests/admin/billboards_spec.rb | 26 +++++++++---------- spec/requests/api/v1/billboards_spec.rb | 2 +- spec/requests/api/v1/docs/billboards_spec.rb | 10 +++---- spec/support/seeds/seeds_e2e.rb | 10 +++---- spec/swagger_helper.rb | 8 +++--- swagger/v1/api_v1.json | 12 ++++----- 34 files changed, 116 insertions(+), 93 deletions(-) rename app/models/{display_ad.rb => billboard.rb} (98%) create mode 100644 lib/data_update_scripts/20230828081018_rename_display_ad_roles_to_billboards.rb create mode 100644 spec/lib/data_update_scripts/rename_display_ad_roles_to_billboards_spec.rb rename spec/models/{display_ad_spec.rb => billboard_spec.rb} (98%) diff --git a/app/controllers/admin/billboards_controller.rb b/app/controllers/admin/billboards_controller.rb index 19d5baf40..609b16429 100644 --- a/app/controllers/admin/billboards_controller.rb +++ b/app/controllers/admin/billboards_controller.rb @@ -5,7 +5,7 @@ module Admin after_action :bust_ad_caches, only: %i[create update destroy] def index - @billboards = DisplayAd.order(id: :desc) + @billboards = Billboard.order(id: :desc) .page(params[:page]).per(50) return if params[:search].blank? @@ -14,15 +14,15 @@ module Admin end def new - @billboard = DisplayAd.new + @billboard = Billboard.new end def edit - @billboard = DisplayAd.find(params[:id]) + @billboard = Billboard.find(params[:id]) end def create - @billboard = DisplayAd.new(billboard_params) + @billboard = Billboard.new(billboard_params) @billboard.creator = current_user if @billboard.save @@ -35,7 +35,7 @@ module Admin end def update - @billboard = DisplayAd.find(params[:id]) + @billboard = Billboard.find(params[:id]) if @billboard.update(billboard_params) flash[:success] = I18n.t("admin.billboards_controller.updated") @@ -47,7 +47,7 @@ module Admin end def destroy - @billboard = DisplayAd.find(params[:id]) + @billboard = Billboard.find(params[:id]) if @billboard.destroy render json: { message: I18n.t("admin.billboards_controller.deleted") }, status: :ok @@ -65,7 +65,7 @@ module Admin end def authorize_admin - authorize DisplayAd, :access?, policy_class: InternalPolicy + authorize Billboard, :access?, policy_class: InternalPolicy end def bust_ad_caches diff --git a/app/controllers/api/v1/audience_segments_controller.rb b/app/controllers/api/v1/audience_segments_controller.rb index d2eddf5a7..fd3773ea1 100644 --- a/app/controllers/api/v1/audience_segments_controller.rb +++ b/app/controllers/api/v1/audience_segments_controller.rb @@ -34,7 +34,7 @@ module Api def destroy @segment = scope.find(params[:id]) - if DisplayAd.where(audience_segment_id: @segment.id).any? + if Billboard.where(audience_segment_id: @segment.id).any? render json: { error: "Segments cannot be deleted while in use by any billboards" }, status: :conflict else @segment.segmented_users.in_batches.delete_all diff --git a/app/controllers/api/v1/billboards_controller.rb b/app/controllers/api/v1/billboards_controller.rb index b9d1f4c02..8f741b1a2 100644 --- a/app/controllers/api/v1/billboards_controller.rb +++ b/app/controllers/api/v1/billboards_controller.rb @@ -8,30 +8,30 @@ module Api rescue_from ArgumentError, with: :error_unprocessable_entity def index - @billboards = DisplayAd.order(id: :desc).page(params[:page]).per(50) + @billboards = Billboard.order(id: :desc).page(params[:page]).per(50) @billboards = @billboards.search_ads(params[:search]) if params[:search].present? render json: @billboards end def show - @billboard = DisplayAd.find(params[:id]) + @billboard = Billboard.find(params[:id]) render json: @billboard end def create - @billboard = DisplayAd.new(permitted_params) + @billboard = Billboard.new(permitted_params) @billboard.save! render json: @billboard, status: :created end def update - @billboard = DisplayAd.find(params[:id]) + @billboard = Billboard.find(params[:id]) @billboard.update!(permitted_params) render json: @billboard end def unpublish - @billboard = DisplayAd.find(params[:id]) + @billboard = Billboard.find(params[:id]) result = @billboard.update(published: false) if result head :no_content @@ -43,7 +43,7 @@ module Api private def require_admin - authorize DisplayAd, :access?, policy_class: InternalPolicy + authorize Billboard, :access?, policy_class: InternalPolicy end def permitted_params diff --git a/app/controllers/billboard_events_controller.rb b/app/controllers/billboard_events_controller.rb index 909f5b9af..7405e5ca6 100644 --- a/app/controllers/billboard_events_controller.rb +++ b/app/controllers/billboard_events_controller.rb @@ -18,7 +18,7 @@ class BillboardEventsController < ApplicationMetalController billboard_event_id = billboard_event_params[:billboard_id] ThrottledCall.perform("billboards_data_update-#{billboard_event_id}", throttle_for: 15.minutes) do - @billboard = DisplayAd.find(billboard_event_id) + @billboard = Billboard.find(billboard_event_id) num_impressions = @billboard.billboard_events.impressions.sum(:counts_for) num_clicks = @billboard.billboard_events.clicks.sum(:counts_for) diff --git a/app/controllers/billboards_controller.rb b/app/controllers/billboards_controller.rb index 002e6a23d..7bf030213 100644 --- a/app/controllers/billboards_controller.rb +++ b/app/controllers/billboards_controller.rb @@ -19,7 +19,7 @@ class BillboardsController < ApplicationController @article = Article.find_by(slug: params[:slug]) end - @billboard = DisplayAd.for_display( + @billboard = Billboard.for_display( area: placement_area, user_signed_in: user_signed_in?, user_id: current_user&.id, diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index ac3bf926c..57699104f 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -54,7 +54,7 @@ class StoriesController < ApplicationController end def assign_hero_banner - @hero_billboard = DisplayAd.for_display(area: "home_hero", user_signed_in: user_signed_in?) + @hero_billboard = Billboard.for_display(area: "home_hero", user_signed_in: user_signed_in?) end def assign_hero_html diff --git a/app/helpers/billboard_helper.rb b/app/helpers/billboard_helper.rb index 9560808ec..3ee631a7a 100644 --- a/app/helpers/billboard_helper.rb +++ b/app/helpers/billboard_helper.rb @@ -1,6 +1,6 @@ module BillboardHelper def billboards_placement_area_options_array - DisplayAd::ALLOWED_PLACEMENT_AREAS_HUMAN_READABLE.zip(DisplayAd::ALLOWED_PLACEMENT_AREAS) + Billboard::ALLOWED_PLACEMENT_AREAS_HUMAN_READABLE.zip(Billboard::ALLOWED_PLACEMENT_AREAS) end def automatic_audience_segments_options_array @@ -20,9 +20,9 @@ module BillboardHelper # # @return [Boolean] true or false on whether the area is a targeted tag placement on the feed. # - # @note An area of "sidebar_left_2" will return false as it is not part of DisplayAd::HOME_FEED_PLACEMENTS + # @note An area of "sidebar_left_2" will return false as it is not part of Billboard::HOME_FEED_PLACEMENTS # whilst an area of "feed_first" will return false. def feed_targeted_tag_placement?(area) - DisplayAd::HOME_FEED_PLACEMENTS.include?(area) + Billboard::HOME_FEED_PLACEMENTS.include?(area) end end diff --git a/app/lib/constants/role.rb b/app/lib/constants/role.rb index 6dab2841b..b96a7bd1d 100644 --- a/app/lib/constants/role.rb +++ b/app/lib/constants/role.rb @@ -22,7 +22,7 @@ module Constants "Resource Admin: Broadcast" => { name: "single_resource_admin", resource_type: "Broadcast" }, "Resource Admin: Comment" => { name: "single_resource_admin", resource_type: "Comment" }, "Resource Admin: Config" => { name: "single_resource_admin", resource_type: "Config" }, - "Resource Admin: DisplayAd" => { name: "single_resource_admin", resource_type: "DisplayAd" }, + "Resource Admin: Billboard" => { name: "single_resource_admin", resource_type: "Billboard" }, "Resource Admin: DataUpdateScript" => { name: "single_resource_admin", resource_type: "DataUpdateScript" }, "Resource Admin: FeedbackMessage" => { name: "single_resource_admin", resource_type: "FeedbackMessage" }, "Resource Admin: HtmlVariant" => { name: "single_resource_admin", resource_type: "HtmlVariant" }, diff --git a/app/models/display_ad.rb b/app/models/billboard.rb similarity index 98% rename from app/models/display_ad.rb rename to app/models/billboard.rb index 5e9e4a3b2..2013e7d15 100644 --- a/app/models/display_ad.rb +++ b/app/models/billboard.rb @@ -1,4 +1,4 @@ -class DisplayAd < ApplicationRecord +class Billboard < ApplicationRecord include Taggable acts_as_taggable_on :tags resourcify @@ -32,7 +32,7 @@ class DisplayAd < ApplicationRecord enum type_of: { in_house: 0, community: 1, external: 2 } belongs_to :organization, optional: true - has_many :billboard_events, dependent: :destroy + has_many :billboard_events, foreign_key: :display_ad_id, inverse_of: :billboard, dependent: :destroy validates :placement_area, presence: true, inclusion: { in: ALLOWED_PLACEMENT_AREAS } @@ -60,6 +60,8 @@ class DisplayAd < ApplicationRecord scope :seldom_seen, ->(area) { where("impressions_count < ?", low_impression_count(area)).or(where(priority: true)) } + self.table_name = "display_ads" + def self.for_display(area:, user_signed_in:, user_id: nil, article: nil, user_tags: nil, location: nil) permit_adjacent = article ? article.permit_adjacent_sponsors? : true diff --git a/app/models/billboard_event.rb b/app/models/billboard_event.rb index 51c94a23d..445ed2859 100644 --- a/app/models/billboard_event.rb +++ b/app/models/billboard_event.rb @@ -2,7 +2,7 @@ # :delete for the relationship. That means no before/after # destroy callbacks will be called on this object. class BillboardEvent < ApplicationRecord - belongs_to :billboard, class_name: "DisplayAd", foreign_key: :display_ad_id, inverse_of: :billboard_events + belongs_to :billboard, class_name: "Billboard", foreign_key: :display_ad_id, inverse_of: :billboard_events belongs_to :user, optional: true self.table_name = "display_ad_events" diff --git a/app/models/organization.rb b/app/models/organization.rb index 237b56d96..b8aa58327 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -28,7 +28,7 @@ class Organization < ApplicationRecord has_many :articles, dependent: :nullify has_many :collections, dependent: :nullify has_many :credits, dependent: :restrict_with_error - has_many :billboards, class_name: "DisplayAd", dependent: :destroy + has_many :billboards, class_name: "Billboard", dependent: :destroy has_many :listings, dependent: :destroy has_many :notifications, dependent: :delete_all has_many :organization_memberships, dependent: :delete_all diff --git a/app/models/tag.rb b/app/models/tag.rb index 6a11b294f..4c9e476af 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -38,7 +38,7 @@ class Tag < ActsAsTaggableOn::Tag belongs_to :badge, optional: true has_many :articles, through: :taggings, source: :taggable, source_type: "Article" - has_many :billboards, class_name: "DisplayAd", through: :taggings, source: :taggable, source_type: "DisplayAd" + has_many :billboards, class_name: "Billboard", through: :taggings, source: :taggable, source_type: "Billboard" mount_uploader :profile_image, ProfileImageUploader mount_uploader :social_image, ProfileImageUploader diff --git a/app/queries/billboards/filtered_ads_query.rb b/app/queries/billboards/filtered_ads_query.rb index f422a3a20..dc9b3d6d3 100644 --- a/app/queries/billboards/filtered_ads_query.rb +++ b/app/queries/billboards/filtered_ads_query.rb @@ -7,10 +7,10 @@ module Billboards # @param area [String] the site area where the ad is visible # @param user_signed_in [Boolean] whether or not the visitor is signed-in - # @param billboards [DisplayAd] can be a filtered scope or Arel relationship + # @param billboards [Billboard] can be a filtered scope or Arel relationship # @param location [Geolocation|String] the visitor's geographic location def initialize(area:, user_signed_in:, organization_id: nil, article_tags: [], - permit_adjacent_sponsors: true, article_id: nil, billboards: DisplayAd, + permit_adjacent_sponsors: true, article_id: nil, billboards: Billboard, user_id: nil, user_tags: nil, location: nil) @filtered_billboards = billboards.includes([:organization]) @area = area @@ -118,7 +118,7 @@ module Billboards def type_of_ads # If this is an organization article and community-type ads exist, show them if @organization_id.present? - community = @filtered_billboards.where(type_of: DisplayAd.type_ofs[:community], + community = @filtered_billboards.where(type_of: Billboard.type_ofs[:community], organization_id: @organization_id) return community if community.any? end @@ -135,7 +135,7 @@ module Billboards types_matching << :external end - @filtered_billboards.where(type_of: DisplayAd.type_ofs.slice(*types_matching).values) + @filtered_billboards.where(type_of: Billboard.type_ofs.slice(*types_matching).values) end end end diff --git a/app/services/content_renderer.rb b/app/services/content_renderer.rb index b8d34148f..6efcedba0 100644 --- a/app/services/content_renderer.rb +++ b/app/services/content_renderer.rb @@ -1,4 +1,4 @@ -# renders markdown for Articles, DisplayAds, Comments +# renders markdown for Articles, Billboards, Comments class ContentRenderer Result = Struct.new(:front_matter, :reading_time, :processed_html, keyword_init: true) @@ -9,8 +9,8 @@ class ContentRenderer end # @param input [String] body_markdown to process - # @param source [Article, Comment, DisplayAd] - # @param user [User, NilClass] article's or comment's user, nil for DisplayAd + # @param source [Article, Comment, Billboard] + # @param user [User, NilClass] article's or comment's user, nil for Billboard # @param fixer [Object] fixes the input markdown def initialize(input, source:, user: nil, fixer: MarkdownProcessor::Fixer::FixAll) @input = input || "" diff --git a/app/views/sidebars/_homepage_content.html.erb b/app/views/sidebars/_homepage_content.html.erb index 129d14c4d..c898db879 100644 --- a/app/views/sidebars/_homepage_content.html.erb +++ b/app/views/sidebars/_homepage_content.html.erb @@ -1,6 +1,6 @@