Rails 4. Gem creating. Include rubyzip gem - ruby-on-rails

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.

Related

How to use engine's gem in main application?

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.

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 active admin deployment : couldn't find file 'jquery-ui'

when trying to deploy with capistrano, when capistrano use command
bundle exec rake
RAILS_ENV=production
RAILS_GROUPS=assets
assets:precompile
I have this error
couldn't find file 'jquery-ui'
(in /home/umbrosus/.rvm/gems/ruby-1.9.3-p392#gancxadebebi/gems/activeadmin-0.5.1/app/assets/javascripts/active_admin/base.js:2)
Before it was working well, but I tried to update to 0.6 and then I started to have this error. I came back to 0.5.1 and the error is still there. Do I do something bad ?
thanks
The "jquery-rails" gem recently removed jQuery UI.
https://github.com/rails/jquery-rails/commit/2fdcdb2633cbc6426d412c050200fc31d14b9a3b
They recommend using the jquery-ui-rails gem.
There is an active pull request (as of this writing) to add that gem as a dependency. However, the developers of ActiveAdmin have stated that they are "locking it down until we officially drop support for Rails 3.0". The version they are locked to is jquery-rails < 3.0.0.
In the meantime, just modify your Gemfile:
gem "jquery-ui-rails" Not recommended, see #Kevin's comment below
Or you can downgrade your version of jquery-rails:
gem "jquery-rails", "< 3.0.0"
Or you can pull from their Github master branch. They have applied a temporary fix.
gem "activeadmin", github: "gregbell/active_admin"
Well, there is no need to downgrade jquery-rails to 2.3.0 or specify a GitHub branch. Just use jquery-ui-rails. To workaround the file name differences:
Simply create app/assets/javascripts/jquery-ui.js
//= require jquery.ui.all
Create app/assets/stylesheets/jquery-ui.css
/*
*= require jquery.ui.all
*/
These load the correct files to satisfy ActiveAdmin
Though the Pull request has been merged into AA by now, you will still have this problem if you work with the latest release of AA. I don't like to force JQuery-rails down to version 2.3.0 so here's an alternative solution to the problem:
In the active_admin.js file replace
//= require active_admin/base
with
//= require jquery
//= require jquery_ujs
//= require jquery.ui.core
//= require jquery.ui.widget
//= require jquery.ui.datepicker
//= require active_admin/application
kudos to Fred for providing that solution here.
Downgrading "jquery-rails" to "2.3.0" fixed this issue for me as well.
In my case, the jquery issue was due to a gem I was using. I wasn't using jquery directly, so adding app/assets/js/jquery-ui.js to my project didn't help.
Adding gem "jquery-rails", "< 3.0.0" to my gemfile fixed it, but I got an issue with turbolinks immediately after that, which is easy enough to fix...
My final gemfile:
# Temporary fix for jquery issue
gem "jquery-rails", "< 3.0.0"
gem 'turbolinks'
... easy peasy
I know this is already solved. But I want to give one more solution to this that worked for me.
I am running Rails 4.0.8 when having this issue.
I simply remove explicit version number for jquery-rails gem jquery-ui-rails gem.
Mine looks like this essentially:
# js
gem 'jquery-ui-rails'
gem 'jquery-rails'
# rails admin
gem 'rails_admin'
Gemfile.lock kinda figured out the correct version for all three gems automatically.

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