Skip to content

Instantly share code, notes, and snippets.

@changs
Created May 30, 2014 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save changs/ae79e90f042446ed8a1d to your computer and use it in GitHub Desktop.
Save changs/ae79e90f042446ed8a1d to your computer and use it in GitHub Desktop.
diff --git a/app/models/setting/ads.rb b/app/models/setting/ads.rb
index f9361c6..5cf650b 100644
--- a/app/models/setting/ads.rb
+++ b/app/models/setting/ads.rb
@@ -1,7 +1,7 @@
class Setting::Ads
include Mongoid::Document
- CACHE_KEY = "ads_config"
+ CACHE_KEY = "ads-config"
# == fields
field :bucket, type: String
@@ -95,69 +95,48 @@ class Setting::Ads
def self.generate_config
config = {}
- Setting::Ads.asc(:app, :sequence).all.each do |ad|
+ Setting::Ads.asc(:app).all.each do |ad|
bucket, app = ad.bucket, ad.app
config["#{bucket}_#{app}"] ||= {}
ad_hash = { provider: ad.provider, tags: ad.tags, size: ad.size }
-
- if ad.places.count == 1 && ad.places.first == "*" ## all
+ if (ad.places.count == 1 && ad.places.first == "*") || ad.places.count > 1 || ad.units.count > 1
slots = ad.places.product(ad.units).map {|p| p.join(".")}
slots.each do |slot|
- config["#{bucket}_#{app}"][slot] = ad_hash
- end
- elsif ad.places.count > 1 || ad.units.count > 1 ## multiple
- config["#{bucket}_#{app}"]["multiple"] ||= {}
- slots = ad.places.product(ad.units).map {|p| p.join(".")}
- config["#{bucket}_#{app}"]["multiple"][slots] = ad_hash
- elsif ad.places.count == 1 && ad.units.count == 1 ## one specific place
- slot = "#{ad.places.first}.#{ad.units.first}"
-
- if ad.units.first == "interstitial" ## special case for interstitial ads
config["#{bucket}_#{app}"][slot] ||= []
config["#{bucket}_#{app}"][slot] << ad_hash
- else
- config["#{bucket}_#{app}"][slot] = ad_hash
end
+ elsif ad.places.count == 1 && ad.units.count == 1 ## one specific place
+ slot = "#{ad.places.first}.#{ad.units.first}"
+ config["#{bucket}_#{app}"][slot] ||= []
+ config["#{bucket}_#{app}"][slot] << ad_hash
end
end
-
config
end
def self.find_ad(bucket, app, slot)
place, unit = slot.split(".")
- configs = []
config_keys = ["#{bucket}_#{app}", "#{bucket.first}_#{app}", "-_#{app}"]
- config_keys.each do |key|
- configs << config.select{|k, v| k == key}
- end
- configs.reject!(&:blank?)
+ configs = config_keys.map { |key| config.select{|k, v| k == key } }.reject(&:blank?)
return :no_config if configs.empty?
- found_confs = Array.new
+ found_confs = Hash.new
+
configs.each do |conf_hash|
conf_hash.each do |key, conf|
- found = (
- conf[slot].presence ||
- (conf["multiple"] && conf["multiple"].find {|k, v| k.include?(slot)}.try(:last).presence) ||
- conf["*.#{unit}"]
- )
- if found
- found_confs << found
- unless found.kind_of? Array
- found_confs.last['placement'] = conf.keys.reject { |key| key == 'multiple' }
- found_confs.last['app'] = key
- end
- end
+ found_confs[slot] ||= []
+ found_confs['asterisk'] ||= []
+ found_confs[slot] << conf[slot].presence if conf[slot].presence
+ found_confs['asterisk'] << conf["*.#{unit}"] if conf["*.#{unit}"]
end
end
- if found_confs.first.kind_of? Array
- found_confs.flatten
+ if unit == 'interstitial'
+ (found_confs[slot] || found_confs['asterisk'] || [:no_config]).flatten
else
- found_confs.select { |x| x['placement'].include?(slot) }.sample || found_confs.first || :no_config
+ (found_confs[slot].first || []).sample || (found_confs['asterisk'].first || []).sample || :no_config
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment