Solution, best practices for setting SASS variables from Rails / ActiveRecord data?

1,302 views
Skip to first unread message

Robert Head

unread,
Oct 8, 2010, 11:38:55 AM10/8/10
to Sass
I'm working on a Rails 2.3.8 project where I want to assign the SASS
variables to rarely-changing, but user-editable data.

I've tried a couple of different scenarios with generating sass with
controller actions and show.sass.erb views. Only partial success. So
far, there are too many layers of abstraction for me to decipher where
I'm going wrong and I'm looking for the collective wisdom here.

In short, how should I approach inserting ruby data into a sass file,
preferrably a sass partial, in Rails?

Nathan Weizenbaum

unread,
Oct 8, 2010, 2:05:15 PM10/8/10
to sass...@googlegroups.com
The best way to pass data into Sass is using user-defined Sass functions. See http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#adding_custom_functions for details.

If you're doing this, you'll want to make sure that your stylesheets get regenerated whenever the data changes. You can do so by calling Sass::Plugin.force_update_stylesheets.


--
You received this message because you are subscribed to the Google Groups "Sass" group.
To post to this group, send email to sass...@googlegroups.com.
To unsubscribe from this group, send email to sass-lang+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sass-lang?hl=en.


Robert Head

unread,
Oct 8, 2010, 5:34:28 PM10/8/10
to Sass
Thanks a bunch.

I've created:
/lib/sass_script_functions.rb
with the sample code.

That works great. However, I'm not clear on the "Rails way" to bring
my data into scope in the module. For example, how would I bring
@user into scope in the following example?

module Sass::Script::Functions

def reverse(string)
assert_type string, :String
Sass::Script::String.new(string.value.reverse)
end

def favorite_color()
Sass::Script::String.new(@user.favorite_color)
end

end


On Oct 8, 11:05 am, Nathan Weizenbaum <nex...@gmail.com> wrote:
> The best way to pass data into Sass is using user-defined Sass functions.
> Seehttp://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#adding_cu...
> details.
>
> If you're doing this, you'll want to make sure that your stylesheets get
> regenerated whenever the data changes. You can do so by calling
> Sass::Plugin.force_update_stylesheets.
>
>
>
> On Fri, Oct 8, 2010 at 8:38 AM, Robert Head <robert.h...@gmail.com> wrote:
> > I'm working on a Rails 2.3.8 project where I want to assign the SASS
> > variables to rarely-changing, but user-editable data.
>
> > I've tried a couple of different scenarios with generating sass with
> > controller actions and show.sass.erb views.  Only partial success.  So
> > far, there are too many layers of abstraction for me to decipher where
> > I'm going wrong and I'm looking for the collective wisdom here.
>
> > In short, how should I approach inserting ruby data into a sass file,
> > preferrably a sass partial, in Rails?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Sass" group.
> > To post to this group, send email to sass...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > sass-lang+...@googlegroups.com<sass-lang%2Bunsubscribe@googlegroups .com>
> > .

Nathan Weizenbaum

unread,
Oct 8, 2010, 5:38:44 PM10/8/10
to sass...@googlegroups.com
You can pass information via the :custom Sass option (http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#custom-option). Be careful, though, because if you're using that with Sass::Plugin you're dealing with global state, which is dangerous. It may be a good idea for you to use Sass::Engine directly, although if you do that make sure you have good caching set up.

To unsubscribe from this group, send email to sass-lang+...@googlegroups.com.

Robert Head

unread,
Oct 8, 2010, 5:53:48 PM10/8/10
to Sass
Docs say:
:custom
An option that’s available for individual applications to set to make
data available to custom Sass functions.

# environment.rb

Sass::Plugin.options[:custom] = ??? Not sure I understand what goes
here.

More thanks in advance.

Nathan Weizenbaum

unread,
Oct 8, 2010, 6:03:07 PM10/8/10
to sass...@googlegroups.com
Again, this is pretty tricky stuff. You don't set :custom in environment.rb; you need to set it when you want to generate your CSS. I think dealing with Sass::Engine directly, where you can set the options per-engine, is really the best solution here.

To unsubscribe from this group, send email to sass-lang+...@googlegroups.com.

Chris Eppstein

unread,
Oct 8, 2010, 6:05:52 PM10/8/10
to sass...@googlegroups.com
Something like this: http://gist.github.com/617650

note: I didn't run this or anything -- it's probably broken. But it's directionally right.

chris

Bill Lazar

unread,
Oct 8, 2010, 7:18:15 PM10/8/10
to sass...@googlegroups.com
Thanks!

Bill

Nathan Weizenbaum

unread,
Oct 10, 2010, 3:19:54 AM10/10/10
to sass-lang
Done.

On Fri, Oct 8, 2010 at 4:18 PM, Bill Lazar <bstg...@gmail.com> wrote:
Thanks!

Bill
Reply all
Reply to author
Forward
0 new messages