Skip to content

Instantly share code, notes, and snippets.

@st8998
Last active February 29, 2016 13:47
Show Gist options
  • Save st8998/932aeb9576dbca6b054c to your computer and use it in GitHub Desktop.
Save st8998/932aeb9576dbca6b054c to your computer and use it in GitHub Desktop.
class DefaultAvatarController < ApplicationController
respond_to :svg
COLORS = %w( #ff658c #fa80db #d185ee #b385f5 #7075ef #5098ea #a8b6cc #03bee9 #00bdb3
#00d590 #a0d930 #f3cb00 #efc47b #ff9740 #d8965b #ff755a #3e464e )
def avatar
model = params[:model_name].capitalize.constantize.find(params[:id])
@abbr = model.name.match(/(?:\s*(\w).*?\s+(\w).*)|(?:^\s*(\w)(\w).*)|(\w)/).to_a[1..-1].join('').upcase
@color = COLORS[model.id % COLORS.size]
expires_in 1.year, public: true
end
end
class DefaultAvatarController < ApplicationController
respond_to :svg
COLORS = %w( #ff658c #fa80db #d185ee #b385f5 #7075ef #5098ea #a8b6cc #03bee9 #00bdb3
#00d590 #a0d930 #f3cb00 #efc47b #ff9740 #d8965b #ff755a #3e464e )
NAMED_REGEXP = /(?:\s*(\w).*?\s+(\w).*)|(?:^\s*(\w)(\w).*)|(\w)/
helper_method :abbr, :color
def avatar
expires_in 1.year, public: true
end
private
def model
@model ||= params[:model_name].capitalize.constantize.find(params[:id])
end
def color
COLORS[model.id % COLORS.size]
end
def abbr
model.name.match(NAMED_REGEXP).to_a[1..-1].join('').upcase
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment