Skip to content

Instantly share code, notes, and snippets.

@chriseppstein
Created October 8, 2010 22:03
Show Gist options
  • Save chriseppstein/617650 to your computer and use it in GitHub Desktop.
Save chriseppstein/617650 to your computer and use it in GitHub Desktop.
module Sass::Script::Functions
def user_color
color_values = options[:custom][:user].color.
scan(/^#?(..?)(..?)(..?)$/).first.
map {|num| num.ljust(2, num).to_i(16)}
Sass::Script::Color.new(color_values)
end
end
map.stylesheet "/stylesheets/*path", :controller => "runtime_stylesheets", :action => "show"
# Note: This might not work at all -- I didn't run it or anything.
class RuntimeStylesheetsController < ApplicationController
before_filter :lookup_user
caches_action :show, :cache_path => :show, :ttl => 24.hours
def show
contents = File.read("#{Rails.root}/app/runtime_stylesheets/#{params[:path].join("/")}")
options = {:custom => {:user => @user}}
# If you're using compass & assuming you have compass-rails installed and activated in production.
options.update(Compass.configuration.to_sass_engine_options)
engine = Sass::Engine.new(contents, options)
render :text => engine.render, :content_type => "text/css"
end
private
def lookup_user
@user = User.find_by_id(session[:user_id])
render :text => "", :status => :not_found unless @user
end
def show_url
"user_stylesheets/#{@user.id}/#{params[:path].join("/")}"
end
end
body {
background-color: user-color();
}
@benissimo
Copy link

Thanks for improving on the user_color example. Returning a Sass::Script::Color instead of a Sass::Script::String is much better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment