Showing posts with label Php. Show all posts
Showing posts with label Php. Show all posts

Tuesday, November 29, 2011

nl2br, strtr php functions in ruby

For those who are php code know very well how important is strtr and nl2br in the web development.


When developing web application in rails I still need these 2 functions. so I dicide just to keep and implement the same name in ruby:
string.rb


class String
def str_translate values, preserve = false
self.gsub /\{[^\}]*\}/ do |match|
key = match[1..-2]
if preserve
values[key.to_sym] || match
else
values[key.to_sym] || ""
end
end
end

def nl2br
self.gsub /\n/ do |match|
<br />
end
end
end

string_spec.rb


require "spec_helper"

describe String do
describe "#str_translate" do
it "should be translated keys in the string by parameters in the list " do
str = "The message from {user} sent out on {day} "
strtr = str.str_translate(:user => "Ilab", :day => "Monday")
strtr.should eq "The message from Ilab sent out on Monday "
end

it "should preserve keys that not in the string by parameters in the list " do
str = "The message from {user} sent out on {day} "
strtr = str.str_translate({:day => "Monday"}, true)
strtr.should eq "The message from {user} sent out on Monday "
end

it "should remove the missing key from the string" do
str = "The message from {user} sent out on {day} "
strtr = str.str_translate(:day => "Monday")
strtr.should eq "The message from sent out on Monday "
end
end

describe "nl2br -new line to br" do
it "should convert all new line to br" do
original = ["\nthis is a great new\nIn the \n", "blah\n\n\nblah\n"]
converted = ["<br />this is a great new<br />In the <br />", "blah<br /><br /><br />blah"]
original.each_with_index do |str, index|
str.nl2br.should eq converted[index]
end
end
end
end

Wednesday, October 12, 2011

Rubyonrails contains php application in its subdirectory

There are many great web software built in PHP . At some point we need to integrate  one of them with our Rubyonrails app.

This tutorial will guide you how to do this. As an example I will use Wordpress since it is one of the most popular one. This can also be applied to any Php application.

Create project



  • Create an Rails project called "venta-facil.com.kh" in /var/www



  • Create a directory /var/www/venta-facil.com.kh/public/blog


 

this will allow you to access wordpress by http://yourdomain.com/blog . Change to whatever you want your url. I will keep highlighting the word blog so you can keep it consistent.

 

Go to wordpress.org download and extract the files to /var/www/venta-facil.com.kh/public/ blog . This directory is the root of your wordpress.

Create virtual host


Let start configuring virtual host. In your terminal   sudo nano /etc/hosts and add the following

127.0.0.1 venta-facil.com www.venta-facil.com

save it. I used my local ip 127.0.01. You can replace it to your web server ip. make sure the file is saved.

Configure your web server


Create a file /etc/apache2/sites-available/venta-facil.com.kh

<VirtualHost *:80>
ServerName venta-facil.com.kh
DocumentRoot /var/www/venta-facil.com.kh/public
<Directory /var/www/venta.com.kh/public>
AllowOverride all
Options -MultiViews
</Directory>

<Location /blog>
PassengerEnabled off
</Location>

</VirtualHost>

 

We need to tell apache to enable this virtualhost with the following command:    sudo a2ensite venta-facil.com.kh

and then reload apache with the following commant: sudo /etc/init.d/apache2 reload

 

Fire it by typing this: http://www.venta-facil.com.kh/blog in your web browser

 

Done !!!