diff --git a/app/controllers/family_merchants_controller.rb b/app/controllers/family_merchants_controller.rb index 4d41db132..642f8d646 100644 --- a/app/controllers/family_merchants_controller.rb +++ b/app/controllers/family_merchants_controller.rb @@ -116,7 +116,7 @@ class FamilyMerchantsController < ApplicationController def merchant_params # Handle both family_merchant and provider_merchant param keys key = params.key?(:family_merchant) ? :family_merchant : :provider_merchant - params.require(key).permit(:name, :color) + params.require(key).permit(:name, :color, :website_url) end def all_family_merchants diff --git a/app/models/family_merchant.rb b/app/models/family_merchant.rb index eeb881883..314ba1ebb 100644 --- a/app/models/family_merchant.rb +++ b/app/models/family_merchant.rb @@ -4,6 +4,7 @@ class FamilyMerchant < Merchant belongs_to :family before_validation :set_default_color + before_save :generate_logo_url_from_website, if: :should_generate_logo? validates :color, presence: true validates :name, uniqueness: { scope: :family } @@ -12,4 +13,25 @@ class FamilyMerchant < Merchant def set_default_color self.color = COLORS.sample end + + 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\./, "") + end end diff --git a/app/views/family_merchants/_form.html.erb b/app/views/family_merchants/_form.html.erb index 49363d7ea..01ac18f67 100644 --- a/app/views/family_merchants/_form.html.erb +++ b/app/views/family_merchants/_form.html.erb @@ -21,6 +21,12 @@
<%= t(".website_hint") %>
+ <% end %>