2019-05-12 22:16:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
module ::AdPlugin
|
|
|
|
class HouseAdsController < ::ApplicationController
|
|
|
|
requires_plugin AdPlugin.plugin_name
|
|
|
|
|
|
|
|
def index
|
2024-04-09 13:54:11 -04:00
|
|
|
render_json_dump(
|
|
|
|
house_ads:
|
|
|
|
HouseAd.all.map do |ad|
|
|
|
|
ad.to_hash.merge!(categories: Category.secured(@guardian).where(id: ad.category_ids))
|
|
|
|
end,
|
|
|
|
settings: HouseAdSetting.all,
|
|
|
|
)
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
2019-05-07 14:32:32 -04:00
|
|
|
def show
|
2024-04-09 13:54:11 -04:00
|
|
|
house_ad_hash = HouseAd.find(params[:id])&.to_hash
|
|
|
|
if house_ad_hash
|
|
|
|
house_ad_hash.merge!(
|
|
|
|
categories: Category.secured(@guardian).where(id: house_ad_hash[:category_ids]),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
render_json_dump(house_ad: house_ad_hash)
|
2019-05-07 14:32:32 -04:00
|
|
|
end
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
def create
|
|
|
|
ad = HouseAd.create(house_ad_params)
|
2022-12-29 07:29:26 -05:00
|
|
|
ad.valid? ? render_json_dump(house_ad: ad.to_hash) : render_json_error(ad)
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if ad = HouseAd.find(house_ad_params[:id])
|
|
|
|
ad.update(house_ad_params)
|
|
|
|
else
|
|
|
|
ad = HouseAd.create(house_ad_params.except(:id))
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
ad.valid? ? render_json_dump(house_ad: ad.to_hash) : render_json_error(ad)
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if ad = HouseAd.find(house_ad_params[:id])
|
|
|
|
ad.destroy
|
|
|
|
else
|
2022-12-29 07:29:26 -05:00
|
|
|
render_json_error(I18n.t("not_found"), status: 404)
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def house_ad_params
|
2023-04-07 10:56:22 -04:00
|
|
|
@permitted ||=
|
|
|
|
begin
|
|
|
|
permitted =
|
2024-04-09 13:54:11 -04:00
|
|
|
params.permit(
|
|
|
|
:id,
|
|
|
|
:name,
|
|
|
|
:html,
|
|
|
|
:visible_to_anons,
|
|
|
|
:visible_to_logged_in_users,
|
|
|
|
group_ids: [],
|
|
|
|
category_ids: [],
|
|
|
|
)
|
2023-04-07 10:56:22 -04:00
|
|
|
permitted[:visible_to_logged_in_users] = ActiveModel::Type::Boolean.new.cast(
|
|
|
|
permitted[:visible_to_logged_in_users],
|
|
|
|
)
|
|
|
|
permitted[:visible_to_anons] = ActiveModel::Type::Boolean.new.cast(
|
|
|
|
permitted[:visible_to_anons],
|
|
|
|
)
|
|
|
|
permitted
|
|
|
|
end
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|