Thursday, September 26, 2013

`raise_if_continuation_resulted_in_a_channel_error!': PRECONDITION_FAILED - parameters for queue 'hello' in vhost '/' not equivalent (Bunny::PreconditionFailed)

This error happens when you have a queue that is not durable and you are doing something to assume that it is durable. Change the name of the queue to a new name and make it durable to fix this error.

Saturday, September 14, 2013

How to customize Rails 404, 422, 500 pages that is compatible with Exception Notifier Plugin

1. Add :
            config.exceptions_app = self.routes
to application.rb.
2. Add routes for error pages :
  match '/404', :to => 'errors#not_found'
  match '/422', :to => 'errors#server_error'
  match '/500', :to => 'errors#server_error'
3. Create a errors controller:
     rails g controller errors not_found server_error
4. Implement the actions :

class ErrorsController < ApplicationController
  def not_found
    render :status => 404, :formats => [:html]

  def server_error
    render :status => 500, :formats => [:html]

5. Customize the views, not_found.html.erb and server_error.html.erb.

Tuesday, September 10, 2013

Updates were rejected because a pushed branch tip is behind its remote

If you are working on a branch and it gives you this error message when you do a git push, you need to run:
git pull origin master

and now the git push will work. This happens when the master changes and you have not updated the local copy with the changes in the remote master branch.

Sunday, September 08, 2013

Clearing Stuck Delayed Jobs

Delayed job had completed its work, for some reason it was still stuck in the queue. To remove it from the queue, on the server run the commands:

Step 1 :

$ RAILS_ENV=production script/delayed_job stop

This will stop the delayed_job process.

Step 2 :

$ pgrep -f delayed_job

This will return no result, therefore confirming that the delayed_job process has been stopped.

Step 3:

Go to rails console in production and run :

> job = Delayed::Job.count
   (0.3ms)  SELECT COUNT(*) FROM `delayed_jobs`
=> 1

> job = Delayed::Job.first
  Delayed::Backend::ActiveRecord::Job Load (0.5ms)  SELECT `delayed_jobs`.* FROM `delayed_jobs` LIMIT 1
=> #
> job.delete

Thursday, September 05, 2013

Disable Edit and Delete in Rails Admin

Add the following code to rails_admin.rb:

  RailsAdmin.config do |config|
    config.actions do
      # root actions
      dashboard                     # mandatory
      # collection actions
      index                         # mandatory
      # member actions

As you can see from above, there is no edit or delete actions defined. Now you will not be able to edit or delete the models when you are logged in as admin.
Due to Kaminari and Will Paginate conflict in Rails Admin, to fix this add the following code to kaminari.rb in config/initializers:

config.page_method_name = :per_page_kaminari

Monday, September 02, 2013

How to find ruby gems

1. Look at the Gemfile.lock and see the dependencies of a gem. Search in github for each gem in the dependency tree to learn more about the gem. You can also use the gem command :
    gem dependency gem_name to get a list of dependencies of a gem. For instance, running :
    $ gem dependency capistrano

   Gives the following output:

Gem capistrano-2.15.5
  highline (>= 0)
  mocha (= 0.9.12, development)
  net-scp (>= 1.0.0)
  net-sftp (>= 2.0.0)
  net-ssh (>= 2.0.14)
  net-ssh-gateway (>= 1.1.0)

2. Follow the Rails Core Team members Twitter feed.
3. Talk to other developers and ask what gems they have used in their projects.
4. Subscribe to Ruby podcasts, blogs.

I was working on a Rails project that was having a giant if-else statements to recognize the mime-types of a given file for uploading to Amazon S3. I was looking at Gemfile.lock and used tip #1 above to find the mime-types and read more about the gem on github. I was able to replace my custom mime-type recognition code with the mime-types gem. If you run:

$ gem dependency mime-types

you will see only development dependencies on other gems, there are no runtime dependency on any other gems. This gem is self-contained and is a good unit of reuse.

It's like using small Unix utility to compose and solve many different problems. The only dependency it might have is that it works only on certain versions of Ruby.

Things to Consider When Evaluating Gems

1. Simplicity vs Flexibility

     There is a trade-off between simplicity and flexibility. For instance for file uploading you could use many gems like fog, carrierwave etc and create lot dependencies in your code that provides ease of switching between libraries - flexibility. But it complicates your codebase and simplicity is lost. You will also have more work during upgrade of your project because of these dependencies.

2. Usefulness

     Can I write this by myself in a simpler way that is easier to manage? Read the source code of the gem to make your own decision about reuse or develop on your own.

3. Support and Maintenance
  •     Is there any critical bugs that is not being addressed by anyone? 
  •     Is the source code easy to read and modify for your needs? 
  •     Are the developers active and keep up with upgrading the gem as new Ruby versions are released?