How do I Make a Constant from a Gem Available in Controller - ruby-on-rails-3

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.

Related

Backburner rake task - NameError: undefined local variable or method

I'm starting up with Backburner to run some jobs in the background of my app. I've tried following the documentation under "Working Jobs", but I'm clearly doing something very obvious and very incorrect.
Gemfile:
gem 'backburner'
Rakefile:
require_relative 'config/application'
require 'backburner/tasks'
Rails.application.load_tasks
And then in the rails console:
rake backburner:work
(which I think will just run all pending, enqueued backburner jobs.)
But all I get is the error: NameError: undefined local variable or method 'work' for main:Object from (pry):1:in '__pry__'
I sorta know what pry does (I use it to throw binding.pry into my code to debug, but that's about it), but I have no idea how it might be causing this. Not to mention I'm still pretty weak on the whole jobs and rake task front in general.
Any help would be much appreciated!
Apparently I'm in the business of asking dumb questions, lately. I was passing a variable to my Backburner.enqueue that is an ID, so I expected it to be numerical, like so: #admin.id. This would have been fine if #admin was defined in the scope of the helper I'm calling my enqueue in, but I'm actually passing that variable to that helper method...which means #admin is not defined, which, guess what Daniel, means #admin.id is undefined. Which means that I'm hitting the job anyways (because no, Backburner is in fact NOT breaking), but I'm not getting the job enqueued the way I thought I was because I was passing an invalid property to it.
So it sort of turns out that the error I got looked completely unrelated to the issue, but there it is. Always pass the variables your methods are expecting, or else expect errors.
EDIT:
Also, I can't downvote my own post. I tried. It deserves it.
EDIT 2:
Turns out it's really hard/not supported to get beanstalkd going on Heroku. Der. Also should have checked that. I just liked how beanstalkd was handling some things and went with it before getting to deploy and realizing it was a no-go.
Instead, I went with Resque as suggested by Heroku. They also support Delayed Job (DJ), but noted it is pretty heavy and Resque is better.
All that to say, I finally got to where I needed to go with Resque, and because it was such a thing I made a little documentation to help me (and maybe any other sad soul who stumbles across this lame post) get background workers running on a Heroku-hosted Rails app in the future. Be warned: it really is a guide for dummies like me, since I had to puzzle out a lot of what now is fairly obvious to me. There are gaps in the Resque documentation that I've included, which really just boils down to being explicit about what certain things mean, and where things go.

Initializer does not execute when models reload on Rails 3.1 development environment

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

How to find and remove gems that are no longer used?

I working with medium Rails application which has about 60 gems dependencies. Many of these dependencies are unnecessary/useless.
Is there any way to find out which gems are not used by application?
This is not possible.
You could however start with the following:
1) Go to http://rubygems.org/ and search for gems, you think aren't 'available' anymore. If they're removed from rubygems, You can't use them anymore so Delete them from your gemfile.
2) Just look carefully into your app and just write down which ones you're sure about. The ones you aren't sure about are the ones you don't use.
3) You can use this: http://ruby-prof.rubyforge.org/ to show the called parameters, call times, memory usage and object allocations
Hope it helps you :)

Extending classes from a gem

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!

rack-google-analytics

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