Rails 4 + Sprockets: Allowing the use of js.erb files - ruby-on-rails

I am attempting to use ES6 syntax in my Rails 4 app and have had some success between the use of sprockets-es6 (0.9.2), sprockets-rails (3.0.4) and sprockets (3.6.0).
The only issue I'm having is that my files need to end in .es6 in order to enable proper compilation and I'd like to be able to use a .es6.erb or .js.erb file type to allow me to use embedded ruby <%= foo %>.
Does anyone know of a way around this?

This question is a few years old and the OP specifies Sprockets 3.6. But I'm guessing many will find this question when looking for a solution to using erb files with a more modern version of sprockets.
If you're using Sprockets 4 and want to use .js.erb you'll need to 'register_mime_type'.
e.g. add the following to a new file called ./config/initializer/register_mime_type.rb
Sprockets.register_mime_type 'application/javascript', extensions: ['.js.erb']
This is described in Extending Sprockets.

Related

Overwrite haml-rails scaffold templates

I want to customize the controller views generated by haml-rails. According to the Rails guide I am supposed to put my customized templates (e.g. index.html.haml) into lib/templates/[subfolders].
In this case I tried several subfolders (e.g. lib/templates/haml/scaffold, lib/generators/haml/scaffold/templates) but I could not get my custom templates to be used.
I know that I could write another generator easily, but I am wondering if there is a more DRY way to do so. In theory it should be possible:
In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths.
I am using Rails (4.2.5.2), haml (4.0.7) and haml-rails (0.9.0).
Holy moly. It worked after all. It is correct to put the templates into lib/templates/haml/scaffold. And now comes the catch: spring will cache the templates. Hence, you must either restart spring after changes or prepend DISABLE_SPRING to the generator command:
DISABLE_SPRING=true rails g scaffold ...

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

Rails Application templates remove a default gem

When using a rails application template is possible to make so that some default gems (sqlite3, coffescript and sass) are not included in the Gemfile?
I'm sure you've solved this problem in the last 7 years, but for everyone else, the best method I've seen for doing this is to gsub it out of the file:
# Removes sqlite3, coffee-rails, and sass-rails gems
gsub_file "Gemfile", /^gem\s+["']sqlite3["'].*$/,''
gsub_file "Gemfile", /^gem\s+["']coffee-rails["'].*$/,''
gsub_file "Gemfile", /^gem\s+["']sass-rails["'].*$/,''
Yes, just modify your application template file to not include them.
take a look https://github.com/RailsApps/rails3-application-templates
to get a better idea, specifically mongoid templates

How can I use option "--bare" in Rails 3.1 for CoffeeScript?

Someone know how can I use this option in Rails 3.1?
Now CoffeScript puts a function with .call(this) on each file, but I want to remove this.
EDIT:
"Can't find variableā€ error with Rails 3.1 and Coffeescript" and "Pattern for CoffeeScript modules" have what I want. I'll change my global vars to use #global scope.
I'd recommend against doing this. See my answer at Pattern for CoffeeScript modules for some of the reasons why. ("Making your CoffeeScript code incompatible with out-of-the-box Rails 3.1" is yet another reason.) Better to just use
window.a = b
or even
#a = b
instead of a = b when you're trying to export something to global scope.
In previous versions of Rails 3.1, bare compilation was enabled. This was classified as a bug, and fixed in RC1.
So while I strongly encourage you not to do this, here's how to turn bare compilation back on: Add
Tilt::CoffeeScriptTemplate.default_bare = true
to your environment.rb.
I do recommend taking advantage of CoffeeScript's closures and following a CommonJS module patter. But sometimes, just sometimes, it is OK to want to use the --bare option. In my case, when rendering a Jasmine spec helper so I could keep things at the top level and also take advantage of the include Sprockets directive in said Jasmine specs.
To that end, I created the "sprockets-blackcoffee" gem, which you can learn about here. https://github.com/metaskills/sprockets-blackcoffee

Resources