Rails 5 Engine - Gems dependencies, how to load them into the application? - ruby-on-rails

I'm having the same issue as this link but none of the solutions here are working. A couple hours of searching and of course reading the main Rails Engine docs have all turned up the same strategies as the SO link. I'm wondering if there is something different about the way Rails 5 handles dependencies.
I have this dependency in my gemspec:
s.add_development_dependency "pg_search"
I have this line in my engine.rb file:
require "pg_search"
When I run rails s from within the engine's directory, everything loads fine. When I mount the engine and try to start the server from the rails app directory, I get this error:
/home/andy/.rvm/gems/ruby-2.3.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require': cannot load such file -- pg_search (LoadError).
Finally, if I add the pg_search gem to the Gemfile of the app that is mounting the engine, the rails server starts. This of course isn't a solution, I'm just trying to provide complete information.

This happened because I used 'add_development_dependency' rather than 'add_dependency'.

Related

Installing Rails engine gem - path not found

I've been following the guide around rails engines here: http://guides.rubyonrails.org/engines.html and have created the example engine blorgh and also have a barebones rails app.
I generated my engine following this command:
rails plugin new blorgh --mountable
And I can confirm that I have: /lib/blorgh.rb in my engine. Now going by the guides it states you simply need to add the following to your main app:
gem 'blorgh', path: 'engines/blorgh'. Now in my main rails app when I try to do bundle install I get:
The path /Users/home/projects/unicorn/engines/blorgh does not exist.
I'm pretty sure I'm missing something basic here.
The path /Users/home/projects/unicorn/engines/blorgh does not exist
Because Rails application going to Search ‘engines’ folder that's stores the engines (even if you just have one!) in your case /engines/blorgh not found any engine.
The path option in Gemfile is for using gem that is on the path specified.
And it have to be the directory where the unpacked gem is located.
In your case it looked to engines/blorgh relative to your working directory. Which is on /Users/home/projects/unicorn/engines/blorgh.
And /Users/home/projects/unicorn/engines/blorgh is simply doesn't exists.
To fix it, make sure you put the engine on /Users/home/projects/unicorn/engines/blorgh

Vendored assets in a Rails engine not loading when used as a Gem

Im writing an isolated Rails Engine which has it's own javascript in app/assets which in turn loads a bunch of dependencies that are kept in the engine's vendor/assets.
I've been using the dummy app in the test folder for development and everything has worked as I expect.
If I package the engine up as a gem and install it into a separate rails app, when I try to access the engine in a browser I get the Sprockets::FileNotFound exception couldn't find file.
If I fire up the console and have a look at Rails.application.config.assets.paths it includes mygem/app/assets but not mygem/vendor/assets.
This is where it gets weird. If I change the rails app's Gemfile and load the engine directly from a path, I don't have these problems. I can view my engine in browser without any Sprockets issues. Loading up the console and looking at Rails.application.config.assets.paths shows both path/to/mygem/app/assets AND path/to/mygem/vendor/assets.
I don't get this. Why would I get different behaviour if the engine is being loaded as a packaged gem or directly from a path?
Answering my own question. School-boy error, nothing to do with the asset pipeline, everything to do with adding the vendor path to the gemspec configuration.
s.files = Dir['{app,config,db,lib,vendor}/**/*', 'README.md', 'LICENSE.md']

Use gem as library in Rails 4

Is there a way to use a gem as a library in Rails 4?
I have tried putting in a gem folder after cloning into lib folder but this doesn't seem to be working
You can set local path to gem in your Gemfile if I clearly understood the problem.
# Gemfile
gem 'my_perfect_gem', path: './path/to/my_perfect_gem'
I think it's better to set local gem location only in development and test environments, so wrap this line in a group. Unfortunately you should restart your rails server any time you've updated the gem.
May be there is a better approach such as using your gem as a part of application in lib folder – Auto-loading lib files in Rails 4

Rails locations - where do I locate a gem

In Rails -
Where should I locate Gems? I downloaded bootstrap and it's working, as well as a sample Rails app, separately, but I want them to work together. There is a bootstrapped rails gem (http://rubygems.org/gems/bootstrapped-rails) which I downloaded, but I'm unsure as to where I should locate it. Under models?
And how do I make sure I am referring to it? I need to add something in controller as well?
Again, more an answer to the question in the title than to what was intended by the questioner but you can use
bundle show <gemname>
To locate the directory where a gem is installed.
As Dfr mentioned: https://github.com/seyhunak/twitter-bootstrap-rails
Twitter bootstrap isn't anything more than (mostly) a collection of css/js/image files.
Add this to your gemfile
gem "twitter-bootstrap-rails"
run
bundle install
run for simple css
rails generate bootstrap:install static
It should place relevant js and css files into your application.js and application.css files accordingly. (Read more about asset pipeline)
To get you started, in the gem's link under section - "Generating layouts and views", you can see rake tasks to generate sample layouts.
e.g.
rails g bootstrap:layout application fixed
You should now have a twitter-bootstraped application.html.erb file under views/layouts.
To answer the question in the title, you can locate your gems by running gem env in the console. That will give you the specific information about your "RubyGems Environment:" When you run gem install some_gem_name it will add this gem to your system.
However, what it sounds like your trying to do is add a gem to your app. If this is the case you add gems to a rails application's Gemfile.
So using your example, you'd locate your Gemfile and add the following:
gem "bootstrapped-rails", "~> 2.0.8.5"
Once that's done, you run bundle install in your terminal.
I find that a good resource for basic rails information can be found here: http://guides.rubyonrails.org/getting_started.html
The tutorial is short and it will give you a great starting point.

Using another gem's javascript in a Rails Engine

I'm trying to create a rails engine that will use javascript from a gem dependency that I have added to the engine. However I keep getting "couldn't find file 'fullcalendar'" when I put the following line in my application.js for my engine:
//= require fullcalendar
This line is loading the javascript from the gem dependency into the rails engine.
This same line will work when the gem is installed on a normal rails app (not engine). What am I missing here? Can an engine load javascript from another engine/gem?
UPDATE:
Researching on my own the issue could be that sprockets only looks for javascript inside the engine. The gem dependency is installed into vendor/cache of the parent app NOT the engine therefore //require fullcalendar fails because it is looking within the engine and the javascript for fullcalendar is in the parent application.
What confuses me is that if I include fullcalendar in the parent application gemfile explicitly than I am able to access it in the engine. This doesn't make sense to me. In both instances the full calendar gems javascript is in the parent app, but the behavior is different. Including the gem in two places is unappelaing to me and doesn't seem like a proper solution. Any thoughts?
I had some trouble with this in the past.
App > Engine 1 > Engine 2
Engine 1 and Engine 2 worked fine on the app. I created a dummy app in Engine 1 to test Engine 2's functionality, also worked fine. But when I put Engine 1 in App it didn't work at all.
My big mistake here was only including Engine 1 in App, Engine 2 has to be included as well!
Inheritance does not work for Gemfiles, just dependencies.
A little late to the party, but I recently had the same issue. Here is my post to describe it the solution http://grantrklinsing.com/articles/2013/01/10/adding-3rd-party-assets-to-a-rails-gem.html. Hope it helps if you didn't get it working.
Does the fullcanendar gem you're using come with an installer? Some gems come with installers for model migrations, assets, views, config files, etc.
If you're referring to the fullcalendar-rails gem it comes with an installer that you have to run using the
rails g fullcalendar:install command.
If the gem doesn't come with an installer you can manaully clone the gem to your local machine by running git clone gem_github_repository_url_here.git which will enable you to navigate the gem's folders and you'll have direct access to any of the assets that the gem uses which can be copied over to your rails application.
If your Engine has a dependency added through the gemspec file, you need to require this dependency directly before Engine initialization.
app/lib/app/engine.rb
require 'fullcalendar-rails'
module App
class Engine < ::Rails::Engine
isolate_namespace App
end
end

Resources