Wednesday, September 21, 2011

Uniqueness email with factory_girl

Use sequence to create valid object with uniqueness constraint.
#Model definition
class User < ActiveRecord::Base
validates :emal, :uniqueness => true
end

#define factory girl
Factory.sequence :email do |n|
  “user_email#{n}@domain.com”
end

Factory.define :user do |user|
  user.name "dongo"
  user.email { Factory.next(:email) }
end

Rails in subdirectory of wordpress

I am developing a rails app. the app is not ready so our product manager requires to use wordpress as main page and the application located in subdirectory of wordpress and let uses try the app via wordpress.

Requirement :


Apache with php and passenger.

I am using apache passenger for deploying rails app. Passenger make  Php and Ruby running smoothly in apache web server.

Below is the summary of what I did to make this happened

Create virtualhost


In /etc/apache2/sites-available/ create file call mysite.com (for simplicity mysite.com should be your domain name) containing the following code:

<VirtualHost *:80>
ServerName mysite.com
DocumentRoot /var/www/wordpress
RailsBaseURI /app
RailsBaseURI /playground
RailsBaseURI /staging
</VirtualHost>

Organise your projects directory


In /var/www directory contains 4 directories as below:

  • wordpress (root directory for wordpress => mysite.com ).

  • app (main rails app  => mysite.com/app ).

  • playground (rails app for playground purpose  => mysite.com/playground ).

  • staging (rails app for staging purpose =>  mysite.com/staging ).


Create symlink


inside /var/www/wordpress create the symlink to app, playground and staging.

ln -s /var/www/app                    /var/www/wordpress/app
ln -s /var/www/playground     /var/www/wordpress/playground
ln -s /var/www/staging            /var/www/wordpress/staging

The syntax is:


ln -s [target_source] [link]

We need symlink because when we access: http://mysite.com/app

then apache will go look for  [webroot]/app where our webroot we configure to be in    /var/www/wordpress/ so it will look for /var/www/wordpress/app

then the operating system serves /var/www/app instead because /var/www/wordpress/app is linked to /var/www/app

 

In case we enable SEO in wordpress then we need to tell apache to escape wordpress every request url starting with app, playground, staging

in /var/www/wordpress  modify .htaccess file as bellow

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !app*
RewriteCond %{REQUEST_FILENAME} !playground*
RewriteCond %{REQUEST_FILENAME} !staging*
RewriteRule . /index.php [L]
</IfModule>

In case if you are working on local machine you need to create hosts. in /etc/ edit hosts file and append the following content to it

192.168.1.100 mysite.com

where :

  • 192.168.1.100 is your computer ip. you can check you computer ip with ifconfig command.

  • mysite.com is your domain name


Tell apache that you want to enable your virtualhost


run the following command  a2ensite mysite.com

where :

  • a2ensite (apache2 enable site ) is the apache utility to enable your virtualhost.

  • mysite.com is the filename of virtual host you created previously in /etc/apache2/sites-available


Reload apache


sudo /etc/init.d/apache2 reload

 

Open up your browser and type :

http://mysite.com should be served by wordpress


http://mysite.com/app rails app.

Done !!!

Check the version of Image magic

In your console type:

> identify --version

 

Tuesday, September 20, 2011

Install ruby and rails.

Installing native libraries:



  • sudo apt-get install curl git zlib1g zlib1g-dev openssl



  • sudo apt-get install libpq-dev


Installing ruby version manager:


bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bashrc

 

Close the terminal and reopen again (reload config) then install:

rvm pkg install zlib
rvm pkg install openssl

 

rvm install 1.9.2
rvm use 1.9.2 --default

 

Thursday, September 15, 2011

Meta keywords in google search engine

I used to use meta keywords extensively in my page. There come the time that I realised that I was totally confused.

Here I found an official article from Google . Google does not use the keywords meta tag in web ranking

visit this http://googlewebmastercentral.blogspot.com/2009/09/google-does-not-use-keywords-meta-tag.html

Wednesday, September 14, 2011

How to copy public key to remote server with just one command

I found that I copy my public key to remote server again and again, then I realise that I need a quicker way to do this.

After googling I found this very useful command.


ssh-copy-id user@remoteserver


ssh-copy-id : is the command

user : is the user of the remote server

remoteserver : is the server of destination

Add remote in mercurial

I need in some point to be able to add remote in hg

To do that go to the root of mercurial open .hg/hgrc and add these entries:

[paths]
default = ssh://hg@bitbucket.org/ilab/resource_mapper

remote1 = ssh://hg@bitbucket.org/ilab/resource_mapper_1


remote2 = ssh://hg@bitbucket.org/ilab/resource_mapper_2




The "default" is the  remote address that will be used when none is specified.

Tuesday, September 13, 2011

Trips for ruby new comers

I found that the following point can trip for ruby new comers:

  • Class definition is an executable code.

  • Only nil and false will be evaluated to false in conditional  statement.

  • A statement always return a value.

  • If the last statement is a value, we don't need the return keywords in a function definition.

  • Use   do  end for multiple line of block and { } same line  for one line code block.

  • { } is optional for hash attribute when it is located in the last parameter.


Next we will show the details of each in the next chapter.

Please comment if you think there is more point that trip you.

Monday, September 12, 2011

SEO rubyonrails

In your model override the to_param method. For instance
class Post < ActiveRecord::Base
def to_param
“#{id}-#{title.parameterize}.html”
end
end

Assume we have a post :
 @post = Post.create! :title => "simple SEO with rubyonrails",
:content => "blah blah ... "

In our view we call
link_to  "Detail", post_url(@post) # "1-simple-SEO-with-rubyonrails"

We get seo url pretty easy.

In our controller we simply call the find or where method as normal since Rails will convert the params[:id] to integer automatically :
@post = Post.where(["id=?", params[:id]) # or @post = Post.find(params[:id])