Tatva-Artha

meaning of "it"

Archive for the ‘Technology’ Category

Upgrading to ruby 1.9: rbx-require-relative requires Ruby version ~> 1.8.7

without comments

If you are upgrading from ruby 1.8 or Ruby Enterprise 1.8.7 to ruby 1.9.2, you may encounter this error.

Installing rbx-require-relative (0.0.5) Unfortunately, a fatal error has occurred.
Please report this error to the Bundler issue tracker at https://github.com/carlhuda/
bundler/issues so that we can fix it. Thanks!/Users/sjain/.rvm/rubies/ruby-1.9.2-p290/
lib/ruby/site_ruby/1.9.1/rubygems/ installer.rb:364:in `ensure_required_ruby_version_met':
rbx-require-relative requires Ruby version ~> 1.8.7. (Gem::InstallError)

This most likely happens because you are declaring a dependency on ruby-debug gem in your Gemfile.

group :development do
  gem 'ruby-debug'
end

With ruby 1.9, you need to update this with new gem name ruby-debug19.

group :development do
  gem 'ruby-debug19'
end

This will eliminate the dependency on rbx-require-relative and fix the issue.

Written by Sharad

August 21st, 2011 at 1:55 pm

Posted in All,Technology

Tagged with , , ,

Setting environment variables in Max OSX

without comments

Any a developer switching from linux to mac, the way to set environment variables is similar yet different enough that is annoying at times. I have so far failed to find a good documentation on where and how to set those in mac osx. Today I found one here.

My takeaway:

Mac OS X applies .bash_profile and .profile only for Terminal.app environment and Apple’s technical documentation suggests using ~/.MacOSX/environment.plist for other applications. So, by default PATH value will differ for RubyMine and the console.

For managing the global environments, it also recommends a system preference pane app. This worked for me.

Written by Sharad

May 16th, 2011 at 10:12 am

Posted in All,Technology

Tagged with , , ,

Javascript Garden

without comments

If you consider yourself a beginner or intermediate Javascript developer and feel the need to advance, here’s something that may be right up you ally: Javascript Garden.

Written by Sharad

March 19th, 2011 at 12:12 am

Posted in All,Technology

Tagged with ,

Mocha unexpected invocation errors

without comments

If you find that your tests pass when run alone but fail with “unexpected invocation” inside “rake test”, you are most likely seeing behavior that is caused by mocha load order.

This is a documented gotcha with mocha. And also discussed here.

It happens when mocha gem is listed as library dependency inside rails app. The library gets loaded too soon. The fix is to load mocha after rails has booted up.

For rails < 3.0.0, this means removing -- config.gem 'mocha' -- entry from config/environment.rb and adding -- require 'mocha' -- at the bottom of test/test_helper.rb.

For rails >= 3.0.0, this means adding — :require => false — inside Gemfile entry as follows.

# Gemfile
group :test do
  gem 'mocha', :require => false
end

And then adding following line at the bottom of test/test_helper.rb:

# test_helper.rb
require 'mocha'

Written by Sharad

March 11th, 2011 at 10:38 pm

Posted in All,Technology

Tagged with , ,

Installing RMagick gem on Mac OSX

without comments

Commands for installing image-magick package and rmagick gem on max osx:

sudo port install tiff -macosx imagemagick +q8 +gs +wmf
sudo gem install rmagick

Reference: http://rmagick.rubyforge.org/install-osx.html

Written by Sharad

March 8th, 2011 at 10:41 pm

Posted in All,Technology

Tagged with , ,

ActiveRecord::MissingAttributeError: missing attribute — a bug or a feature?

with one comment

Sooner or later, the need arises to assign default values to model attributes. In rails this is usually done in model’s after_initialize() with a new_record? guard.

Something like:

class User
  def after_initialize
    if new_record?
      self.country = "US"
    end
  end
end

This initializes the object with proper default so the UI form gets properly populated and the database gets proper default if user hasn’t overridden it.

Sometimes, you realize that you have (pre-existing?) objects in database that also need the same default if the current value is nil. In such case, you may modify your initializer like this:

class User
  def after_initialize
    self.country ||= "US"
  end
end

This works for both scenarios: User.new and User.find.

However, it introduces a behavior that may not be apparant immediately.

$ script/console
ree> User.find(u.id)
  User Load (0.9ms)   SELECT * FROM `users` WHERE (`users`.`id` = 1) 
ree> User.exists?(:id => u.id)
  RateSearch Load (0.6ms)   SELECT `users`.id FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
ActiveRecord::MissingAttributeError: missing attribute: country
/[prj]/app/models/User.rb:3:in `after_initialize'

A closer look reveals that ActiveRecord tries to be smart and only fetch ‘id’ column when performing Model.exists? call.

Many people have tripped this and logged it as a bug. The report has been silently ignored, and I believe, for good/performance reasons.

So, what do we get around it? Here’s one way.

class User
  def after_initialize
    self.country ||= "US"
  rescue ActiveRecord::MissingAttributeError
    # this should only happen on Model.exists?() call. It can be safely ignored.
  end
end

As they say, it is not a bug, it is a feature.

Written by Sharad

March 3rd, 2011 at 8:07 pm

Posted in All,Technology

Tagged with , ,

Online Regular Expression Tool

without comments

Rubular, is a tool that you may find handy when developing ruby regular expression for scraping unstructured data needs.

http://rubular.com/

Good one!

Written by Sharad

March 2nd, 2011 at 6:14 pm

Login as multiple users simultaneously for testing

without comments

Ever wondered how to login as two different users at the same time? The situation manifests in any project where you want to be able to test a workflow involving several actors with different roles. The problem is that browsers allow only one login for a given domain at any one time. So, you are required to logout as current_user before logging in as another. One of the solution people use is install multiple browsers.

Read the rest of this entry »

Written by Sharad

May 26th, 2010 at 8:40 pm

Posted in Technology

Disabling form post in google chrome and safari

without comments


So, I found this issue with google chrome and safari web browser and I believe this applies to any webkit based browser. When a form submit is disabled by setting the submit button to disabled=true attribute, the browser is usually expected to prevent submission when the user hits “ENTER” on any of its text fields. This works fine in Firefor, my default browser.

In my rails app, I had to do this to work around this.

We use haml rendering engine for our project and my form looks like this:

- remote_form_for @product_search, :url => product_searches_path, |
  :html => {:method => :post, :id => 'product_search_form'} do |form|

Read the rest of this entry »

Written by Sharad

May 17th, 2010 at 5:36 pm

Testing Cookies in Rails

with one comment

Testing cookies in rails have a few quirks. Some of these are documented here nicely.

I ran into a different one today. In my case, I needed to set a few flashVars variables to pass to flex app (swf file) depending on presence of certain cookies. So, my test involved setting cookies on controller/request and then making sure they appear as flash variables. Here is what I did.

Test:

  should "render last_user_name and auth_token cookies as flashVars on new UserSession" do
    controller.request.cookies[:last_successful_username] = "vriuser1"
    controller.request.cookies[:auth_token] = "12345"
    get :new
    assert_equal "autoLogin=true&username=vriuser1", assigns(:flash_vars)
  end

Controller

  def new
    auth_token = cookies[:auth_token]
    last_username = cookies[:last_successful_username]
    @flash_vars = []
    @flash_vars << 'autoLogin=true' if auth_token
    @flash_vars << "username=#{last_username}" if (last_username)
    @flash_vars = @flash_vars.join("&")
    @user_session = UserSession.new
  end

Until I realized that setting cookies as symbols don’t work. Even though you can read cookies using symbolic-keys, you can’t set them as such in tests. I had to set them as strings.

  should "render last_user_name and auth_token cookies as flashVars on new UserSession" do
    controller.request.cookies['last_successful_username'] = "vriuser1"
    controller.request.cookies['auth_token'] = "12345"
    get :new
    assert_equal "autoLogin=true&username=vriuser1", assigns(:flash_vars)
  end

That did the trick!

Written by Sharad

August 6th, 2009 at 2:50 pm

Posted in Technology