diff --git a/app/controllers/admin/display_ads_controller.rb b/app/controllers/admin/display_ads_controller.rb index 801986863..c8258b026 100644 --- a/app/controllers/admin/display_ads_controller.rb +++ b/app/controllers/admin/display_ads_controller.rb @@ -60,7 +60,7 @@ module Admin private def display_ad_params - params.permit(:organization_id, :body_markdown, :placement_area, :published, :approved, :name) + params.permit(:organization_id, :body_markdown, :placement_area, :published, :approved, :name, :display_to) end def authorize_admin diff --git a/app/models/display_ad.rb b/app/models/display_ad.rb index f05b7c99d..02e450aec 100644 --- a/app/models/display_ad.rb +++ b/app/models/display_ad.rb @@ -10,6 +10,8 @@ class DisplayAd < ApplicationRecord POST_WIDTH = 775 SIDEBAR_WIDTH = 350 + enum display_to: { all: 0, logged_in: 1, logged_out: 2 }, _prefix: true + belongs_to :organization, optional: true has_many :display_ad_events, dependent: :destroy @@ -21,9 +23,17 @@ class DisplayAd < ApplicationRecord scope :approved_and_published, -> { where(approved: true, published: true) } - def self.for_display(area) + def self.for_display(area, user_signed_in) relation = approved_and_published.where(placement_area: area).order(success_rate: :desc) + relation = if user_signed_in + relation.where(display_to: %w[all logged_in]) + else + relation.where(display_to: %w[all logged_out]) + end + + relation.order(success_rate: :desc) + if rand(8) == 1 relation.sample else diff --git a/app/views/admin/display_ads/_form.html.erb b/app/views/admin/display_ads/_form.html.erb index a2c157b25..e76e09770 100644 --- a/app/views/admin/display_ads/_form.html.erb +++ b/app/views/admin/display_ads/_form.html.erb @@ -20,6 +20,28 @@ <%= select_tag :placement_area, options_for_select(display_ads_placement_area_options_array, selected: @display_ad.placement_area), include_blank: "Select...", class: "crayons-select" %> +
+
+ Display to user group +

Determines which user group will be able to see the Display Ad

+ + + + + + +
+
+
<%= label_tag :published, "Published:", class: "crayons-field__label" %> <%= select_tag :published, options_for_select([false, true], selected: @display_ad.published), class: "crayons-select" %> diff --git a/app/views/admin/display_ads/index.html.erb b/app/views/admin/display_ads/index.html.erb index ceb84252f..5adb5151f 100644 --- a/app/views/admin/display_ads/index.html.erb +++ b/app/views/admin/display_ads/index.html.erb @@ -22,6 +22,7 @@ Name Placement Area + Display to User Group Published Approved Success Rate @@ -32,6 +33,7 @@ <%= link_to display_ad.name, edit_admin_display_ad_path(display_ad) %> <%= display_ad.human_readable_placement_area %> + <%= display_ad.display_to %> <%= display_ad.published %> <%= display_ad.approved %> <%= display_ad.success_rate %> diff --git a/app/views/articles/_sidebar.html.erb b/app/views/articles/_sidebar.html.erb index 0f13bc2e1..a55d9f563 100644 --- a/app/views/articles/_sidebar.html.erb +++ b/app/views/articles/_sidebar.html.erb @@ -19,16 +19,16 @@
<% end %> <% end %> - <% cache("display-area-left-#{rand(5)}", expires_in: 5.minutes) do %> - <% @left_sidebar_ad = DisplayAd.for_display("sidebar_left") %> + <% cache("display-area-left-#{rand(5)}-#{user_signed_in?}", expires_in: 5.minutes) do %> + <% @left_sidebar_ad = DisplayAd.for_display("sidebar_left", user_signed_in?) %> <% if @left_sidebar_ad %>
<%= @left_sidebar_ad.processed_html.html_safe %>
<% end %> <% end %> - <% cache("display-area-left-2-#{rand(5)}", expires_in: 5.minutes) do %> - <% @second_left_sidebar_ad = DisplayAd.for_display("sidebar_left_2") %> + <% cache("display-area-left-2-#{rand(5)}-#{user_signed_in?}", expires_in: 5.minutes) do %> + <% @second_left_sidebar_ad = DisplayAd.for_display("sidebar_left_2", user_signed_in?) %> <% if @second_left_sidebar_ad %>
<%= @second_left_sidebar_ad.processed_html.html_safe %> diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index d111557c3..74eaa803c 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -191,7 +191,7 @@ <% cache("article-bottom-content-#{@article.id}-#{user_signed_in?}-#{(@organization || @user).latest_article_updated_at}", expires_in: 18.hours) do %> - <% @display_ad = DisplayAd.for_display("post_comments") %> + <% @display_ad = DisplayAd.for_display("post_comments", user_signed_in?) %> <% if @display_ad %>
<%= @display_ad.processed_html.html_safe %> diff --git a/app/views/sidebars/_homepage_content.html.erb b/app/views/sidebars/_homepage_content.html.erb index 480b63bf3..2f6d8c4c8 100644 --- a/app/views/sidebars/_homepage_content.html.erb +++ b/app/views/sidebars/_homepage_content.html.erb @@ -1,6 +1,6 @@