Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Forked from unixmonkey/load_sass_variables.rb
Last active July 15, 2017 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sixtyfive/323233 to your computer and use it in GitHub Desktop.
Save sixtyfive/323233 to your computer and use it in GitHub Desktop.
# Throw this in config/initializers and add
# @import variables.sass to the top of application.sass
# to have all variables in app_config.yml
# available as variables in your sass stylesheets
#
# First draft by unixmonkey (http://gist.github.com/323198)
# flatten method added by J. R. Schmid <jrs+git@weitnahbei.de>
app_config = YAML::load(File.open(File.join(RAILS_ROOT, 'config', 'app_config.yml')))
def flatten(input, prefix = nil)
output = {}
input.each do |key,value|
if value.class == Hash
output = output.merge(flatten(value, key))
else
output["#{prefix}_#{key}"] = value
end
end
return output
end
sass_variables = []
flatten(app_config).each do |key,value|
sass_variables << "!#{key}= #{value}"
end
File.open(File.join(RAILS_ROOT, 'public', 'stylesheets', 'sass', 'variables.sass'), 'w+') do |file|
file.write(sass_variables.join("\n"))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment