I have implemented what edebill suggested in his answer to this question.
If I point to the gem in the usual way, with it installed in my environment
gem 'activerecord_datawarehouse'
rake -T does not show my rake tasks, but if I point directly to the gem source code, like
gem 'activerecord_datawarehouse', :path => "/home/acras/code/activerecord_datawarehouse"
It shows and rake tasks work perfectly.
What could I be missing here? I did double checked and the installed gem is the same that I have in the source code.
Fixed it on my end. In the gemspec, you need to include the rake tasks files as well, not just the lib files:
Instead of:
s.files = Dir['lib/**/*.rb']
Use:
s.files = Dir['lib/**/*.rb'] + Dir['tasks/*.rake']
Related
I'm trying to write a rake file to import data from csv, and I want to use smarter_csv gem. I have the gem installed globally (I don't want to add it to my Gemfile because it's a one-off task).
In my rake file I require 'smarter_csv' but when I run the task I get the following error:
rake aborted!
LoadError: cannot load such file -- smarter_csv
Every rake example I can find tells you to just require 'foo'. I can run the code manually in irb after requiring smarter_csv.
What am I missing?
(If it matters, I'm using rbenv on macOS Catalina)
If you do want to load a gem without having it in your Gemfile you can do the following in your rake task:
$: << '/Users/<user>/.rvm/rubies/ruby-
x.x.x/lib/ruby/gems/x.x.x/gems/smarter_csv-x.x.x/lib'
require 'smarter_csv'
I think the option to have the gem in your Gemfile with the require: false option is more suitable however. The gem won't be loaded until you specifically require it.
Rails uses bundler, and loads the gems mentioned in gemfile into memory. Doesn't look like you've any other option.
I had a similar use case, I tried putting it into a development group like so,
group :development do
gem 'smarter_csv'
end
We have a gem which runs via a Rake task. The task is defined in lib/tasks/<namespace>.rake. After reading Rake tasks inside gem not being found I confirmed that the .gemspec includes the file defining the task; there is also a railtie which should be including the tasks as suggested in including rake tasks in gems. And yet our Rails 4.1 application doesn't seem to load the Rake task.
What am I missing?
I just successfully tested your gem and I can see no problem with the rake tasks in it:
I added gem_fresh to the Gemfile and ran bundle, the gem installed
Immediately I could see the rake present in the list of rakes:
$ rake -T outdated
rake gem_fresh:outdated # outdated
Then I updated the railtie.rb file to use the load method to load the rake defined in lib/task/metrics.rake and searched the available rakes again:
# lib/gem_fresh/railtie.rb:
rake_tasks do
namespace :gem_fresh do
desc "outdated"
task :outdated => :environment do
GemFresh::Reporter.new.report
end
end
load "tasks/metrics.rake"
end
$ rake -T outdated
rake gem_fresh:outdated # outdated
rake metrics:outdated_gems # display outdated gem version metrics
So, I can see no problem with your gem. Both methods in railtie (inline rake as well as using the load method) seem to work OK. The only difference I noticed is that I tested this on rails 4.2 but I rather doubt that would make a difference.
Did you put the gem into your Gemfile? If I remove it from there, I indeed see no gem_fresh rakes defined.
The problem was not with the gem, but with the way it was included in the app.
In the Gemfile, this works and includes the rake task:
gem 'gem_fresh'
This works but doesn't include the rake task:
group :development do
gem 'gem_fresh'
end
This seems to be due to how Bundler requires gems in Rails apps. From the documentation:
By default, a Rails generated app calls Bundler.require(:default, Rails.env) in your application.rb, which links the groups in your Gemfile to the Rails environment.
If for some reason that Rails.env argument wasn't evaluating to include the :development group, which seems to be the case when I call rake -T, the gem wouldn't be Bundler.require-d, and the rake tasks wouldn't be loaded.
The fact that it isn't including the :development group seems to be an odd Bundler "gotcha", but at least now I know that moving it to the default group solves the issue and it's not a problem with the gem.
in my case, the project requiring the gem had a rake file, and the gem I was requiring had the same name when I changed the name of the rake file I could see the tasks in the project.
my_gem had lib/tasks/XXXX.rake and my_proj also had lib/tasks/XXXX.rake,
after I changed my_gem XXXX.rake to YYYY.rake I managed to list and use the tasks in YYYY.rake
I'm using twitter-bootstrap-rails gem. Last night I updated my gems, and tried using the icon-globe but all i got was an empty space.
here are my Gemfile:
gem 'jquery-rails'
gem 'therubyracer'
gem 'mongoid'
gem 'bson_ext'
gem 'bson'
gem 'mongo'
gem 'devise'
gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
I tried rake tmp:clearbut this didn't worked too
I just ran into this the other day as well. I have my Rails servers setup so that I can run multiple Rails apps on the same server under their own suburi. Apparently the asset-path helper in the bootstrap_and_overrides.css.less is not including the relative path for the sprites and instead points the background-image url to /assets instead of /suburi/assets.
Following what I found here: https://github.com/rails/rails/issues/3365 I was able to gather that I needed to do the folllowing when precompiling assets:
RAILS_RELATIVE_URL_ROOT="/suburi" rake assets:precompile
This sets the relative root within the environment when you precompile and everything then works as it should.
The thing that really threw me for a loop was that in development everything worked just fine. As soon as I pushed it to the production server the icons stopped showing up. I kept thinking there was an issue with my server or my code. All along it was just the asset-path helper not including the suburi when I precompiled my assets.
Just set your full suburi path in the RAILS_RELATIVE_URL_ROOT environment variable before running the precompile and it will work.
Update: You can set this variable in the config/application.rb file by adding
config.action_controller.relative_url_root = '/suburi'
This would be the best option as it would not require extra typing when deploying.
You can read about it here:
http://guides.rubyonrails.org/configuring.html#configuring-action-controller
did you try:
bundle exec rake assets:precompile
on your production environment?
If i define a few gems in my config/environments/test.rb file like this:
config.gem "rspec"
config.gem "rspec-rails"
config.gem "mocha"
and then run 'rake gems:install RAILS_ENV=test'
I get the following error:
Missing these required gems:
mocha
Run `rake gems:install` to install the missing gems.
however if I run rake gems:install like it says it will continue to recurse like this forever.
How do I actually get the gems to install using rake (not gem install)?
thanks!
I wonder, is there a reference to something from the mocha gem in your rake file or environment.rb file? I've seen issues like this before and it presents as this type of problem.
Try installing mocha 'manually' with...
gem install mocha
Then see if you can run rake gems:install.
I ran into this problem as well, and followed the directions here to resolve it. Specifically, deleting and regenerating lib/tasks/rspec.rake is pretty crucial. Also, adding
:lib => false
to
config.gem "rspec", :lib => false, :version => ">= 1.2.0"
helped.
I found this was a GEM_PATH issue. Basically, rails can't find the gems you've installed and even though they're there, they're completely invisible. A bit weird, but hey.
On dreamhost I had to configure the line:
ENV['GEM_PATH'] = '/home/<my_account>/.gems:/usr/lib/ruby/gems/1.8/gems'
in config/environment.rb
but on my dev box this doesn't work for me and has to be removed entirely.
YMMV but I'd suggest that's a good place to start looking.
Which platform are you using? If it it window then
Execute Below command and its works
gem install mocha --platform=mswin32
I have the following gems defined in my environment.rb file:
config.gem "authlogic"
config.gem "paperclip"
config.gem "pauldix-feedzirra", :lib => "feedzirra", :source => "http://gems.github.com"
config.gem 'whenever', :lib => false, :source => 'http://gemcutter.org/'
I have them installed on my local computer and everything is working well.
Since I am working on a shared-server (DreamHost), I need to unpack those gems to get them to work (can't install them as I did on my own computer to get them to work).
Before uploading, I ran the following on my local machine:
rake gems:unpack
This created the following folders in /vender/gems:
authlogic-2.1.3, paperclip-2.3.1.1, pauldix-feedzirra-0.0.18, whenever-0.4.1
So it looks like they're all there.
When I run rake db:migrate on the server, though, I get these following error:
Missing these required gems:
pauldix-feedzirra
For some reason, the feedzirra unpacked gem is not detected. Could anybody offer a clue as to why this is happening and a potential solution?
Thanks!
EDIT: Thanks, but the code to put in environment.rb doesn't work, and bundler won't install properly on my server. Any other suggestions?
This isn't exactly an answer, but since I could never get config.gem to work properly, I recommend using Bundler whenever I can. It just works and it handles interdependencies between gems well. It also replaces config.gem in Rails 3 from what I understand.
I notice two things about feedzirra: first, it depends on 3 other gems, and at least one of those build native extensions. And I'm going to call it "feedzirra" - I'm not a fan of github's ill-considered autopackaging fiasco.
If it were only the former, then rake gems:unpack:dependencies would do the trick.
However, at least curb (which feedzirra depends on) is building extensions, and those can't simply be unpacked. You could either get Dreamhost to install them (good luck) or install them into your user's local gem directory.
To do that, you'll need to update your .gemrc and be sure that it includes a line like:
:user_install: true
Then rake gems:install
Unless your deployment environment won't let you build executables, in which case you'll need to shell out for a less restricted package - I know for a fact that Dreamhost does provide packages that will allow for extension-gems.
(And there's the separate issue of libcurl being deployed...)
Try Following.put this code in envoirment.rb
config.load_paths += Dir["#{RAILS_ROOT}/vendor/gems/**"].map do |dir|
File.directory?(lib = "#{dir}/lib") ? lib : dir
end
Don't know if my hints are useful, because feedzirra is compiled extension (against CURL i think). Better solution is to normally install feedzirra gem (it will compile itself) on your server.
You have not installed (unpacked) feedzirra gem, but pauldix-feedzirra. Probably you need feedzirra unpacked too.
Try to add
config.gem feedzirra
into environment.rb and run locally
rake gems:install
rake gems:unpack
It looks like feedzirra unpacked gem is missing in /vendor/plugins. Look if feedzirra will be copied there after those commands...