I have a gem, that declares the class FulltextRow
I created an initializer called hacks.rb were I open classes from different gems.
I added the following code:
FulltextRow.class_eval do
....
end
However, If I don't require the class I get the exception:
uninitialized constant FulltextRow (NameError)
But it works well If I do:
require 'fulltext_row'
FulltextRow.class_eval do
....
end
My question is:
Why do I need to require a class that is defined in a gem? Aren't they automatically required?
I would love to help you out but it's hard to troubleshoot this with the lack of information given. I do have two suggestions, though.
1) You should verify the gem is loading properly within the gem. What I mean is to have supporting tests that affirm that the gem is working as intended. I've had instances where my gem's classes were not loading because I wasn't setting up the file structure right or had a silly typo somewhere.
2) It is also possible that your gemfile has set to not load the gem automatically.
gem full_text_row, require: false
Good luck!
Related
We are currently using Ruby 1.9.3, Rails 3.1 (i know, we're working hard to upgrade all our applications).
We're using a module (let's call it 'OurModule' to add a method (let's call it 'OurAddOnMethod' to a model defined in a gem (let'd call that 'GemModel'). We have that module file living in the 'config/initializers' directory.
That file defines the module, and then calls this to include it in the model:
# Include the extension
GemModel.send(:include, OurModule)
When developing, things work well mostly, but periodically we will get an error that basically says "Undefined method 'OurAddOnMethod' in 'GemModel'". Restarting the server resolves the issue (for a while).
I'm assuming this is happening because the models are reloaded periodically with changes made in the development environment, and it appears that the initializers do not also get reloaded at that time..? It seems like this may not be the best way to set things up; it is quite frustrating to deal with.
Can anyone enlighten me on a better way to achieve this?
I ended up using wrapping the code in the following, and keeping it in initializers:
ActionDispatch::Callbacks.to_prepare do
# configure stuff or initialize
end
I feel really bad, i completely missed this question that seems to completely cover mine (linking to the answer that i used):
https://stackoverflow.com/a/8636163/287516
I'm upgrading an old Rails 2 app to 3.2. Rspec is giving me this error:
expected /app/models/api/key.rb to define Api::Key
The actual file is:
module API
class Key
So the capitalization is wrong according to Rails convention. I'd like to avoid project wide searching and trying to change everywhere the constant is referenced. Is there any way to tell Rails the module is in all capitals?
For reference, I did try to use the inflector:
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym "API"
end
Is it a caps issue? You have "API" and the error says its expecting "Api". I'm not sure if that would matter or not but it seems like it would
I'm using the gem rack-google-analytics in my rails project but when I run it in production mode I get an error.
rack-google-analytics-0.11.0/lib/rack/google-analytics.rb:11:in `initialize': Tracker must be set! (ArgumentError)
I'm trying to find out what this means. initialize': Tracker must be set!
in my application.rb file I have this at the bottom of it.
if Rails.env == "production"
config.middleware.use("Rack::GoogleAnalytics", :web_property_id => "UA-18760745-1")
end
If O take that out the error goes away so it has something to do with initializing this behavior, but just not quite sure why.
Anyone ran into this issue and have a solution to it, that they could share.
The only answer I could find was to not use that Gem and use this one instead as it works right out of the box. Maybe someone else can prove me wrong, which I hope.
Hope this other link helps someone else.
https://github.com/bgarret/google-analytics-rails#readme
Don't know if it helps, but here is my code:
config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-XXXXXXXX-1'
in application.rb
I'm assuming it requires a value for the :tracker symbol upon initialization (I'm following this readme on the gem's GitHub: https://github.com/kangguru/rack-google-analytics)
Obviously, replace XXXXXXXXX with your organization's tracker code.
And it seems like setting the :web_property_id symbol is a feature of a different (but similarly named) gem, rack-google_analytics: https://github.com/ambethia/rack-google_analytics
I have a feeling this is a dumb one, but I've spent some time trying to figure it out and googling it, and to no avail. I'm trying to use a Ruby Gem in a Controller. I have included it in my Gemfile, run bundle install, seen it show up in my gems list, restarted my local server. But somehow whenever I try to call the gem ( rails_rrdtool ) It just tells me
uninitialized constant RrdgraphsController::RRD
app/controllers/rrdgraphs_controller.rb:22:in `show'
The spot in my code where it wigs is when I'm calling
RRD.graph
It's as though it doesn't know where the heck the gem is... However, I can use require to import it successfully into an irb session. So I know that it works, it's just not getting in there some how...
Bundler should be handling the inclusion of the gem I assume. Am I calling it in the wrong place?
This looks like a namespacing issue. Your error says it is looking for the constant inside of the current class: RrdgraphsController::RRD when it should be looking for a class outside of the current context.
Try prefixing the class name with a double colon to fully define the location of the class.
::RRD.graph #rest of your code
There's a good analogy of what this does in this other accepted answer. Basically it creates an absolute path so Ruby doesn't have to guess.
Originally, I have posted Stack Overflow question Ruby on Rails gems... Re-open models (now deleted). But I believe this question is way too confusing... I'll try to ask differently based on what I have discovered.
Let's say...
Gem A has an engine (lib/a/engine.rb) and declares a model M (ActiveRecord based, 'app/models/m.rb').
In gem A, there is an entry point (lib/a.rb) which is parsed when rails loads the gem.
In gem B, there is an entry point (lib/b.rb) which is parsed when rails loads the gem.
Gem B depends on gem A (specified in gemspec) and needs to re-open the A::M model.
I have checked that 'a.rb' is loaded before 'b.rb', and it is.
Now my problem is that while being in 'b.rb', if I try to do a class_eval on the M model (From gem A, remember?) to re-open it and add 'B specific logic' to it, the M class is not yet defined... I have put messages in 'm.rb', and I figured that it is being loaded a long time after 'a.rb' and 'b.rb' are loaded...
From within gem B, how can I get a callback after ActiveRecord have loaded my model A::M? Do I need to do something in my engines? Or in other words, what files are loaded after ActiveRecord has loaded models in A?
Just in case, make sure to load gem A manually in gem B as well.