docbrown/app/controllers/display_ad_events_controller.rb
Jeremy Friesen b56ae3acbb
Extracting constants and tidying tests (#15946)
Prior to this commit, we had a couple of references to magic strings
regarding DisplayAdEvent objects.  This commit seeks to consolidate that
behavior.

In a handful of cases we had javascript variables, but as these JS files
are `erb` templates, we can inject the constant as well.  This reduces
repetition of knowledge.
2022-01-05 09:19:39 -05:00

36 lines
1.1 KiB
Ruby

class DisplayAdEventsController < ApplicationMetalController
include ActionController::Head
# No policy needed. All views are for all users
def create
# Only tracking for logged in users at the moment
display_ad_event_create_params = display_ad_event_params.merge(user_id: session_current_user_id)
@display_ad_event = DisplayAdEvent.create(display_ad_event_create_params)
update_display_ads_data
head :ok
end
private
def update_display_ads_data
ThrottledCall.perform(:display_ads_data_update, throttle_for: 15.minutes) do
@display_ad = DisplayAd.find(display_ad_event_params[:display_ad_id])
num_impressions = @display_ad.display_ad_events.impressions.size
num_clicks = @display_ad.display_ad_events.clicks.size
rate = num_clicks.to_f / num_impressions
@display_ad.update_columns(
success_rate: rate,
clicks_count: num_clicks,
impressions_count: num_impressions,
)
end
end
def display_ad_event_params
params[:display_ad_event].slice(:context_type, :category, :display_ad_id)
end
end