Skip to content

armgit / sure

Public Imported from GitHub
0

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.rb
index 4d41db1322..642f8d646d 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 eeb881883c..314ba1ebbe 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 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.yml
index 4f2bec525f..aeb0fead57 100644
--- a/config/locales/views/merchants/en.yml
+++ b/config/locales/views/merchants/en.yml
@@ -11,6 +11,8 @@ en:
title: Edit merchant
form:
name_placeholder: Merchant name
+ website_placeholder: Website (e.g., starbucks.com)
+ website_hint: Enter the merchant's website to automatically display their logo
index:
empty: No merchants yet
new: New merchant