0

I'm making a gem for Rails. I need access to the ApplicationController because I'll toy with it. Absolutely nothing online gives information about what to do with gemspec and then somehow manage to get Rails accessible in my gem.

I imagine the goal is eventually to be able to talk to Rails like:

module Rails
  module ActionController
    #code
  end
end
David
  • 7,028
  • 10
  • 48
  • 95
  • 2
    If you are developing a gem exclusively for Rails I strongly recommend you generate the initial scaffold using `rails plugin new gem_name`. There's a ton of info on developing rails plugins. – ichigolas Jul 17 '14 at 21:26
  • @nicooga Ok. I imagine once I get it working that way I should focus on making the plugin a gem, right? – David Jul 17 '14 at 21:28
  • The plugin generator generates the gemspec :). – ichigolas Jul 17 '14 at 21:29
  • Ah, so it is as simple as getting it up on rubygems? – David Jul 17 '14 at 21:29
  • Yes, it also generates a Rakefile with some useful commands like `release` – ichigolas Jul 17 '14 at 21:30
  • Thanks, @nicooga. I'll check it out. Hopefully I could go from there. If you include this information in an Answer below, I'll gladly mark it as the answer :). – David Jul 17 '14 at 21:31

1 Answers1

2

If you are developing a gem exclusively for Rails I strongly recommend you generate the initial scaffold using rails plugin new gem_name. There's a ton of info on developing rails plugins.

The initial structure generated looks like this:

gem_name
  gem_name.gemspec
  lib/
    gem_name.rb
    gem_name/
      version.rb
      engine.rb # if generated using --mountable

The whole rails environment becomes available [edit: after your gem is loaded] so extending ApplicationController can be done like this:

# lib/gem_name.rb
require 'gem_name/controller_extensions'
module GemName
end

# lib/gem_name/controller_extensions.rb
module GemName::ControllerExtensions
  # bleh
end

# dummy_application/app/application_controller.rb
class ApplicationController < ActionController::Base
  include GemName::ControllerExtensions
end

Look at this question.

Community
  • 1
  • 1
ichigolas
  • 7,595
  • 27
  • 50
  • So, if I had access to ApplicationController, my gem would basically be finished. However, with what you wrote above and my code in the `module GemName::ControllerExtensions` (I appropriately swapped out GemName with my own). I get an error though when I try to run `rails s` in the dummy app within the test folder supplied. The error is: `/Path/To/Gem/lib/gem/controller_extension.rb:15:in ': uninitialized constant ApplicationController (NameError)`. – David Jul 17 '14 at 22:38
  • I see. Is there anyway where I wouldn't have to include it like you did? I've tested, and confirmed that what you have there does indeed work :) – David Jul 18 '14 at 05:53