Skip to content
Snippets Groups Projects
Commit f706d281 authored by Teal's avatar Teal
Browse files

colorful avatars

parent 4056861b
Branches
No related tags found
No related merge requests found
class User < ApplicationRecord class User < ApplicationRecord
after_initialize :set_avatar_color
def text_color
r, g, b = avatar_color.delete_prefix('#').chars.each_slice(2).map { |hex| hex.join.to_i(16) }
# Calculate relative luminance (WCAG 2.0 formula)
luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255
# Choose text color based on luminance threshold
luminance > 0.5 ? "#000000" : "#ffffff"
end
def initials
name.split(/\s+/).map(&:first).join('')
end
def set_avatar_color
return unless self.avatar_color.nil?
r = rand(256)
g = rand(256)
b = rand(256)
# r = [r, 128].max
# g = [g, 128].max
# b = [b, 128].max
self.avatar_color = "##{r.to_s(16).rjust(2, '0')}#{g.to_s(16).rjust(2, '0')}#{b.to_s(16).rjust(2, '0')}"
end
end end
<div class="relative inline-flex items-center justify-center h-8 p-2 overflow-hidden rounded-full" style="background-color: <%= user.avatar_color %>" title="<%= user.name %>">
<span class="font-medium" style="color: <%= user.text_color %>"><%= user.name %></span>
</div>
...@@ -59,7 +59,7 @@ current_time = @sessions_by_date[@conference.days.first].first.starts_at.advance ...@@ -59,7 +59,7 @@ current_time = @sessions_by_date[@conference.days.first].first.starts_at.advance
<ul class="list-disc"> <ul class="list-disc">
<% session.assignments.each do |assignment| %> <% session.assignments.each do |assignment| %>
<li> <li>
<span class="assigned-user"><%= assignment.user.name %></span> <span class="assigned-user"><%= render partial: 'application/user_avatar', locals: { user: assignment.user } %></span>
<%= link_to '[Remove]', conference_session_assignments_path(session.conference, session, user_id: assignment.user_id), data: { turbo_method: :delete, confirm: 'Are you sure?' } %> <%= link_to '[Remove]', conference_session_assignments_path(session.conference, session, user_id: assignment.user_id), data: { turbo_method: :delete, confirm: 'Are you sure?' } %>
</li> </li>
<% end %> <% end %>
......
<%= form_with url: conference_session_assignments_path(session.conference, session), method: :post, local: true do |f| %> <%= form_with url: conference_session_assignments_path(session.conference, session), method: :post, local: true do |f| %>
<%= f.select :user_id, options_from_collection_for_select(@users, :id, :name) %> <%= f.select :user_id, options_from_collection_for_select(@users - session.assignments.collect(&:user), :id, :name) %>
<%= f.submit "Assign", class: 'primary' %> <%= f.submit "Assign", class: 'primary' %>
<% if @assignment&.errors&.any? %> <% if @assignment&.errors&.any? %>
<div class="alert alert-danger"> <div class="alert alert-danger">
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<ul> <ul>
<% @session.assignments.each do |assignment| %> <% @session.assignments.each do |assignment| %>
<li> <li>
<%= assignment.user.name %> <%= render partial: 'application/user_avatar', locals: { user: assignment.user } %>
<%= link_to '[Remove]', conference_session_assignments_path(@session.conference, @session, user_id: assignment.user_id), data: { turbo_method: :delete, confirm: 'Are you sure?' } %> <%= link_to '[Remove]', conference_session_assignments_path(@session.conference, @session, user_id: assignment.user_id), data: { turbo_method: :delete, confirm: 'Are you sure?' } %>
</li> </li>
<% end %> <% end %>
......
class AddAvatarColorToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :avatar_color, :string
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_05_24_091736) do ActiveRecord::Schema[7.1].define(version: 2024_05_24_124124) do
create_table "assignments", force: :cascade do |t| create_table "assignments", force: :cascade do |t|
t.integer "user_id", null: false t.integer "user_id", null: false
t.integer "session_id", null: false t.integer "session_id", null: false
...@@ -187,6 +187,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_24_091736) do ...@@ -187,6 +187,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_24_091736) do
t.string "email" t.string "email"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "avatar_color"
end end
add_foreign_key "assignments", "sessions" add_foreign_key "assignments", "sessions"
......
...@@ -32,4 +32,5 @@ end ...@@ -32,4 +32,5 @@ end
User.find_or_create_by!(email: "teal@teal.is") do |user| User.find_or_create_by!(email: "teal@teal.is") do |user|
user.name = "Teal" user.name = "Teal"
user.avatar_color = "#14bfb5"
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment