mirror of
https://github.com/we-promise/sure.git
synced 2026-05-31 08:19:03 +00:00
Goals::AvatarComponent had `attr_reader :icon` which shadowed the global `icon` view helper. Template called `icon(icon, size:, color:)` which Ruby resolved against the attr-reader (zero-arity), throwing "wrong number of arguments (given 2, expected 0)" the moment a goal had a saved icon and the show page tried to render its avatar. - Drop `:icon` from attr_reader; expose as `icon_name` instead. - Template uses `helpers.icon(icon_name, ...)` matching the Goals::StatusPillComponent pattern (other Goals VCs already use `helpers.icon`). Reproduced + verified live via Playwright: edit modal → pick an icon → save → show page renders the new avatar with the SVG. Same for create flow (new modal → pick icon → step 2 → submit → show renders).
61 lines
1.5 KiB
Ruby
61 lines
1.5 KiB
Ruby
class Goals::AvatarComponent < ApplicationComponent
|
|
SIZES = {
|
|
"sm" => { box: "w-6 h-6", text: "text-[10px]", radius: "rounded-md" },
|
|
"md" => { box: "w-9 h-9", text: "text-sm", radius: "rounded-lg" },
|
|
"lg" => { box: "w-11 h-11", text: "text-base", radius: "rounded-xl" },
|
|
"xl" => { box: "w-16 h-16", text: "text-2xl", radius: "rounded-2xl" }
|
|
}.freeze
|
|
|
|
PALETTE = Goal::COLORS
|
|
|
|
# Deterministic color pick from the palette so the same string maps to
|
|
# the same color across processes (Ruby's String#hash is randomized per
|
|
# boot for DoS protection — not stable enough for visual identity).
|
|
def self.color_for(name)
|
|
return PALETTE.first if name.blank?
|
|
PALETTE[Digest::MD5.hexdigest(name).to_i(16) % PALETTE.size]
|
|
end
|
|
|
|
def initialize(goal: nil, name: nil, color: nil, icon: nil, size: "md")
|
|
@goal = goal
|
|
@name = name || goal&.name
|
|
@color = color || goal&.color || Goal::COLORS.first
|
|
@icon = icon || goal&.icon
|
|
@size = SIZES.key?(size) ? size : "md"
|
|
end
|
|
|
|
attr_reader :color
|
|
|
|
# Don't expose @icon via attr_reader — `icon` collides with the global
|
|
# icon helper used inside the template.
|
|
def icon_name
|
|
@icon
|
|
end
|
|
|
|
def initial
|
|
return "?" if @name.blank?
|
|
@name.strip.first&.upcase || "?"
|
|
end
|
|
|
|
def icon_size
|
|
case @size
|
|
when "sm" then "xs"
|
|
when "md" then "sm"
|
|
when "lg" then "md"
|
|
when "xl" then "xl"
|
|
end
|
|
end
|
|
|
|
def box_classes
|
|
SIZES[@size][:box]
|
|
end
|
|
|
|
def text_classes
|
|
SIZES[@size][:text]
|
|
end
|
|
|
|
def radius_classes
|
|
SIZES[@size][:radius]
|
|
end
|
|
end
|