2019-05-12 22:16:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
module ::AdPlugin
|
|
|
|
class HouseAd
|
|
|
|
include ActiveModel::Validations
|
|
|
|
|
2023-04-07 10:56:22 -04:00
|
|
|
attr_accessor :id, :name, :html, :visible_to_logged_in_users, :visible_to_anons
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
NAME_REGEX = /\A[[:alnum:]\s\.,'!@#$%&\*\-\+\=:]*\z/i
|
|
|
|
|
|
|
|
validates :name, presence: true, format: { with: NAME_REGEX }
|
|
|
|
validates :html, presence: true
|
|
|
|
|
|
|
|
validate do
|
|
|
|
if self.class.all.any? { |ad| ad.id != self.id && ad.name.downcase == self.name.downcase }
|
|
|
|
errors.add(:name, :taken) # unique name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@name = "New Ad"
|
|
|
|
@html = "<div class='house-ad'>New Ad</div>"
|
2023-04-07 10:56:22 -04:00
|
|
|
@visible_to_logged_in_users = true
|
|
|
|
@visible_to_anons = true
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_hash(h)
|
|
|
|
ad = self.new
|
|
|
|
ad.name = h[:name]
|
|
|
|
ad.html = h[:html]
|
|
|
|
ad.id = h[:id].to_i if h[:id]
|
2023-04-07 10:56:22 -04:00
|
|
|
if h.key?(:visible_to_logged_in_users)
|
|
|
|
ad.visible_to_logged_in_users = h[:visible_to_logged_in_users]
|
|
|
|
end
|
|
|
|
ad.visible_to_anons = h[:visible_to_anons] if h.key?(:visible_to_anons)
|
2019-04-18 17:52:59 -04:00
|
|
|
ad
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create(attrs)
|
|
|
|
ad = from_hash(attrs)
|
|
|
|
ad.save
|
|
|
|
ad
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.alloc_id
|
2022-12-29 07:29:26 -05:00
|
|
|
DistributedMutex.synchronize("adplugin-house-ad-id") do
|
2019-04-18 17:52:59 -04:00
|
|
|
max_id = AdPlugin.pstore_get("ad:_id")
|
|
|
|
max_id = 1 unless max_id
|
|
|
|
AdPlugin.pstore_set("ad:_id", max_id + 1)
|
|
|
|
max_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find(id)
|
2022-12-29 07:29:26 -05:00
|
|
|
if r = AdPlugin.pstore_get("ad:#{id}")
|
2019-04-18 17:52:59 -04:00
|
|
|
from_hash(r)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all
|
2022-12-29 07:29:26 -05:00
|
|
|
PluginStoreRow
|
|
|
|
.where(plugin_name: AdPlugin.plugin_name)
|
2019-04-18 17:52:59 -04:00
|
|
|
.where("key LIKE 'ad:%'")
|
|
|
|
.where("key != 'ad:_id'")
|
2022-12-29 07:29:26 -05:00
|
|
|
.map { |psr| from_hash(PluginStore.cast_value(psr.type_name, psr.value)) }
|
|
|
|
.sort_by { |ad| ad.id }
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
2023-04-07 10:56:22 -04:00
|
|
|
def self.all_for_anons
|
|
|
|
self.all.select(&:visible_to_anons)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all_for_logged_in_users
|
|
|
|
self.all.select(&:visible_to_logged_in_users)
|
|
|
|
end
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
def save
|
|
|
|
if self.valid?
|
|
|
|
self.id = self.class.alloc_id if self.id.to_i <= 0
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin.pstore_set("ad:#{id}", to_hash)
|
2023-04-07 10:56:22 -04:00
|
|
|
Site.clear_anon_cache!
|
2019-04-18 17:52:59 -04:00
|
|
|
self.class.publish_if_ads_enabled
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(attrs)
|
|
|
|
self.name = attrs[:name]
|
|
|
|
self.html = attrs[:html]
|
2023-04-07 10:56:22 -04:00
|
|
|
if attrs.key?(:visible_to_logged_in_users)
|
|
|
|
self.visible_to_logged_in_users = attrs[:visible_to_logged_in_users]
|
|
|
|
end
|
|
|
|
self.visible_to_anons = attrs[:visible_to_anons] if attrs.key?(:visible_to_anons)
|
2019-04-18 17:52:59 -04:00
|
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_hash
|
2023-04-07 10:56:22 -04:00
|
|
|
{
|
|
|
|
id: @id,
|
|
|
|
name: @name,
|
|
|
|
html: @html,
|
|
|
|
visible_to_logged_in_users: @visible_to_logged_in_users,
|
|
|
|
visible_to_anons: @visible_to_anons,
|
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin.pstore_delete("ad:#{id}")
|
2023-04-07 10:56:22 -04:00
|
|
|
Site.clear_anon_cache!
|
2019-04-18 17:52:59 -04:00
|
|
|
self.class.publish_if_ads_enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.publish_if_ads_enabled
|
|
|
|
if AdPlugin::HouseAdSetting.all.any? { |_, adsToShow| !adsToShow.blank? }
|
|
|
|
AdPlugin::HouseAdSetting.publish_settings
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|