How to provide an erb-file as an opal-template in rails? - ruby-on-rails

I have a rails-application (rails 5.2) with opal (0.11.1) running.
I would like to use erb-templates on client site. I followed the steps described in the official opal-docs (http://opalrb.com/docs/guides/v0.11.1/templates.html), but requiring the opalerb-file fails. sprocket claims the file could not be found:
couldn't find file 'views/user' with type 'application/javascript'
File views/user.opalerb exists.
It seems that sprockets does not handle / recognize opalerb-files, although I don't find a hint that things have to be configured.
How can I make opal with sprockets find and compile this file?

I ran into the same problem (Rails 5.2.2, Opal 0.11.4). The "opalerb" extension was not being correctly registered with Tilt and Sprockets. I addressed this locally by creating a file called "opal.rb" in config/initializers with one line:
require 'opal/sprockets/erb'
Alternatively, I was also able to fix the problem by explicitly including "opal-sprockets" in my Gemfile, pulling directly from the master branch in Github:
gem 'opal-sprockets', git: 'https://github.com/opal/opal-sprockets.git'
The final option seems to be to include "opal-haml" (even if you don't intend to use HAML). This seems to trigger initialization, although I haven't investigated why.
gem 'opal-haml'

Related

How do I import ruby gems assets to project? [duplicate]

I'm trying to wrap the bootstrap-sass gem inside another gem (let's call it my-engine). Along the way, I'm building a small Rails application to test things out. As a first step, I wanted to make sure I could get bootstrap-sass working directly in my Rails application. The Gemfile for the Rails app looks like this:
gem 'bootstrap-sass', '3.3.1.0'
gem 'my-engine, path: "~/dev/my-engine"
This works fine. The bootstrap assets are loaded into my Rails application and everything looks good. Now, I want to take bootstrap-sass out of my Rails app and let it load through my-engine. So, my Rails application Gemfile now looks like:
gem 'my-engine, path: "~/dev/my-engine"
The .gemspec for my-engine has:
spec.add_runtime_dependency 'bootstrap-sass', '3.3.1.0'
I can re-bundle the my-engine gem with no problems. I can re-bundle the Rails application with no problems. However, when I refresh the page of the Rails app, I get the following error:
File to import not found or unreadable: bootstrap-sprockets.
That break occurs when sprockets is trying to build the application.css file. Sometimes this will pass and I'll get a different error about missing the bootstrap.js javascript file when the application.js is being built.
Why is this happening? I'm wondering if it has something to with the fact that I'm developing the gems locally and haven't published them, although I'm not sure why that would affect bootstrap-sass which is published. I'm using bundler 1.5.3.
Make sure 'bootstrap-sass' is required in your engine. One sensible place to do this is in your lib/my-engine.rb file:
require 'bootstrap-sass'
Adding the bootstrap-sass gem as a runtime dependency in the .gemspec isn't enough when you're trying to wrap gems.
As you want to use more and more scss/js/coffeescript libraries, you may want to consider moving to bower vs gemfiles as the source for bootstrap-sass-official. We use bower-rails for rake tasks and auto-configuration. It's a really lite config/rake task layer over standard bower.
Addressing your answer, bootstrap problems via the gem was one of the reasons I switched our engine over to just bower assets. We now import bootstrap-sass-official and have full control, note however that for sass files you will need to import the longer path to the source file, i.e. in our engine _application.scss:
# our custom variable overrides
#import 'overrides/variables';
#import 'bootstrap-sass-official/assets/stylesheets/bootstrap-sprockets';
#import 'bootstrap-sass-official/assets/stylesheets/bootstrap';
NOTE: if you want your app sass variables to override engine and sass variables, make sure your engine has _application.scss not application.scss, the leading underscore is critical for variable context/scope.
Thinking ahead, you may need to ignore bower transitive dependencies as we did.
(i.e. some dependencies may use 'bootstrap' while others use 'bootstrap-sass-official' etc)
We use it like this in our .bowerrc such as the following:
{
"ignoredDependencies": [
"bootstrap",
"bootstrap-sass",
"bootstrap-sass-official"
]
}
In conclusion
We have been using this for several months with success. bower-rails will install the dependencies in /vendor/assets and if referenced in your engine, you won't need to reference them at all in your application project. It has been fast and easy to maintain/add/update libraries and know exactly how files are included.

Sprockets can't find TinyMCE from gem

I'm using Spree 0.70.3, Rails 3.1.3, and a custom fork of the Spree Editor plugin. (N.B. the relevant code for this fork hasn't changed from master.)
When I request a page which should get TinyMCE on a text area, I see this error in my development log:
Error compiling asset admin/all.js:
Sprockets::FileNotFound: couldn't find file 'tinymce-jquery'
(in <gemset-path>/bundler/gems/spree_editor-48cdfcfaf7bd/app/assets/javascripts/admin/spree_editor.js:9)
The line in question looks like this:
//= require tinymce-jquery
The editor depends on the tinymce-rails gem, which has a few candidates for the file in question:
assets/vendor/tinymce/jquery-tinymce.js
assets/precompiled/tinymce/jquery.tinymce.js
assets/precompiled/tinymce/tiny_mce_jquery.js
assets/integration/tinymce-jquery.js
The README for that gem says the require line quoted above is correct, however. None of the gem's paths are in Sprockets' search path as shown by putting Rails.application.config.assets.paths into the console.
Should I be adding the gem's path to the Sprockets search path somehow, or is this something the gem isn't doing correctly?
I assume you cannot precompile the gem, I had a similar issue so what i did was to download the javascript plugin from the site manually and copied the directories into my rails app. or i also tried to use a lower version of tinymce-rails gem and it worked.

Railtie initialization of gem dependencies

I am trying to understand the initialization process for Rails 3 plugins. I have a plugin packaged as a gem that I am including in my Gemfile for my project.
In turn, that gem specifies in its gemspec that it depends Sunspot::Rails. The gem itself gets pulled in and I can access its classes, but it doesn't appear that the railtie initialization code gets run.
However, if I add a line in my project's Gemfile for sunspot_rails directly, then the initialization code is run.
Does anyone know of a way to have my gem/plugin run the initialize code in its dependencies without having to include all of them directly in my project?
Thanks in advance.
After reading the above article and responses, I realized that I was requiring sunspot/rails instead of sunspot_rails. It's necessary to require sunspot_rails because that in turn requires the railtie (which is not automatically required as part of having the gem listed in the gemspec).
Here is a link to a very good article on the difference between gemspec and Gemfile that helped explain it all.

How to freeze a gem that doesn't want to freeze? (rack)

When I try to deploy my rails app to my shared hosting (dreamhost) I get this error:
can't activate rack (~> 1.0.1, runtime) for [], already activated rack-1.0.0 for []
So I want to freeze the rack gem in my dev environment, and add it to the project, but even though I have this in my config/environment.rb:
config.gem 'rack'
Doing a rake gems:unpack:dependencies doesn't freeze the gem.
ajmbp:trunk ajl$ rake gems:unpack:dependencies
(in /Users/ajl/dev/site/trunk)
ajmbp:trunk ajl$ ll vendor/gems/
.svn/ haml-2.2.14/ net-ssh-2.0.16/
I tried adding another gem to the config.gem just to test (RedCloth) and that does work as expected, but no luck with rack.
Any ideas?
Thanks!
Have you checked your environment variables (RUBY_GEMS I think it is, or something like that)?
Also, your environment.rb and (if you use fastcgi) your .fcgi.
Make sure the paths are ordered from highest priority to lowest.
See this article.
Any gem that is loaded in your Rakefile (e.g. metric_fu, vlad, etc) is considered to be a ‘framework gem’ by Rails, and such gems are not unpacked. Given that the vendor/gems directory is not yet in the load path when the Rakefile is loading, this is probably a good idea.
In other words, if you have a library that provides Rake tasks, or is otherwise necessary for your .rake files to be valid, don’t expect “config.gem” and friends to handle it for you.
For Dreamhost see their own documentation on installing your own gems. Or see this excellent article on how to load your own packages and gems (be warned it's not for the faint of heart).
But all my reading is telling me that you may still run into trouble because of the way Passenger may be using the Rack version installed by Dreamhost.
Good Luck.

Why is a gem specification needed and why do some gem creators not provide them?

I've noticed some ruby gems have .specification files and others don't.
If they're important, why are you not required (by whatever tool builds them) to provide one when you attempt to create your gem?
Many gems are configured using hoe or newgem, which generate a specification only on the building of the gem. The spec is treated as a temporary bit of code only used to create a gem so it is typically not packaged up. There are rake tasks for both of these tools that will generate a spec file though.
Some times the gem spec is in the Rakefile rather than a separate gemspec.

Resources