One good thing in recent Ruby on Rails are Gem Dependencies! However.. they are very frustrating when you need gems in your plugins. This gives you a Catch 22..
The rake task to install gems:
1 | sudo rake gems:install # Use sudo! See notes below |
gives you an error like:
1 2 3 4 5 6 | sudo rake gems:install (in /your/rails/dir) rake aborted! no such file to load -- gemname (See full trace by running task with --trace) |
This is because the Rails::Initializer block in your environment.rb will try to load all plugins.. and those plugins need gems.. and those are the gems you’ve put in the block.. hmmmm.. nasty.
However. There is a hack to still be able to use the rake gems:install command.. and I’m going to give it to you
In your environment.rb, inside the Rails::Initializer block, use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if $rails_gem_installer # If we're in a rake gems:install or similar task (this is set by Rails, the $-sign makes it a global variable) # We stop the initializer to load the files from the /config/initializers dir. This is to disable the usage of plugins or gems in that code. puts 'Disabling the application initializers (rails_gem_installer == true)' class Rails::Initializer def load_application_initializers; end end # Next, do *only* load the needed plugins that are not dependent on gems. For example exception_notification since that one is used in application.rb. puts 'Not loading all plugins (rails_gem_installer == true)' config.plugins = [:exception_notification] else # Otherwise, when we're just loading the environment.. load everything in the right order. So this is YOUR config.plugins = [something]! config.plugins = [:translatable_columns, :userstamp, :all] end |
Now, using this code you will be able to install gems using the rails tasks even if though the gems are needed by plugins in your application. Whoeepie!
Further notes:
Have you ever used the rake gems:install task without sudo? Then your gems get installed in your homedir (~/.gem/ruby/1.8/). To uninstall them the regular command will not work (at least..with me).. you will have to use
1 | gem uninstall -i ~/.gem/ruby/1.8/ [yourgemname] |
[edit]
You can also disable observers if you have them since they may load models depending on plugins depending on gems depending on the rake gems:install task
1 2 3 | if not $rails_gem_installer config.active_record.observers = [:user_observer] end |
1 2 3 4 5 | # For my own notes: # You can use ENV['_'], __FILE__ or $0. # The latter gives you the most correct results (also when using sudo). # So you can also use something like: config.active_record.observers = [:user_observer] unless $0 =~ /bin\/rake$/ |
Subscribe to our RSS feed
1 Response to “Plugin gem dependencies in your environment.rb”