Skip to content

Commit 4971c9e

Browse files
sponsor banner ad stimulus controller (#276)
* sponsor banner ad stimulus controller For refrence this feature is being inmplemented using the same method as the sponsor footer component Enables a website creator/editor to add a dynamic sponsor banner ad to any stataic page. The command that will be required will be ``` <div data-controller="banner-ads" data-banner-ads-event-slug-value="railsconf-2022" data-banner-ads-index-value="0"></div> ``` For now I coded in an inital index, will probably change this be a random index in the ads array. * random starting index, and some css * final CSS tweaks
1 parent c261d1e commit 4971c9e

File tree

9 files changed

+90
-2
lines changed

9 files changed

+90
-2
lines changed

app/assets/stylesheets/themes/default/application.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ body {
3939

4040
header + #content {
4141
background-color: var(--main_content_background);
42-
padding: 0 20px 0 20px;
43-
margin-top: 100px;
42+
margin-top: 50px;
4443
}
4544

4645
p {

app/assets/stylesheets/themes/default/nav.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
width: 100%;
1010
background: var(--nav_background_color);
1111
color: var(--nav_text_color);
12+
z-index: 11;
1213

1314
#header-logo-container {
1415
display: flex;

app/assets/stylesheets/themes/default/sponsors.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@
162162
}
163163
}
164164

165+
.banner-ad-wrapper {
166+
position: relative;
167+
height: 170px;
168+
padding-bottom: 30px;
169+
max-height: 170px;
170+
171+
.banner-ad-item {
172+
position: absolute;
173+
top: 0;
174+
right: 0;
175+
width: 100%;
176+
transition: all 200ms ease-in-out;
177+
178+
img {
179+
margin: auto;
180+
max-height: 130px;
181+
}
182+
}
183+
}
184+
165185
@media screen and (max-width: 1200px) and (orientation: portrait),
166186
(max-width: 1123px) and (orientation: landscape) {
167187
.sponsors-wrapper {

app/controllers/sponsors_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ def sponsors_footer
1010
@sponsors_in_footer = Sponsor.published.with_footer_image.order_by_tier
1111
render layout: false
1212
end
13+
14+
def banner_ads
15+
@sponsors_in_banner = Sponsor.published.with_banner_ad
16+
render layout: false
17+
end
1318
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Controller } from 'stimulus'
2+
3+
export default class extends Controller {
4+
static targets = [ 'ad' ]
5+
static values = {
6+
eventSlug: String,
7+
}
8+
9+
connect() {
10+
const path = `/${this.eventSlugValue}/banner_ads`
11+
fetch(path)
12+
.then((res) => res.text())
13+
.then((html) => {
14+
const fragment = document
15+
.createRange()
16+
.createContextualFragment(html);
17+
this.element.appendChild(fragment);
18+
19+
this.config()
20+
})
21+
}
22+
23+
config() {
24+
this.setAdInterval()
25+
this.setIndex()
26+
27+
this.showCurrentIndex()
28+
this.startAdRotation()
29+
}
30+
31+
showCurrentIndex() {
32+
this.adTargets.forEach((ad, index) => {
33+
ad.hidden = index != this.index;
34+
})
35+
}
36+
37+
setAdInterval() {
38+
this.adInterval = 7500; // 7.5 second ad interval
39+
}
40+
41+
setIndex() {
42+
this.index = Math.floor(Math.random() * this.adTargets.length);
43+
}
44+
45+
startAdRotation() {
46+
setInterval(() => {
47+
48+
this.index += 1
49+
if (this.index >= this.adTargets.length) {
50+
this.index = 0
51+
}
52+
53+
this.showCurrentIndex()
54+
}, this.adInterval)
55+
}
56+
}

app/models/sponsor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Sponsor < ApplicationRecord
1111

1212
scope :published, -> { where(published: true) }
1313
scope :with_footer_image, -> { joins(:footer_logo_attachment) }
14+
scope :with_banner_ad, -> { joins(:banner_ad_attachment) }
1415
scope :order_by_tier, -> {
1516
order_case = "CASE tier"
1617
TIERS.each_with_index do |tier, index|
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.banner-ad-wrapper
2+
- @sponsors_in_banner.each do |sponsor|
3+
= link_to image_tag( sponsor.banner_ad, alt: sponsor.name), sponsor.url, target: "_blank", class: "banner-ad-item", data: { 'banner-ads-target': 'ad' }

app/views/sponsors/show.html.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
= tier
1010
.sponsors-wrapper
1111
= render sponsors
12+
13+
%div{ data: { 'controller': 'banner-ads', 'banner-ads-event-slug-value': 'railsconf-2022', 'banner-ads-index-value': '0' } }

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
get '/(:slug)/program', to: 'programs#show', as: :program
167167
get '/(:slug)/schedule', to: 'schedule#show', as: :schedule
168168
get '/(:slug)/sponsors_footer', to: 'sponsors#sponsors_footer'
169+
get '/(:slug)/banner_ads', to: 'sponsors#banner_ads'
169170
get '/(:slug)/sponsors', to: 'sponsors#show', as: :sponsors
170171
get '/(:slug)/:page', to: 'pages#show', as: :page
171172

0 commit comments

Comments
 (0)