Wednesday, February 20, 2013

Ruby chaining and comments

I have run across interesting feature in ruby when I tried to write chain methods separated in new lines. Ruby 1.9+ enables dots in new line at the beginning of command, not only at the end of last line, like this:

"somestring"
  .upcase
  .first

But, if you want to put comment in separate line before chain method call then ruby throws syntax error. So, this is invalid expression in ruby:

"somestring"
  .upcase
  #take first letter
  .first

If you want this to be valid you have to put dots at the end of the line.

"somestring".
  upcase.
  # take first letter
  first

Tuesday, February 12, 2013

MS Excel doesn't read UTF-8 CSV file correctly

I had an issue where MS Excel would not open CSV file generated in node.js in correct encoding. Instead of opening it with UTF-8 encoding it would use default ANSI encoding. To resolve this I had to add BOM at the beginning of a file. So my write file function would look like:


fs.writeFile(filename, '\ufeff' + data, 'utf8', function(err) {
    if (err) console.error(err);
});

This still doesn't resolved the issue MS Excel has, where it doesn't recognize C in CSV.

Reference:

  1. StackOverflow

Thursday, February 23, 2012

jQuery popup boxes plugin

I have recently created a jQuery plugin that mimic behavior of javascript popup boxes: alert, prompt and confirm. The problem that I had with usage of native javascript popup boxes is that it stops the execution of script until user close it. Also, I wanted to separate a code that will be executed if user confirms or cancels the dialog without further inspecting of return value.

With that in mind I created alternative for popup boxes using jQuery plugin and jQuery deferred objects (introduced in jQuery 1.5). Also, to simplify my code all popup boxes are dependable on jQuery UI dialog.

The three methods are:
  • $.alert(messagetitledialogOptions)
  • $.confirm(messagetitledialogOptions)
  • $.prompt(titledefaultValuedialogOptions)
So, to alert some message simply call:

$.alert("Poruka")

Deferred
The beauty of it comes with a usage of deferred object. So if we want some code to be executed only if user confirms it, than we can write something like this:

$.confirm("Changes on this document are not saved. Open a new one anyway?")
  .done(function() {
    // make some ajax call
  })
  .fail(function() {
    $.alert("Pfff... we almost lost this file")
  })

What will happen is that if user clicks OK, the deferred object will be resolved and code under done will be executed. If user clicks cancel or closes the dialog, code under the fail will be executed. So, there is no need for additional if condition. Also, code is cleaner and easier to read and understand.

The same thing can be managed with use of prompt as well.

$.prompt("How much?", 1)
  .done(function(data) {
    $.alert("You have enetered " + data)
  })

This would prompt user to enter value. After that if user clicks OK or presses enter it would run function passed to done and it would provide entered value in data argument.

Dependence:

Source code:

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