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
Related
When we have a directory under app/ that we want Zeitwerk to work off of, and say that naming happens to be something like
app/stuff/graphql.rb
app/stuff/graphql_error.rb
then Zeitwerk is looking for some module Stuff that has some module or class Graphql. But in my code, I am always writing my modules and classes as GraphQL to match that convention. So Zeitwerk is now throwing Zeitwerk::NameError as it tries to work with the code. I don't want to use Stuff::GraphqlError, I want to use Stuff::GraphQLError. How do I trick Zeitwerk here?
I believe Zeitwerk has inflectors that can be used for this:
https://github.com/fxn/zeitwerk#inflection
In order for app/stuff to act as a namespace, you have to put app itself as an autoload path. This is a bit tricky, please have a look at https://guides.rubyonrails.org/classic_to_zeitwerk_howto.html#having-app-in-the-autoload-paths.
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.
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
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
I have an Array extension method I want to use in my Rails 3 project. Where should it live?
I have a app/classes where I originally put it (array_extensions.rb), and in my config/application.rb I load the path: config.autoload_paths += %W(#{Rails.root}/app/classes). However, when I drop to rails console the extension is not loaded.
Is there a pre-definded place I should put my extension methods for Rails 3? Or, a pre-definded way to add them? I know Rails has it's own extensions methods for Array. Should I add mine to active_support/core_ext/array/conversions.rb?
What's the best practice for Rails 3?
The better way is create your extension in lib/core_ext directory to understand easyly where is your core_ext.
After create an initializer to require this file.
All .rb files in config/initializers are required into the environment at startup; you should be putting extensions in there.