Friday, March 16, 2012

An Intoduction into FreeBSD ports

Under Linux Platform we usually use rpm ,up2date ,yum or apt-get command to install a package.
Under FreeBSD you can use pkg_add command or ports system.

The FreeBSD Ports Collection is a package management system that provides an easy and consistent way of installing software packages on the FreeBSD. Usually ports is located at /usr/ports directory.

The FreeBSD Ports and Packages Collection offers a simple way for users and administrators to install applications. There are currently 23322 ports available.
Installing an application is as simple as downloading the port, unpacking it and typingmakein the port directory. However, the most convenient (and common) method is to download the framework for the entire list of ports by installing the entire ports hierarchy at FreeBSD installation time, and then have thousands of applications right at your fingertips.

Task: Find out FreeBSD port name or package name

There are 3 different methods available to search a port name. Use any one of the following method only.

#1 : Using whereis command

If you know the exact name of the port, but just need to find out which category it is in, you can use the whereis(1) command. Simply type whereis file, where file is the program you want to install.
# whereis php5
Output:
php5: /usr/ports/lang/php5
# whereis lighttpd
Output:
lighttpd: /usr/ports/www/lighttpd

#2: Using make command

Change directory to /usr/ports
# cd /usr/ports
To search type the command as follows:
# make search name="package-name"
For example search a package called lighttpd or php
# make search name="php"
OR
# make search name="lighttpd"
Output:
Port:   lighttpd-1.4.13_2
Path:   /usr/ports/www/lighttpd
Info:   A secure, fast, compliant, and very flexible Web Server
Maint:  mnag@FreeBSD.org
B-deps: autoconf-2.59_2 libtool-1.5.22_4 m4-1.4.8_1 pcre-7.0_1 perl-5.8.8
R-deps: pcre-7.0_1
WWW:    http://www.lighttpd.net/

#3: Using locate command

You can also use locate command:
# locate php
# locate php | grep php5

Task: Install FreeBSD port

Above output displays port Path - /usr/ports/www/lighttpd. Just change directory to /usr/ports/www/lighttpd
# cd /usr/ports/www/lighttpd
Now install a port:
# make; make install
Clean the source code tree:
# make clean
 Thanks & HAppp Porting..............!

An Introduction to Ruby on Rails

Since its debut in 2004, Ruby on Rails has rapidly become one of the most powerful and
popular frameworks for building dynamic web applications.

What makes Rails so great? First of all, Ruby on Rails is 100 percent open-source,
available under the permissive MIT License, and as a result it also costs nothing to
download and use. Rails also owes much of its success to its elegant and compact design;
by exploiting the malleability of the underlying Ruby language, Rails effectively creates
a domain-specific language for writing web applications.

The Folder Structure of an ROR  Application is shown below ..

app/
Core application (app) code, including models, views, controllers, and helpers
onfig/
Application configuration
db/
Files to manipulate the database
doc/
Documentation for the application
lib/
Library modules
log/
Application log files
public/
Data accessible to the public (e.g., web browsers), such as images and
cascading style sheets (CSS)
script/rails
A script provided by Rails for generating code, opening console sessions,or starting a local web server
test/
Application tests (made obsolete by the spec/ directory in Section3.1.2)
tmp/
Temporary files
vendor/
Third-party code such as plugins and gems
README
A brief description of the application
Rakefile
Utility tasks available via the rake command
Gemfile
Gem requirements for this app
config.ru
A configuration file for Rack middleware
.gitignore Patterns for files that should be ignored by Git

Thursday, March 15, 2012

Installing Mysql 5.1 on FreeBSD

Now its turn for configuring MySQL

We all aware of Mysql and a lot of times it is the first things we install on a server. I recently tried it on my FreeBSD server and it went well.
You can install mysql from the FreeBSD Ports Collection.

cd /usr/ports/databases/mysql51-server make install clean

Then you will have to wait until the package builds itself from source.
* please wait it will take some time to complete...refresh  your eyes...have a chat with your friend...:)-
Once that is done you will want to use mysql’s install script to get you up and running:
/usr/local/bin/mysql_install_db
Make the mysql directory owned by the user, ’mysql’.

chown -R mysql /var/db/mysql/ chgrp -R mysql /var/db/mysql/
chown changes the user and/or group ownership of each given file, according to its first non-option argument, which is interpreted as follows. If only a user name (or numeric user ID) is given, that user is made the owner of each given file, and the files' group is not changed. If the user name is followed by a colon or dot and a group name (or numeric group ID), with no spaces between them, the group ownership of the files is changed as well. If a colon or dot but no group name follows the user name, that user is made the owner of the files and the group of the files is changed to that user's login group. If the colon or dot and group are given, but the user name is omitted, only the group of the files is changed; in this case,chownperforms the same function aschgrp.

Run mysql as the ’mysql’ user.
/usr/local/bin/mysqld_safe –user=mysql

Then you will want to set the Mysql root password:

/usr/local/bin/mysqladmin -u root password YoUrPaSSwoRd
Start Mysql on Boot
You will probably want Mysql to start on boot. To enable Mysql on boot you will need to add a line to your rc.conf file.

vi /etc/rc.conf
Then add the following lines near the bottom:

# Enable Mysql mysql_enable="YES"
Thats it! You should have a working installation of mysql and have it configured to load up on boot.

TEAMPHP: Installing Ruby on Rails in a FreeBSD Server

TEAMPHP: Installing Ruby on Rails in a FreeBSD Server: At First you have to Install ruby # pkg_add -r -v rubyusing the following command For Checking what version of Ruby installed # ru...

Installing Ruby on Rails in a FreeBSD Server


At First you have to Install ruby

# pkg_add -r -v rubyusing the following command

For Checking what version of Ruby installed
# ruby –version

* (‘#’ denotes running command as root; ‘>’ denotes running command as non privileged user).
Gems isn’t installed as default, so the next task is putting that in. A simple

“make search name=gems”

shows the gem port in /usr/ports/devel/ruby-gems.

Following the standard “make install clean” actions it will put the latest version on your machine.
#gem –version

Now you’re good to go with putting on the specific gems to get Rails apps running. We begin of course with rails itself.

# gem install  rails

This gives you a collection of gems that comprise the rails framework, and the Ruby documentation pages as well. For Installing gems you required, you can follow the below syntax.

# gem install mysql

# gem install authlogic

Rake is the Rails functionality that sets up your database schema and needs to be present for the application configuration stage.Getting that installed is done by:
# gem install rake

You Can see the list of gems installed using the command

# gem listNow we can install Apache2.2

# pkg_add -r -v apache22

Install Passenger

# pkg_add -r -v rubygem-passenger

Add lines created by running the above commands to LoadModule passenger with apache
# nano /usr/local/etc/apache2/httpd.conf
add the following lines in the httd.conf file:


LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.2/ext/apache2/
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.2
PassengerRuby /usr/local/bin/ruby18


*change the pasenger version as per your..

Restart apache after making changes to the configuration file for any reason, you will need to restart the server:
# /usr/local/sbin/apachectl restart


You can start the server by typing:
/usr/local/sbin/apachectl start


You can stop the server at any time by typing:
/usr/local/sbin/apachectl stop


To restart Apache without aborting current connections, run:
# /usr/local/sbin/apachectl graceful.



Now you can create and run  your ROR application ...

If You have n't installed the MySQL, then refer my next article on how to do that....

Happy COding....

yii: drop down list filter in CGridView

BY default the filter for each data column is a text field. to display the field as drop down list, CDataColumn::filter property must be set as an array.

for example, for user table there's column for sex represented by 1 = male and 2 = female. in the view file:

PHP:
<?php 
$this->widget('zii.widgets.grid.CGridView'array(
    'id'=>'user-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'id',
        'name',
        'email',
        array(
            'name' => 'sex',
            'filter' => array(=> 'Male'=> 'Female'),
        ),
        array(
            'class' => 'CButtonColumn',
        ),
    )
);
 ?>

note lines 10 to 13 from the above code.

note for the array data: the array index is used for value attribute in <option> tag, and the array value is the display text.

data from a model


say if a model has attributes id and code, and you want to list codes in drop down list.

in model, define a method to return array of data:

PHP:
class Code extends CActiveRecord {
...
    public function getCodeArray() {
        $codes self::model()->findAll();
        $data array(); // data to be returned
        foreach ($codes as $c) {
            $data[$c->id] = $c->code;
        }
        return $data;
    }
...
}

in view file, replace filter of the column with the following code:

PHP:
...
    'filter' => Code->getCodeArray(),
...

Prawn: The Pure Ruby PDF Generation Library

Create and Install
Note: I’m assuming Rails 2.1+. Fire up terminal and create a new app and install the prawnto plugin.

rails prawn_demo
sudo gem install prawn
script/plugin install git://github.com/thorny-sun/prawnto.git

Now add prawn as a dependency in environment.rb.

config.gem 'prawn'

For the sake of the demo we’ll need some demo data. Let’s create a Book model with some columns that we can shove lorem ipsum into.

script/generate scaffold book title:string author:string description:text
rake db:migrate

BooksController#show Example


I ran over to The Pragmatic Programmer’s site and snagged a few titles to enter as test data. Let’s get started with something simple and add a pdf version of the show action. Open up the books controller and add format.pdf { render :layout => false } so that the action looks something like this:

def show
  @book = Book.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @book }
    format.pdf { render :layout => false }
  end
end

Now if you visit http://localhost:3000/books/2.pdf, you’ll get a template is missing error. Let’s add the show view. The prawnto plugin adds all the wiring, so all you need to do is create the show.pdf.prawn file inside app/views/books. For now, lets hello world that mofo with the following:

pdf.text "Hello World!"

Now if you revisit that url, you’ll get a pdf that says hello world. Simple, eh? Let’s make the view specific to the book and tweak the look a bit.

pdf.font "Helvetica"
pdf.font.size = 13
pdf.text "Book: #{@book.title}", :size => 16, :style => :bold, :spacing => 4
pdf.text "Author: #{@book.author}", :spacing => 16
pdf.text @book.description

The first two lines set the default font stuff. As you can see, pdf.text takes a string and then a hash of options. :size adjust the font size, :style changes the weight, and :spacing controls the space between lines. Pretty self-explanatory, but I thought I would cover it anyway.

BooksController#index Example


Now let’s create a pdf of all the books with each book on it’s own page. Add format.pdf { render :layout => false } to the respond to block in the index action and create the following index.pdf.prawn view:

pdf.font "Helvetica"
pdf.font.size = 13

@books.each do |book|
  pdf.text "Book: #{book.title}", :size => 16, :style => :bold, :spacing => 4
  pdf.text "Author: #{book.author}", :spacing => 16
  pdf.text book.description
  pdf.start_new_page
end

So the examples I showed were pretty basic and there is a lot more you can do with prawn, but I didn’t feel like coming up with examples. Helpers work just like in views which is handy. Also, prawn does images and data tables in a pretty simple manner. Check out the prawn and prawnto websites for more.

How can pagination be done in ruby on rails 3.1?

The more recent kaminari gem can be used in Rails 3. You can refer https://github.com/amatsuda/kaminari
it is very simple to use.
Put this line in your Gemfile:
gem 'kaminari'
Then do bundle install
Typically, your controller code will look like this:
@users = User.order(:name).page params[:page]
On the views Just call the paginate helper:
<%= paginate @users %>
Now you can see the paginated result..