Sunday, March 25, 2012

How to center a Div on the screen using JQuery

Centering a Div on the screen (both horizontally & vertically) is truly painful when depending only on CSS, So I'm trying here to make this functionality using JQuery,

first: add the 'center' function to JQuery :
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) +
        $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) +
        $(window).scrollLeft() + "px");
    return this;
}

second: use this function wherever you want:
$('#id').center();

and that is it :)


you can try this code here http://jsfiddle.net/DerekL/GbDw9/

Sunday, February 12, 2012

Installing Rails 3.1.1, ruby 1.9.3, apache2, Passenger and Mysql on Ubuntu 11.10

To install Rails 3.1 on  fresh Ubuntu 11.10 I followed the following steps.


Note:
Currently I am running Ubuntu on a Virtual Machine (VMware Player )


Step 1: Install  libYAML from source
cd /usr/src
sudo wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
sudo tar xvzf yaml-0.1.4.tar.gz
cd yaml-0.1.4
sudo ./configure --prefix=/usr/local
sudo make 
sudo make install


Step 2: Install  required libraries
 sudo apt-get install libxml2-dev libxslt-dev libzlib1g zlib1g-dev build-essential openssl libssl-dev libmysqlclient16-dev


Step:3 Install ruby  from source file
cd /usr/src
sudo wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.gz
sudo tar xvzf ruby-1.9.3-p0.tar.gz
cd ruby-1.9.3-p0
sudo ./configure --prefix=/usr/local --enable-shared --disable-install-doc --with-opt-dir=/usr/local/lib
sudo make
sudo make install


Step:4 Test proper ruby is installed
sudo ruby -v
The above command should display some thing as follows
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]


Step 5: Install ruby gem
cd /usr/src 
sudo wget http://rubyforge.org/frs/download.php/75475/rubygems-1.8.11.tgz
sudo tar xvzf rubygems-1.8.11.tgz
cd rubygems-1.8.11
sudo ruby setup.rb


Step 6: Install Mysql server and Mysql client
sudo apt-get install mysql-server-5.1 mysql-client-5.1

Step 7: Install rails
sudo gem install rails


Step 8: Install apache2
sudo apt-get install apache2


Step 9: Install passenger
cd /usr/src 
sudo wget http://rubyforge.org/frs/download.php/75337/passenger-3.0.9.tar.gz
sudo tar xvzf  passenger-3.0.9.tar.gz
cd passenger-3.0.9
sudo ./bin/passenger-install-apache2-module  
Follow the instruction provided by the installer and restart the apache


Step 10: Install Node.js 
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs-dev


Step 11: Install readline library 
sudo apt-get install libreadline6-dev
cd /usr/src/ruby-1.9.3-p0/ext/readline
sudo ruby extconf.rb
sudo make
sudo make install


Step 12: Testing rails application
 a. create a new rails project. My project directory is /var/projects
     cd /var/projects
   sudo rails new demo --database=mysql
 b. create mysql database
     sudo mysql -u root -p 
     It prompts mysql password and provide the one which you provide at the time of
      installing Mysql server
     mysql> create database demo_developement;
  mysql> exit


c. Install  vim editor (optional) if  you are fond of vim
    sudo apt-get install vim vim-rails


 d. Update database.yml file
     cd /var/projects/demo
      Open database.yml and update 'password' attribute by using following command
      sudo vim config/database.yml


 e. Update apache configuration file and  update the virtual host entry shown below and restart
    sudo vim /etc/apache2/sites-enabled/000-default


  <VirtualHost *:80>
      ServerName localhost
      DocumentRoot /var/projects/demo/public
      RailsEnv development
      <Directory /var/projects/demo/public>
         AllowOverride all
         Options -MultiViews
      </Directory>
   </VirtualHost>
  sudo /etc/init.d/apache2 restart
f. Create log files
  sudo touch /var/projects/demo/log/development.log

Note:
1. You may get permission denied error something like
    "Permission denied - /var/projects/demo/tmp/cache/assets/CF0"
    To resolve this just give read and write permission for this folder. You can use the following command for   
     development environment
    sudo chmod 777 /var/projects/demo/tmp/cache/assets

2. To view apache log files you can use the following command
  sudo tail -f /var/log/apache2/error.log -n 100
     where "-n 100" denotes last 100 lines of error.log file

3. For installing Rails using RVM follow this blog


Reference:

Sunday, February 5, 2012

Some useful Git commands and resources



Git Cheat Sheets


Enable GIT colors commands

  • git config --global color.diff auto
  • git config --global color.status auto
  • git config --global color.branch auto
  • git config color.interactive auto
  • Or, you can set all of them on with the color.ui option:
  • git config color.ui true

Creating & pushing new branch

  • First, you must create your branch locally
  • git checkout -b your_branch
  • push the branch to the remote repository origin and tracks it
  • git push -u origin your_branch

listing all files in a commit:

$ git show --pretty="format:" --name-only bd61ad98

Restoring a deleted file in a Git repo:

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

Wireless enabling on Ubuntu

just install b43-fwcutter:
  • sudo apt-get install b43-fwcutter

working with DateTime instances in Doctrine 2

DateTime changes are detected by Reference

When calling EntityManager#flush() Doctrine computes the changesets of all the currently managed entities and saves the differences to the database. In case of object properties (@Column(type=”datetime”) or @Column(type=”object”)) these comparisons are always made BY REFERENCE. That means the following change will NOT be saved into the database:
<?php
/** @Entity */
class Article
{
    /** @Column(type="datetime") */
    private $updated;

    public function setUpdated()
    {
        // will NOT be saved in the database
        $this->updated->modify("now");
    }
}
The way to go would be:
<?php
class Article
{
    public function setUpdated()
    {
        // WILL be saved in the database
        $this->updated = new \DateTime("now");
    }
}​

Refere​nce:
http://www.doctrine-project.org/docs/orm/2.0/en/cookbook/working-with-datetime.html

Multiple virtual hosts with Apache (on windows)

Creating multiple virtual hosts using apache is so common, you may find making it so easy and straight forward on Linux or even Mac, but believe me it is not the case with Windows !​


   Here is the deal, the idea behind how to make this is to give another IP for your new host, and make apache listen on that IP, let's do it:

Step 1: add your new host in the hosts file under "C:\Windows\System32\drivers\etc​" and assign new IP to it:
  1. 127.0.0.1       localhost  
  2. 127.0.0.2       mysite.com  
Step 2: make your apache server listens on the new IP by modifying httpd.conf file in the "apache\conf" folder, put the following in the Listen section:
  1. Listen 80  
  2. Listen 127.0.0.2:80  
Step 3: and finally, open "apache\conf\extra\httpd-vhosts.conf" and add your new virtual hosts like this:
  1. <virtualhost mysite.com:80>
  2. DocumentRoot "absolute_location_to_mysite_public_folder"
  3. ServerName Boston.com
  4. ServerAlias boston.com
  5. <directory "absolute_location_to_mysite_public_folder">
  6. AllowOverride All
  7. Options Indexes FollowSymLinks
  8. Order allow,deny
  9. Allow from all
  10. </directory>
  11. </virtualhost>

  12. <virtualhost localhost:80>
  13. DocumentRoot "apache_htdocs_folder"
  14. ServerName localhost
  15. ServerAlias localhost
  16. <directory "apache_htdocs_folder">
  17. AllowOverride All
  18. Options Indexes FollowSymLinks
  19. Order allow,deny
  20. Allow from all
  21. </directory>
  22. </virtualhost>








Finally, you can use the two hosts in your browser:
  • localhost
  • mysite.com

Problem running Zend commands inside NetBeans (solved)

When trying to run zf commands inside Netbeans
it didn't work until I did the following:
    1. ​Using cmd I created the .zf.ini file using the script​ "zf --setup fonfig-file"
      1. .zf.ini is in your user directory like in windows: "C:\Users\abdelhady_mohammed"
    1. Then I opened netbeans and from tools>Options>php>Zend, then choosed "Register Provider"
    2. Right click the project Zend>run command and choose "Refresh Commands"
And here we go, every thing is Ok inside Netbeans

Then I tried to run zf commands again on windows command line:
And here comes the bug, zf commands is no longer runnable on windows command line and here is the full bug report on Netbeans site:

Solution:
get the "NetBeansCommandsProvider.php" located in "D:\Boston_Dev_Env\NetBeans\php\zend"
and copy it to the Zend Library folder "D:\Boston_Dev_Env\server\ZendFramework-1.11.7\library"

and every thing will be fine then

Additional reference:
http://www.kilinjal.com/blog/how-configure-zend-tool-netbeans?page=show