How to use engine's gem in main application? - ruby-on-rails

I am making a rails plugin named signup.
In this plugin, I used bcrypt gem, Here is my engine.rb
module Signup
class Engine < ::Rails::Engine
isolate_namespace Signup
require 'rubygems'
require 'bcrypt'
end
end
My Gemfile:
source 'https://rubygems.org'
gemspec
gem 'bcrypt'
I follow this Rails Engine - Gems dependencies, how to load them into the application?
But In my demo application, when start rails server, then I got this error:
lib/signup/engine.rb:9:in `require': cannot load such file -- bcrypt (LoadError)
I want to load bcrypt gem in my demo application. If I add gem 'bcrypt' in my demo application's Gemfile and run bundle install, it does not show any kind of error, But I don't want to do it.
I want that gem 'bcrypt' will be load automatically in my demo application without adding this in Gemfile.

Related

Rails doesn't load actionform gem from Gemfile

My Rails 4.2 app fails to load some GitHub hosted gem (installed via bundler v1.8.2). I am using rvm 1.26.10 (master)
In my Gemfile,
gem 'simple_form', '~> 3.1.0'
gem 'actionform', :github => 'rails/actionform'
Bundler install them in different location:
$ bundle show simple_form
/Users/me/.rvm/gems/ruby-2.1.5#my_app/gems/simple_form-3.1.0
$ bundle show actionform
/Users/me/.rvm/gems/ruby-2.1.5#my_app/bundler/gems/actionform-4a858fecf4c2
Rails never load the actionform gem.
After inserting the line //= require action_form to my app/assets/javascript/application.js file, this error comes
Sprockets::FileNotFound at / couldn't find file 'action_form'
However, the action_form.js file exists in the gem file.
Moreover, when i try to reproduce the readme example, i got this error
NameError at /conferences/new uninitialized constant ActionForm
require 'bundler/setup' is in boot.rb
Any advise about this issue?
Thanks!
The problem is a mismatch between the gem name and the file inside the gem. Because the gem is named 'actionform', Bundler will try to require 'actionform', however the file is actually called action_form.
You can tell Bundler the right file name with this syntax:
gem 'actionform', :github => 'rails/actionform', :require => 'action_form'
Note that it is normal for gems from git sources to be installed into a different location than gems installed from gem servers. It has nothing to do with this problem.

Rails 4. Gem creating. Include rubyzip gem

I'm creating my own gem(mountable plugin), and I need to include an older version of rubyzip gem. The reason for using the old version is that I have some code templates written in Rails 3. In the gemfile I include the old version as follows:
gem 'rubyzip', "~> 0.9.9"
It installs successfully. In the lib file where I wrote my gem-module I've added:
require 'devise' # work nice.. haven't any error.
require 'zip/zip' # give me error that it is not found
If I try require zip doesn't work either. If I don't require the gem in my lib file, the error disappeared but in the gem's controller I don't have access to Zip constant anymore.

How do I use a RubyGem I made in Rails?

I recently developed a simple RubyGem and deployed it to RubyGems.org. How do I use this RubyGem within a Rails application? Here is the code to the Gem
module Gem1
class Message
def self.gem?
puts "Hi This is a RubyGem"
end
end
end
You can specify the gem in your Gemfile and do a bundle install.
Typically can be done
gem 'nokogiri'
in your Gemfile and run bundle install.
once the gem is installed you can access it across your application

no such file to load -- rack/openid

I am working on a rails gem which depends on rack/openid. But when I require it and fire up my application I get this error
no such file to load -- rack/openid
The gem is installed
$ gem list | grep openid
rack-openid (1.3.1, 1.2.0)
ruby-openid (2.1.8)
I've seen this question but it did NOT helped a lot.
Problem with require rack/openid in Rails 3 [native require work properly]
PS: I can require it from Irb just fine
It looks like you haven't added your Gem to the Gemfile, or you haven't added rack-openid as a dependency to your gem.
When Rails starts up, it uses bundler to set up the load path to match the Gemfile.lock file, so even a gem is installed locally you won't be able to require it if it isn't listed there.
Gemfile.lock is created by bundler based on the gems listed in Gemfile and their dependencies.
Make sure the gem you're working on specifies rack-openid as a dependency in its .gemspec, and then add gem 'my-gem-name' to your applications Gemfile (replace my-gem-name with whatever your gem is actually named).

Rails 3: define plugin gem dependency

I wrote a plugin that requires a gem as a dependency.
Where do I have to define this dependency?
I have tried to create a Gemfile in vendor/plugins/my_plugin/, but bundle install doesn‛t find this file.
Ok. I have solved.
1) Create a Gemfile in vendor/plugins/my_plugin like:
# Gemfile
source "http://rubygems.org"
gemspec
2) Create a gemspec file. In the folder vendor/plugins run this command:
bundle gem my_plugin
(Note this command ask you for overwrite some files. Check the files before answer: Y)
3) Open gemspec file in vendor/plugins/my_plugin/ and add before the keyword end:
s.add_dependency('will_paginate', '~> 3.0.pre2')
(In this example I have used will_paginate how required dipendency of my_plugin)
4) Now go in your rails app and edit Gemfile, add:
gem 'my_plugin', :path=>'vendor/plugins/my_plugin'
The path specified supposed that your plugin is already in vendor/plugins folder of your rails app.
Of course when deploy rails app you don't need anymore to specify :path argument.
5) Now in rails app root do:
bundle install
And dependency of my_plugin (will_paginate in this case) is installed.
Thank to Sinetris for initial input.
Create a Gemfile in your vendor/plugins/my_plugin/ like:
# Gemfile
source "http://rubygems.org"
gemspec
gem "your-dependency-gem-name"
note the gemspec directive.
Take a look at Using Bundler with Rubygem gemspecs for more information.
Sebtm's own answer is quite good, but it still didn't work as Tiago and orangechicken described. I had to add
require 'your-dependency-gem-name'
on top of lib/my_plugin.rb right before the engine of my_plugin is loaded.
See http://guides.rubyonrails.org/engines.html#other-gem-dependencies
Gemfile in the application folder.
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3
Reference for myself. If you're making a plugin that should work with Rails as a RailTie, use rails plugin my_plugin to create the gem instead of bundle gem my_plugin. It saves you (me) a lot of trouble.
Edit: When do you need the gem to work as a RailTie? Whenever you want to add rails generator, rake tasks or add your code to a Rails app (Called Rails Engine).
/Edit
$ rails plugin new my_plugin
then you add dependencies
Gem::Specification.new do |s|
#...
s.add_dependency "rails"
s.add_runtime_dependency "heroku"
s.add_development_dependency "sqlite3"
end
to include it in your rails app, use path: as described by #Sebtm
or release it to rubygems.
$ gem build my_plugin.gemspec
$ gem push my_plugin-0.7.0.gem #replace version number
#in Gemfile, as always
gem 'my_plugin'
or tag it on github. (use their release UI) and depend on it using
gem 'my_plugin', github: 'accountname/my_plugin', tag: 'v0.7.0'

Resources