Import SASS file from database instead of filesystem
Since you're using Compass to compile, you can add a custom Sass importers in the Compass config file. For example, compiling using compass compile -c config.rb
, you would include something like this in your config.rb
file:
require File.join(File.dirname(__FILE__), 'importer.rb')
Sass.load_paths << Sass::Importers::Custom.new()
Then in importer.rb
in the same directory, you would include your importer definition:
module Sass
module Importers
class Custom < Base
def find(name, options)
if name == '[globals]'
options[:syntax] = :scss
options[:filename] = 'globals'
options[:importer] = self
return Sass::Engine.new("$imported-variable: blue;", options)
else
return nil
end
end
def find_relative(uri, base, options)
nil
end
def key(uri, options)
[self.class.name + ":" + uri, uri]
end
def mtime(uri, options)
nil
end
def to_s
'[custom]'
end
end
end
end
Then in your Sass file you can use the importer:
@import '[globals]';
p {
color: $imported-variable;
}
Of course, this is just a dummy implementation that only accepts a URI matching "[globals]"
. You'll need to supply your own implementation that accesses your MySQL database, as I don't have any experience with database access in Ruby. Hopefully this should get you a little closer, though, in addition to the links that @Sean has provided.
import SASS partial over http instead of filesystem
This documentation is discussing the fact that you can implement your own importer; HTTP is being used as an example. Fortunately, it is not too difficult to do so.
Here, I've implemented a simple HTTP importer: https://gist.github.com/1111803
It doesn't cache as aggressively as it could, and you should be aware that Sass will use it in addition to the filesystem to look for all imports (if you use a framework like Compass, there are many of these). If you need more performance, you should probably cache the failures in this case. Still, it seems to work in my testing.
You can use it simply by requiring the sass_http.rb
file and then adding it to the load path:
require 'sass_http'
Sass::Plugin.options[:load_paths] ||= []
Sass::Plugin.options[:load_paths] << Sass::Importers::HTTP.new("http://stylesheets.example.com/")
The path currently must refer to a directory (i.e. end in a slash). You should get all of the usual debugging information (with HTTP, rather than filesystem, paths).
Get SASS from database (compile passed data instead of reading from file)
I ended up using this chunk of code. Totally not elegant, but I hope it will help someone with pervertish needs.
sql.rb in sass gem dir:
require 'dbi'
require 'sass'
require config with database credentials
if File.exist?(config)
require config
SQL_OPTIONS = {
:style => :nested,
:load_paths => ['.'],
:cache => true,
:cache_location => './.sass-cache',
:syntax => :scss,
:filesystem_importer => Sass::Importers::Filesystem,
:css_location => "./public/stylesheets",
:always_update => true,
:template_location => [["scss", "css"]]
}.freeze
db = DBI.connect('dbi:ODBC:driver={SQL Server};server=' + $db_host, $db_user, $db_pass)
db.select_all('SELECT name, scope, content FROM ' + $db_name + '.dbo.scss') do | row |
File.open(Dir.pwd + '/css/' + row['name'] + '.css', 'w') do |css|
css.puts Sass::Engine.new(row['content'], SQL_OPTIONS).render
puts ' overwrite css/' + row['name'] + '.css [db]'
end
end
else
puts 'ignore database stylesheets, config_ruby doesn\'t exist'
end
In lib/sass/engine.rb I included this file at the end:
require File.expand_path(File.dirname(__FILE__) + '../../../sql')
It requires additional dbi
and dbd-odbc
gems and affects sass --update as well as --watch.
Hope this helps someone.
Related Topics
Can the Rails Tmp/Cache/Assets Files Be Safely Deleted
Rails 6: Zeitwerk::Nameerror Doesn't Load Class from Module
How to Marshal a Lambda (Proc) in Ruby
Best Way to Split Arrays into Multiple Small Arrays in Ruby
How to Check If a String Has at Least One Number in It Using Ruby
What Is the Opposite of Ruby's Include
Travis Ci: Fatal: Role Does Not Exist
How to Check Ruby Syntax Error in Ruby Code
Is Systemexit a Special Kind of Exception
Rails Server Does Not Start by Command "Rails S"
Couldn't Find User with Id=Sign_Out
How to Get Sinatra to Work with Httpclient
Ruby Regex Gsub a Line in a Text File
How to Disable Wait Time for Watir
Understanding Namespaces in Ruby
Cannot Start Rails Server, "No Such File to Load -- Bundler/Setup"