Skip to content

Instantly share code, notes, and snippets.

@Antti
Forked from chriseppstein/custom_functions.rb
Last active August 29, 2015 14:12
Show Gist options
  • Save Antti/bde3fd1ca184e0a7c432 to your computer and use it in GitHub Desktop.
Save Antti/bde3fd1ca184e0a7c432 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment