The personal finance app for everyone (by everyone)
Ruby
Family merchant logo (#660)
soky srm4cc321a083c1
diff --git a/app/controllers/family_merchants_controller.rb b/app/controllers/family_merchants_controller.rbindex 4d41db1322..642f8d646d 100644--- a/app/controllers/family_merchants_controller.rb+++ b/app/controllers/family_merchants_controller.rb@@ -116,7 +116,7 @@ class FamilyMerchantsController < ApplicationControllerdef merchant_params# Handle both family_merchant and provider_merchant param keyskey = params.key?(:family_merchant) ? :family_merchant : :provider_merchant- params.require(key).permit(:name, :color)+ params.require(key).permit(:name, :color, :website_url)enddef all_family_merchantsdiff --git a/app/models/family_merchant.rb b/app/models/family_merchant.rbindex eeb881883c..314ba1ebbe 100644--- a/app/models/family_merchant.rb+++ b/app/models/family_merchant.rb@@ -4,6 +4,7 @@ class FamilyMerchant < Merchantbelongs_to :familybefore_validation :set_default_color+ before_save :generate_logo_url_from_website, if: :should_generate_logo?validates :color, presence: truevalidates :name, uniqueness: { scope: :family }@@ -12,4 +13,25 @@ class FamilyMerchant < Merchantdef set_default_colorself.color = COLORS.sampleend++ def should_generate_logo?+ website_url_changed? || (website_url.present? && logo_url.blank?)+ end++ def generate_logo_url_from_website+ if website_url.present? && Setting.brand_fetch_client_id.present?+ domain = extract_domain(website_url)+ self.logo_url = "https://cdn.brandfetch.io/#{domain}/icon/fallback/lettermark/w/40/h/40?c=#{Setting.brand_fetch_client_id}"+ elsif website_url.blank?+ self.logo_url = nil+ end+ end++ def extract_domain(url)+ original_url = url+ normalized_url = url.start_with?("http://", "https://") ? url : "https://#{url}"+ URI.parse(normalized_url).host&.sub(/\Awww\./, "")+ rescue URI::InvalidURIError+ original_url.sub(/\Awww\./, "")+ endenddiff --git a/app/views/family_merchants/_form.html.erb b/app/views/family_merchants/_form.html.erbindex 49363d7ea5..01ac18f67e 100644--- a/app/views/family_merchants/_form.html.erb+++ b/app/views/family_merchants/_form.html.erb@@ -21,6 +21,12 @@<div class="relative flex items-center border border-secondary rounded-lg text-subdued"><%= f.text_field :name, placeholder: t(".name_placeholder"), autofocus: true, required: true, data: { color_avatar_target: "name" } %></div>+ <% if family_merchant.is_a?(FamilyMerchant) %>+ <div class="relative flex items-center border border-secondary rounded-lg text-subdued">+ <%= f.text_field :website_url, placeholder: t(".website_placeholder") %>+ </div>+ <p class="text-xs text-subdued"><%= t(".website_hint") %></p>+ <% end %></section><section>diff --git a/config/locales/views/merchants/en.yml b/config/locales/views/merchants/en.ymlindex 4f2bec525f..aeb0fead57 100644--- a/config/locales/views/merchants/en.yml+++ b/config/locales/views/merchants/en.yml@@ -11,6 +11,8 @@ en:title: Edit merchantform:name_placeholder: Merchant name+ website_placeholder: Website (e.g., starbucks.com)+ website_hint: Enter the merchant's website to automatically display their logoindex:empty: No merchants yetnew: New merchant