How do you configure Webpacker to work with a Rails engine? - ruby-on-rails

I'm trying to configure Webpacker to work with a Rails engine. I'm specifically interested in how people are setting up their webpacker.yml to move their engine's assets to the main app's /public/ folder.
First, I've followed the Webpacker instructions for Using in Rails Engines. However, Step 7 stumps me. The idea here is to set the public_root_path to your app's /public folder. I could obviously hard-code the correct path on my personal machine, but that's unsustainable for production or other devs. I can't rely on ERB to have a dynamic path either.
I also tried the second option of Step 7 which involves middleware, but it didn't seem to have any effect.
I ultimately want to be able to import JavaScript from the engine into the main app from the app/javascript/packs/application.js file. How do I get Webpacker to find the files from the engine without hard-coding the path?

Related

Google App Engine asset pipeline standard environment

Is it possible to use the Ruby on Rails asset pipeline in Google App Engine when the app is deployed to a standard (not flexible) environment? I know precompilation happens when deploying to a flexible environment but I can't get it to work for the standard environment.
The problem is that the default configuration of app.yaml is preventing a crucial file being uploaded to GAE.
Specifically the skip_files section has some defaults that are preventing all dot-files from being uploaded, including the sprockets manifest file: /public/assets/.sprockets-manifest-5y483543959430890.json. Without this file, Rails assumes that assets haven't been precompiled.
You need to override the default skip_files configuration with something that doesn't prevent the sprockets manifest from being uploaded, but still blocks things like .git/*.
This is working for me now, but I'm sure it could be further refined:
skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\.git/.*$
It is possible. Check the full documentation here: Ruby in the App Engine Standard Environment.
Note that the Ruby Standard Environment it's on Beta stage, so keep in mind that it might get changed overtime.
Instead of using the skip_files section of app.yaml, you can instead create a .gcloudignore file and add a line for /public/assets. If this directory is not present, the docs say:
The Ruby runtime executes rake assets:precompile during deployment to generate static assets and sets the RAILS_SERVE_STATIC_FILES environment variable to enable static file serving in production.

Changing Directory Names in a Rails App

I'm relatively new to Rails as well as using PassengerPhusion. I am running an Ubuntu server on Azure, and have the demo app that Passenger provides working fine. I've even changed the text on the homepage.
My question is this:
In my directory, the file directory's name for the app is passenger-ruby-rails-demo and while I am experimenting, i am changing the name of the directory to something like passenger-ruby-rails-demo-test and it returns an error message when viewing fleetpro.cloudapp.net.
I've tried looking through files trying to figure out how this is routed but haven't had any luck. Is there a file within the Rails installation that is telling Passenger to be inside the specific passenger-ruby-rails-demo directory? Pretty newbish question, but it is really bothering me!
I'm not sure about how the naming convention works in regards to the root directory name of your app "passenger-ruby-rails-demo", but I believe the name of that directory is important to running your Rails app, and might have to do something with the name of the module in your config/application.rb file which is named after your Rails app.
There is a solution though: use gem rename.
Add gem rename to your Gemfile and run bundle install.
Then in your app's root directory, run this:
rails g rename:app_to New-Name
This will basically "clone" the app with your new name. You may have to check to make sure all your config files are present afterwards, but from my experience using it, it was a quick breeze. You will most definitely have to push the new renamed app back to git or Azure.
EDIT
As an example I renamed a Rails app to show what you could expect from the output after running the command:
The Rails app's name isn't the problem, it's the PassengerAppRoot switch you'll be using:
PassengerAppRoot /path/to/your/app
Rails doesn't actually care which folder it's put into, so renaming Rails won't fix your problem.
Renaming Rails only changes the internal class references within your application (things like Rails.application.name which have very subtle implications for your app).
In your Azure server, you'll need to locate either your .htaccess / httpd.conf / nginx.conf file, and change the PassengerAppRoot to your new path. This should resolve the issue.

Where should I save standalone non-ruby scripts in my Rails app?

This is a simple question, but I haven't been able to find an exact answer to it anywhere.
I have a standalone Python script which I am using in my Rails app. What is the appropriate folder I should save it in according to convention, so that I can push it to production (currently running it from my computer's desktop)? I think the answer is lib/assets but I want to make sure.
I don't think there is an exact answer for this question.
If it is a ruby script, it is usually placed in lib or bin.
From the rails folder descriptions in Getting Started with Rails guide:
bin/ Contains the rails script that starts your app and can contain
other scripts you use to setup, deploy or run your application.
lib/ Extended modules for your application.
You could put it in lib/assets folder as it reflects your understanding that it is an external asset used in the system.

Middleman images/fonts from bower

I'm wondering what the best way is to use images & fonts from bower packages with middleman. As an example, I'm trying to add the slick.js carousel to my project. It's on bower and includes css, js, images, and fonts in the bower code.
With middleman, I have things set up where I've added the bower_components directory to the path for sprockets and compass, so the scss and js files are getting compiled correctly and working fine.
But the images and fonts aren't getting put anywhere where they'll be used. The slick.js library uses scss and is set up to use the compass image-url and font-url functions if they exist, meaning I need to somehow get the assets from the bower_components directory to be served from the same place as all my own images and fonts, and in a way that works in both the development middleman server mode as well as when running build.
How do I do this?
Obviously possible solutions are just to vendorize the slick.js library directly into my code or include it from the cdn where it's already hosted and not worry about not having it compiled into my single css and js files. Either could work fine but I'm wondering about the general case, surely this is common scenario for anyone using bower and middleman.
I figured it out - I thought compass was for requiring scss files and sprockets was just for the js, but middleman also uses sprockets (the middleman-sprockets library) for copying arbitrary static assets.
It's a bit manual and verbose (if there were a lot more files middleman suggests writing a script to auto-discover them by file extension types and import them) but my solution is to include the following in the config.rb file:
# set local vars I'll need to access later
images_dir = 'images'
set :images_dir, images_dir
# ... other config
sprockets.import_asset('slick-carousel/slick/ajax-loader.gif') {|p| "#{images_dir}/ajax-loader.gif"}
I use grunt, but it's the same issue. Generally you have the following options:
-Commit what you need in the bower_components directory right in to source control and reference your resources from there (somewhat recommended especially if something external is down when you are doing a build), or if you don't like exposing bower_components in URLs, create a route that directs to your bower_components folder
-Copy components on build/middleman script execution to a specified path. There will be no resources to check in for this option, you just choose a destination to reference in your code and have middleman copy your components out there.

Rails Asset Pipeline Path Issue

I have a Rails app running on a shared web host under a folder in my root direction called 'mintrus-ror/'. There is a symbolic link 'public_html/' that points to 'mintrus-ror/public/'. My Rails app loads in the browser but the stylesheets don't load. Looking at the rendered source I noticed the assets path it is using is '/mintrus-ror/assets/application.css'. I am trying to figure out how to change it so it does not include the 'mintrus-ror/' directory in the assets path. Any ideas?
You can try playing with the config.assets.manifest configuration option in config/environments/production.rb. There are other variables that influence this, one being the web server configuration. I've got no recent (less than six years ago) experience with shared hosts, but I've read that on some systems, you can edit the .htaccess file.
I also presume that you're running in production mode and have previously compiled the assets with rake assets:precompile during your deployment step. Capistrano does this automatically when the asset pipeline integration is turned on.
The Rails Guide on the Asset Pipeline may be useful.
Your best bet may be to host on Heroku. It's free for small sites and a lot less hassle.

Resources