Wednesday, January 11, 2012

jQuery AJAX resolved fail for XML file in IE

I had a jQuery script that gets dynamically generated XML from server. This code worked fine in Chrome and Firefox, but for some reason AJAX deferred object has been resolved as fail in IE, even though response status code was 200 and I could see XML content in Network tab in IE's Developer tool.

$.ajax({
 url: 'generateXml/',
 dataType: 'xml'
})
.done(function(data) {
 alert("success");
})
.fail(function() {
 alert("fail");
});

When I pointed url to static XML file everything worked fine. Only difference I notice is that content type was somewhat different.

For static file it was application/xml, while my dynamically generated file was of type text/xml.



When I changed my backend code for XML generator to set ContentType as application/xml everything worked fine.

Thursday, January 5, 2012

Source code beautifier

I use and strongly recommend http://hilite.me/ for code beautification.

My languages supported.

Ruby on Rails I18n good practice

Following is example of what I think its good practice on setting up I18n on pages, this mini pattern can later grow but I think it is a good starting point.

Let's say you have BaseController which will be extended by your controllers.

class BaseController < ActionController::Base
  
  before_filter :set_session_locale

  def set_session_locale
    session[:selected_locale] = params[:lang].nil? ? session[:selected_locale] : params[:lang] 
    
    I18n.locale = session[:selected_locale] || I18n.default_locale
  end

end

before_filter will make sure that this is triggered before each request, this is important since I18n will not persist beyond one request. Priority will be given to URL param lang, if not than to session locale, if not than to default locale.

Session will be used to propagate locale selected. You can force language with each request to each controller than extends BaseController for example:

localhost:3000/index?lang=de

To extend this approach for registered users you can use another controller which will be used once user is logged in. Here is example of this ApplicationController

class ApplicationController < BaseController

  before_filter         :set_user_locale
  skip_before_filter    :set_session_locale
  
  # Application code which will among other things set @user

  def set_user_locale
    I18n.locale = @user.locale|| I18n.default_locale
  end

end

We can see now that before_filter is used to trigger user preferred locale, if none is present it default to I18n one.

I18n.locale = @user.locale|| I18n.default_locale
 
We can see that skip_before_filter is used to disable BaseController's set session 
locale, since priority is now to user. If we didn't added this filter this would still work fine, 
but there would be unnecessary call to set_session_locale method, from  
ApplicationController.
 
Have fun and tweak approach to your needs. If you have any questions leave it at comments section I will be happy to answer them.

Tuesday, December 27, 2011

Example of Git config file, .gitconfig


[user]
name = Haris
email = haris@xxx.xxx
[core]
excludesfile = /Users/haris/.gitignore
[alias]
   st = status
   c = commit
   co = checkout
   br = branch

Ignore (modified) repository file with Git

So here is a problem, after you add something to git repository you have to remove it so you can ignore it. Problem with this is sometimes you want to ignore file but not remove it from repository. For example I am using RoR on windows and rest team is using MaxOS, so I should not commit Gemfile.lock but I cannot remove it either here is a way:

git update-index --assume-unchanged Gemfile.lock
or you can do same with directory
git update-index --assume-unchanged log

Re-install gem in Ruby

Good way to avoid using install and uninstall commands to re-install the game you can use.
gem pristine <gem name here>
You can also use --all flag to rollback all gems. 

Tuesday, December 13, 2011

Javac debug mode by default

So here is my problem, I am working on a big project and build is organized trough couple of dozens ant scripts. Of course nobody had insight to add debug mode to be configurable on javac tasks so you have to go manually change and add debug="true" attribute.

Not sure if anybody will need this but here it goes, way to default javac to be in debug mode. First go in JAVA_HOME/bin and change javac.exe to javac-old.exe, after that create javac.bat script.

Inside of javac.bat enter following command (this is tested with JDK 1.5_xx).


CALL javac-old -g %* 


What this will do is basically re-route all calls to javac to your "real" javac (in this example javac-old.exe) and pass parameter -g (meaning debug-all mode) and also %* (meaning pass or parameters that you received to next call). Hope it helps somebody.