How to require module extension code - ruby-on-rails

My setup: Rails 3.0.9, Ruby 1.9.2
I need to extend Active Merchant module to include my own code, I'm following the article here
http://blog.matthodan.com/how-to-add-support-for-paypal-website-payment
As per the article, I created paypal_recurring_payments.rb in /vendor/plugins/active_merchant/lib/active_merchant/billing/gateways/paypal/. Next I have to modify an Active Merchant gem file paypal.rb to add a line of code. The problem is that I can't deploy that code change to Heroku, so I need an alternate way of doing this. Any insights will be much appreciated.

Fork the gem on github
Make the appropriate change to your fork of the gem.
Reference your fork in the Gemfile using the :git option. See the docs for examples.

Related

Is it possible to create a ruby gem that when added to a rails project simply appends code to its initializers?

I've got some helper code (monkey patches) I tend to carry around from rails project to rails project via simply copying files or pasting code into the initializers folder.
I think a clean way to deploy different categories of my initalizer code would be to simply add gems to my project Gemfile that would do the equivalent of this for me. I'm not very skilled at creating gems or the deeper side of ruby on rails engineering so I just wanted to know if this were possible before I got started with it.
Yes, this is definitely possible. It should be probably enough to make a simple gem, put your monkey-patches there and require them in the gem entry point (e.g., if the gem is called foobar, then bundler will require lib/foobar.rb by default).
There is a guide on creating gems available in Bundler docs. In case you need to interact with Rails application configuration, you can take a look at Rails Engines.

How can I alter a gem on heroku?

I recently deployed my app to heroku. It uses Devise, which I had to alter the after_sign_up_path so it would redirect correctly to a set page after someone signs up. This works fine locally, but when I deploy on heroku it installs the original unmodified devise gem.
Does anyone know of a way to modify a gem on heroku, or to make it use the modified gem in my vendor folder?
Thanks.
So, in general, it shouldn't be any different deploying your app on heroku than anywhere else.
One difference is that on heroku you're probably running in 'production' environment settings, and locally in 'development'. You can run in production locally too, if that were the issue.
But I don't think it is. When you say you "altered" Devise, do you mean you actually edited the source code inside the Devise gem source itself?
That's not a good practice. When a new version of Devise comes out, perhaps with security patches, what are you going to do? Re-customize the new version? It's not a great maintenance plan. If you really want to do this you can. The best way might be to create a fork of the Devise gem in a git repo (on github or anywhere else), customize that and commit your customizations to the repo, and then point to your customized version of Devise from your own git repo in your Gemfile with gem 'devise', :git => 'http://some.git.repo.clone.url.org'
But it's a bad idea.
Devise probably already supports your use case with configuration, rather than by editing Devise source code. Most experienced devs would consider that a much preferable way to accomplish what you want.
In this case, it looks like you should be providing an after_signup_path method override in your own local app, not modifying the default implementation in devise source. Google for more info, or consult the URL Finks provides in another answer, or post another question specifically asking how to do what you want to do with devise (it's actually nothing to do with heroku), being as specific as possible about what you want to accomplish.
Assuming you forked Devise to your github account, you can point your Gemfile to it by doing this:
gem "devise", :git => "git://github.com/user/devise.git", :branch => "my-awesome-branch"
By the way, I'd recommend you overwrite some devise methods rather than hack the gem but thats just my 2cents.
This may not answer your question but according to devise wiki, you don't have to alter the gem to get the redirect behavior you like, you just need to override the after_sign_in_path_for method in application controller. https://github.com/plataformatec/devise/wiki/How-To%3A-Redirect-to-a-specific-page-on-successful-sign-in-and-sign-out
Example:
def after_sign_in_path_for(resource)
current_user_path
end

Is there an updated rails_sql_views gem for rails 3.2?

I need the functionality provided by the rails_sql_views gem. However it looks like the last commit for this gem was made in 2010. Has this project been outdated by a new project? I'd like to find an active gem to use to get this functionality.
http://activewarehouse.rubyforge.org/rails_sql_views/
http://rubygems.org/gems/rails_sql_views
After further research here is a Rails 3 candidate for similar functionality:
https://github.com/bradphelan/Active-Illusion
This is a gem of this blog post:
http://xtargets.com/2011/08/02/tableless-views-with-active-record/
However this solution doesn't seem to be very popular.
schema_plus gem has create_view method that seems compatible (although I'm not familiar with rails_sql_views).
barancw, I needed this gem for our product using Rails 3.2.5, so I forked the repo and updated the necessary pieces. This gem is great for improving the performance of our large database queries, as it reduces the need to load objects into memory. I combined this gem with another optimization: Rails - given an array of Users - how to get a output of just emails?
https://github.com/ryanlitalien/rails_sql_views
Original documentation: http://rubydoc.info/gems/rails_sql_views/0.8.0/frames/index
Please keep in mind the docs are a bit outdated ("require_gem" has been replaced with "gem" and add the gem to your Gemfile as well).

How to integrate Resque plugin with rails 2.3.5 application?

I am trying to integrate my rails app with Resque plugin but in the doc it is not given that where should I write the code and how to use that code when running application. May be it is not understandable for me.
It would be helpful for me if any one explain it with an example application.
Thanks in advance.
You just need add the gem in your config.gem 'resque'
After you can call by Resque.enqueue(xxx). All class call by Resque queue need to be load. You can add it on app/worker directory and add it in load_path. Or put in lib and use the autoloading stuff.
You could follow the steps as specified in the railscasts.

Necessary steps for using a gem (e.g. json) in Ruby on Rails app?

I'm having trouble getting rubygems to work in my Rails app. Specifically, I'm trying to use the json gem, documented here: http://flori.github.com/json/ I can successfully use JSON.parse() in IRB and script/console but not in my rails app.
I know it's some combination of config.gem 'json' in environment.rb and other things, but can't find a good explanation anywhere.
Can someone give me a concise list of what is required to use this gem OR point me towards comprehensive documentation of using gems in Rails? thanks!
config.gem is used for gem dependency in rails and it does nothing more than telling rails that a gem is needed, and helping the user install the appropriate gem, etc (more details here: http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies)
however by installing a gem, it should be able to be used by the rails app automatically, if not, probably you can add require "json" into environment.rb or in an .rb files in the initializers folder?
Hope it helps =)
I solved it. There's decent documentation here: http://apidock.com/rails/Rails/Configuration/gem
Once you have used config.gem in environment.rb, you should not need to 'require' it later.
My problem was that I had not stopped and restarted the server! It worked in script/console because everything was getting reloaded every time.

Resources