0

I'm working on an infrastructure for Rails application and I'm trying to take something out of someones existing project. I am sort of new to rails but I read the guides on plugins and engines ect..

So I have a gemified Engine, containing some module. I have a model say SharedPost trying to extend said module and I'm getting the uninitialized constant error

uninitialized constant Stake::SharedPost

Here's some of what my engine looks like:

#file: lib/stake/shared_post.rb
module Stake
  module SharedPost
    ...
  end
end

#file: lib/stake/engine.rb    
module Stake
  class Engine < ::Rails::Engine
    isolate_namespace Stake

  end
end

And in the main app I've got

#file: Gemfile
...
gem 'stake'
...

#file: config/routes.rb
Pop::Application.routes.draw do
  root :to => 'home#index'

  mount Stake::Engine, :at => '/stake'
end

#file: app/models/posted.rb
class Posted < ActiveRecord::Base
  extend Stake::SharedPost
    ...
  end
end

The main application will load, though with no available data on it. While I try to run

rake db:seed

for example when trying to load the Posted model I get the error uninitialized constant Stake::SharedPost

What am I missing to get access to my gem's namespaced modules?

EDIT: I've read into the acts_as pattern and that doesn't seem to be the cleanest way of doing things, plus I'm not sure how to implement that onto my engine. Is there another solution?

Shrewd
  • 731
  • 5
  • 14

1 Answers1

1

In lib/stake.rb are you including the lib/stake/shared_post.rb file?

It should look something like this:

# file lib/stake.rb

require "stake/shared_post.rb"

module stake
    ....
end
Krista
  • 895
  • 1
  • 6
  • 16
  • I actually figured this out a while after posting but thought the post was long gone for some reason. Thanks! A question though, how come files in the lib directory don't get required automatically? isn't that something rails and\or the Gemfile are supposed to do? – Shrewd Aug 26 '12 at 08:40
  • 1
    As far as I know, files in the lib directory do get required automatically - just perhaps not in nested folders. Also, the behavior of an isolated namespaced engine is probably a little different than a normal rails application? Also if just all files were automatically required, then they would all be accessible from the main app - and you might not want to expose all of them. Sorry these are all just guesses :) It's not something I've looked into extensively, but hope that helps! – Krista Aug 28 '12 at 15:00