Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import SASS file from database instead of filesystem

I don't have much experience with Ruby. I want to @import sass from database instead of filesystem. I did not find any examples online. How should I go about implementing this. I see that I have to extend an importer class but since I have no understanding of ruby I need to know where in my filesystem does this class reside (Just to check it out) and in general what are the basic steps involved.

More information

MySQL database contains sass content. So in my web application I accept sass (as string) from user which may contain an import statement for example :

@import test.scss
body  { color:red } 

Now in my MySQL DB I will have something like this

Table  sass_files
column_name             filename  |   content
example row             test.scss |   p {color:blue;}

I want to make this import work, sure I can just do a regular expression match to get the filename from the user input and then query the DB by that file name and get the content. But I read that there is a nice way to make ruby/sass use DB as a load-path instead of filesystem.

UPDATE

SO i created a dummy custom importer class with find method as

def find(name, options)
  Sass::Engine.new("p { color :blue; }", options)
end

How do i add this importer to sass load paths w/o using ruby, like can i make change to the source files in the sass gem folder and add this importer to lib/sass/importers?

Thanks

like image 803
nikhil Avatar asked Nov 26 '12 04:11

nikhil


2 Answers

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.

like image 71
hopper Avatar answered Oct 18 '22 10:10

hopper


Out of the box, sass will only import local files (anything else is compiled into a CSS @import statement) but as the sass docs explain you can write your own custom importers by extending the Sass::Importers::Base class.

This answer to another question gives an example of a custom HTTP importer. You could approach this in (at least) two ways: write a custom importer for your database or serve the sass file over HTTP from the database (via a simple PHP page or something)

like image 37
Sean Redmond Avatar answered Oct 18 '22 09:10

Sean Redmond