Apply banner default colors to dev seeds

Banners created through the admin form were getting the default color.
However, banners created by other means (like the `db:dev_seed` rake
task) were not getting these default values.

This feature was originally implemented when we were using Rails 4.
With Rails 5, we can provide default values to all new banners and
simplify the code at the same time thanks to its `attribute` method.

Now, when creating a new banner, instead of getting a blank space, we
get an empty line with the banner's default background color, which most
users won't know what it's about until they fill in the banner's title.
So we're not displaying the content of the banner when it's empty,
thanks to the `:empty` CSS pseudoclass.
This commit is contained in:
Javi Martín
2020-08-12 14:45:45 +02:00
parent 361b7ee09d
commit 0b83be6837
5 changed files with 16 additions and 20 deletions

View File

@@ -2292,6 +2292,10 @@ table {
a > * { a > * {
@include grid-row; @include grid-row;
padding: 0 rem-calc(16); padding: 0 rem-calc(16);
&:empty {
display: none;
}
} }
+ .budget.expanded, + .budget.expanded,

View File

@@ -3,22 +3,6 @@ module BannersHelper
@banners.present? && @banners.count > 0 @banners.present? && @banners.count > 0
end end
def banner_default_bg_color
"#e7f2fc"
end
def banner_default_font_color
"#222222"
end
def banner_bg_color_or_default
@banner.background_color.presence || banner_default_bg_color
end
def banner_font_color_or_default
@banner.font_color.presence || banner_default_font_color
end
def banner_target_link(banner) def banner_target_link(banner)
link_to banner.target_url do link_to banner.target_url do
tag.h2(banner.title, style: "color:#{banner.font_color}") + tag.h2(banner.title, style: "color:#{banner.font_color}") +

View File

@@ -2,6 +2,9 @@ class Banner < ApplicationRecord
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases include ActsAsParanoidAliases
attribute :background_color, default: "#e7f2fc"
attribute :font_color, default: "#222222"
translates :title, touch: true translates :title, touch: true
translates :description, touch: true translates :description, touch: true
include Globalizable include Globalizable

View File

@@ -56,8 +56,7 @@
<p class="help-text"><%= t("admin.shared.color_help") %></p> <p class="help-text"><%= t("admin.shared.color_help") %></p>
<div class="row collapse"> <div class="row collapse">
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= f.text_field :background_color, label: false, type: :color, <%= f.text_field :background_color, label: false, type: :color %>
value: banner_bg_color_or_default %>
</div> </div>
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= f.text_field :background_color, label: false, id: "background_color_input" %> <%= f.text_field :background_color, label: false, id: "background_color_input" %>
@@ -70,8 +69,7 @@
<p class="help-text"><%= t("admin.shared.color_help") %></p> <p class="help-text"><%= t("admin.shared.color_help") %></p>
<div class="row collapse"> <div class="row collapse">
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= f.text_field :font_color, label: false, type: :color, <%= f.text_field :font_color, label: false, type: :color %>
value: banner_font_color_or_default %>
</div> </div>
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= f.text_field :font_color, label: false, id: "font_color_input" %> <%= f.text_field :font_color, label: false, id: "font_color_input" %>

View File

@@ -11,4 +11,11 @@ describe Banner do
it "is valid" do it "is valid" do
expect(banner).to be_valid expect(banner).to be_valid
end end
it "assigns default values to new banners" do
banner = Banner.new
expect(banner.background_color).to be_present
expect(banner.font_color).to be_present
end
end end