Accessing Rails Models or Helpers in SCSS - ruby-on-rails

I'm using Rails 3.1 and SCSS in the Asset Pipeline. Is there anyway to access Rails helpers or controller data in the SCSS file? Something like...
#main {
background-color: #{current_user.preferences.background_color}
}
I know I can set my own $variables but I'm not sure how I would populate them from the controller's data.

As far as I know this is not what Asset Pipeline was designed for.
Think about it, you have a rake assets:precompile command to convert all your .scss.erb files to a static .css file.
So how could you ever possibly access variables like current_user from that .scss.erb file?
In my opinion, it's not possible to get controller variables in .scss.erb or .coffee.erb.

You can chain template processors with Rails 3.1, so you can do my.css.scss.erb, and then embed your variables like so:
$user-background-color: <%= current_user.preferences.background_color %>
Then you can use the Sass variables throughout your SCSS.
I took a different approach to solving this problem for Rails 3.0: Using SASS with user-specified colors

Related

Using SASS variables in Rails code

I am afraid that this question is simply stupid, but... is there any option to use SASS variables in rails .erb files?
I defined a few colors in my variables.css.scss and I wish to use their values in my views or helpers in Rails. Maybe Rails can see some compiled sass resources or something?
Thank you for answers!
You can do it the other way around, ie using ruby code in your css/sccs code. It's a bit tricky, but it may help you :
First declare the color as a ruby constant :
# Put this into config/initializers/constants.rb for example
module Constants
FOO_COLOR = '#123456'
end
Next, rename the variables.css.scss into variables.css.scss.erb and use the constant created at the previous step
$fooColor: <%= Constants::FOO_COLOR %>
Finally use the color in your other scss files
#import "_variables";
#foo {
background-color: $fooColor;
}
And you can also use the Constants::FOO_COLOR in your Rails code too.
Be careful, you may be using precompiled assets in production. It will work with a constant as shown below, but it won't work if you want to change the value, or get it from a DB.

Where does the sass "font-url" method get defined?

bourbon uses font-url here.
Rails has the method font_url which I'm fairly certain is what is being invoked. However, I can't find where the connection between these two things is made. I have explored the codebases of bourbon, sass, sass-rais, and rails.
Where is font-url defined, and/or the connection between it and rails's font_url made?
update
Clarification: my ultimate goal is to define my own helpers in rubyland which are siblings to font_url.
font-url is a part of rails asset pipeline just like image-url. If you look at rail guides it clearly says
When using the asset pipeline, paths to assets must be re-written and sass-rails provides -url and -path helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.
So if you are using font-url("some_font") it will look for some_font in app/assets/font directory
Update:
As it is mentioned in docs that if you are using sass then your can use your assets with hypenated urls(image-url) but if you are using a ruby file then those helpers would be underscored (image_url) probably because Ruby doesn't like you having methods or variables with hyphens in the name syntactically, but semantically, there's nothing wrong with it

Can I access SASS variables from Rails classes?

In my Rails 4 application I have a number of SASS variables like this one:
$primary_color: #ec4158;
Is it possible to access that variable from a Rails class somehow?
Thanks for any help.
I think you have to make it the other way around.
define constants in an initializer or somewhere else (like this)
generate your sass using erb (yourstyle.sass.erb)
use the same constants in your prawn generation

How to make Rails render slim templates instead of erb templates?

In my current project I used to use erb as the default view template, then I decided to switch to slim, so I used tools to convert all the .erb files to .slim files.
Now I have erb and slim files co-exist in the same folder, the problem is after I restarted the rails server, it still rendered the old .erb files, not the .slim files as I expected.
I have already put 'gem slim-rails' in my Gemfile and updated it, so what else should I do to let Rails choose these slim templates to render instead of the erb templates?
PS: Do I have to delete all the .erb files? Because I want to keep them as a study purpose.
i think you can just change the name of the files which contain those erb templates, so no need to delete them. So when you want to use erb, change to original name.
Make this configuration in config/application.rb
class Application < Rails::Application
...............................
config.generators do |g|
g.template_engine :slim
end
end
It seems that the answer to my last question is YES, I have to delete all the .erb templates, only in this way can Rails render the .slim templates as expected.
Though I still don't know why Rails prefer erb than slim when they both exist, could it be that e in erb priors to s in slim?

How can I peek at intermediary files processed by asset pipeline?

I've got somefile.js.coffee.erb file which is processed by Rails asset pipeline. My ERB code returns some string that cannot be parsed by Coffee which result in SyntaxError exception. I would like to peek into generated somefile.js.coffee file, or in general any intermediary file processed by asset pipeline.
I've tried to examine Sprockets with no luck:
environment = Sprockets::Environment.new
MyApplication::Application.config.assets.paths.each {|p| environment.append_path p}
rerb = environment['somefile.js.coffee.erb']
rerb.source #=> it's already preprocessed
Or to look into \tmp\cache\assets but there are also only preprocessed files, additionaly obscured by fingerprinted name.
Maybe there is a way to hook into asset-pipeline I have no idea how..
Why I need ERB? To generate client-side-model stubs with fields and validations matching Rails model using KnockoutJS (https://github.com/dnagir/knockout-rails extended -> https://github.com/KrzysztofMadejski/knockout-rails).
I am using Rails '~> 3.2.12', sprockets (2.2.2).
Edit: I've ended up injecting erb code in ### comments, to sneak-peak at generated code while coffeescript file is still compiling:
###
<%= somefun() %>
###
Altough I would suggest using #Semyon Perepelitsa answer as it produces coffee script file as it is seen by coffee compiler.
Just remove "coffee" from the file extension temporarily: somefile.js.erb. You will see its intermediate state at /assets/somefile.js as it won't be processed by CoffeeScript.
I wonder if you can put <% binding.pry %> just before the line and mess around till you get it right. Never tried during a compile and don't use coffeescript. In theory, it should work (or is worth a shot) so long as you put gem pry in your Gemfile and run bundle first.

Resources