Wednesday, March 26, 2008

script/console reload models

if you are one of the Rails applications developer, then you would have used this command before
ruby script/console

it is very nice and gives you the ability to load your environment and try it as you wish, but what if you changed anything in the application models, you will find that the console isn't aware of these changes at all

so what can you do to make him aware of your changes?

here it is, just run this command in your console
Dispatcher.reset_application!


Now you can try the new changes you made in your models

Wednesday, February 27, 2008

Unobtrusive JavaScript

While serving the internet looking for something, i found someone talking about something called 'Unobtrusive JavaScript', it was the first time for me to hear that expression so i looked for it and found that

Unobtrusive JavaScript is one of the emerged paradigms in JavaScript, its main idea is to achieve more separation while working on web applications

Since now, we almost reached to an agreement to separate presentation from content/structure through separation of CSS from HTML, this paradigm is greedy and wants extra separation of behaviour from structure

lets look at this example to clarify things out
<#input type="text" id="date" onchange="validateDate(this);" />


here we can see that our structure is that simple input field but it is mixed by this onchange function which shows to us what this input does on change

Now, look at that one
<#input type="text" id="date" />


here we have only structure and no behaviour, so WHERE SHOULD WE ADD OUR BEHAVIOUR?, we should do that with javascript away from our presentation as follows

window.onload = function(){ //Wait for the page to load.
var input = document.getElementById('input');
input.onchange = function(){
//Do something changed.
}
};


This is the simplest illustration that i can give to this paradigm, it makes you live in a world of layers (structure, presentation, behaviour)

actually, i am admired with this paradigm so much because it layered our application and thus allowed for better separation but i think one of its problems is that looking at structure won't give you a quick idea about what elements behave until you visit the behaviour layer

finally, i would like to end with this nice quote from one of the articles i read about this topic
The first rule of the unobtrusive Javascript club is don't talk about the unobtrusive Javascript club.


this post isn't done yet until i hear from you what do you think about this emerged paradigm and whether it should be the dominant in the following years or not

Ip2Country Mapping

I was working on a web application that required mapping every request to its corresponding country in order to use this data later in site statistics

i searched over the internet for a solution that can do this mapping for me, i found a lot of solutions that are great to the extent that they can also get the ISP and geographical location of the request but MOST OF THEM WERE NOT FREE

Since i was only looking for a simple solution that simply maps ip to corresponding country, i searched for free versions of these solutions which are limited but free

i found this Website that offered a nice & easy solution for this issue. Although, this website has a very nice tutorial but i will explain what i did because i modified the steps provided by the website somehow

Just do as follows

1. Download the CSV file that contain all mapping details

2. Create a new table in your database that should carries all data needed to do
this mapping, you will needs these columns
* begin_num - FLOAT
* end_num - FLOAT
These are two magical numbers that will be used later in
our mapping

* ISO - VARCHAR(2)
* Name - VARCHAR(255)

2. Import CSV file into that table but, remember to choose only 3rd, 4th, 5th, and 6th values while importing - neglect 1st and 2nd one because they are useless in our mapping

3. On every request, apply this formula on the IP as follows

ipnum = 16777216*w + 65536*x + 256*y + z

where

IP Address = w.x.y.z


4. Use ipnum to retrieve corresponding ISO or name of country through this simple query

SELECT ((required-columns)) FROM ((table-name)) WHERE ((ipnum)) BETWEEN begin_num AND end_num


That's it, you can now map any request to the corresponding country with an accuracy reaching 98% as stated by this website

Note:
1. This query may return nothing if the IP can't be mapped and this will happen especially when using localhost
2. Always index begin_num and end_num for better performance
3. Always update your CSV file because it is updated every now and then on the website so keep an eye on the website to get updates

Wednesday, December 19, 2007

Subversion for Apache 2.2.x On Windows Platform

In this post i will talk about how to add Subversion modules on Apache 2.2.x, the problem that i faced while doing this configuration is that SVN executables exist for previous Apache versions only, so i thought about writing this post to give fast summarized steps to prepare SVN for this version of Apache

Enough talking, time to start configuring our Apache, i will do that step by step

1. Choose the latest version of Apache 2.2.x

2. Choose the latest version of SVN(on the time of writing this post, no SVN executable was there to be used with Apache 2.2.x)

3. Install Apache (procedures aren't included in this post) then install SVN

4. Create a repository for your SVN as in that example

mkdir c:\svn
svnadmin create c:\svn\repos


5. Configure Apache server configuration file, you can reach this file through that path

Start/All Programs
/Apache HTTP Server 2.0.50
/Configure Apache Server
/Edit the Apache httpd.conf Configuration File



6. Look for the part of the file that is showing loaded and unloaded Apache modules, and make sure that these two modules are loaded

LoadModule dav_module modules/mod_dav.so
LoadModule dav_fs_module modules/mod_dav_fs.so


7. Download the binary built against Apache 2.2.x from this link

8. Extract the archive you downloaded to a folder and then replace files in your SVN directory folder with these extracted files

9. Go to (SVN directory)/bin and take the two modules

mod_dav_svn.so
mod_authz_svn.so

found there and copy them to (Apache directory)/modules

10. Open Apache configuration file again and add these two lines

LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so

in modules part after the two lines that we added previously in step 6

11. Add at the end of the configuration file these lines



where the path beside the location tag is the path that you will access the SVN through it and the path beside SVNPath is the path where you installed your repository

These are the required steps needed to prepare your SVN with this version of Apache, i hope i was helpful in that article and up to the point


For more details on how to add authentication for SVN and other details follow that link, where you will find other steps related to SVN preparation on Apache Server but for old versions of Apache, skip first part as we already did it and read from the part called "Securing Your Server"