* feat: add a migration that creates a display_to column on the display_ads table * feat: add an enum for display_to * feat/WIP: add rough draft of the field to the UI and permit it in the controller * feat: take into account on whether an ad needs to be shown on logged in or out or both + update cache keys on sign in * fix: if a user is not signed in then we want to display all and logged out * fix: display_for spec in model * feat: add some tests for display_to * feat: style the label * feat: show the display_to on the index page * feat: add a seed for e2e * chore: force true
74 lines
1.9 KiB
Ruby
74 lines
1.9 KiB
Ruby
module Admin
|
|
class DisplayAdsController < Admin::ApplicationController
|
|
layout "admin"
|
|
|
|
after_action :bust_ad_caches, only: %i[create update destroy]
|
|
|
|
def index
|
|
@display_ads = DisplayAd.order(id: :desc)
|
|
.page(params[:page]).per(50)
|
|
|
|
return if params[:search].blank?
|
|
|
|
@display_ads = @display_ads
|
|
.where("processed_html ILIKE :search OR placement_area ILIKE :search OR organizations.name ILIKE :search",
|
|
search: "%#{params[:search]}%")
|
|
end
|
|
|
|
def new
|
|
@display_ad = DisplayAd.new
|
|
end
|
|
|
|
def edit
|
|
@display_ad = DisplayAd.find(params[:id])
|
|
end
|
|
|
|
def create
|
|
@display_ad = DisplayAd.new(display_ad_params)
|
|
|
|
if @display_ad.save
|
|
flash[:success] = I18n.t("admin.display_ads_controller.created")
|
|
redirect_to edit_admin_display_ad_path(@display_ad.id)
|
|
else
|
|
flash[:danger] = @display_ad.errors_as_sentence
|
|
render :new
|
|
end
|
|
end
|
|
|
|
def update
|
|
@display_ad = DisplayAd.find(params[:id])
|
|
|
|
if @display_ad.update(display_ad_params)
|
|
flash[:success] = I18n.t("admin.display_ads_controller.updated")
|
|
redirect_to edit_admin_display_ad_path(params[:id])
|
|
else
|
|
flash[:danger] = @display_ad.errors_as_sentence
|
|
render :edit
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@display_ad = DisplayAd.find(params[:id])
|
|
|
|
if @display_ad.destroy
|
|
render json: { message: I18n.t("admin.display_ads_controller.deleted") }, status: :ok
|
|
else
|
|
render json: { error: I18n.t("admin.display_ads_controller.wrong") }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def display_ad_params
|
|
params.permit(:organization_id, :body_markdown, :placement_area, :published, :approved, :name, :display_to)
|
|
end
|
|
|
|
def authorize_admin
|
|
authorize DisplayAd, :access?, policy_class: InternalPolicy
|
|
end
|
|
|
|
def bust_ad_caches
|
|
EdgeCache::BustSidebar.call
|
|
end
|
|
end
|
|
end
|