Saturday, September 10, 2011

Learning Ramaze (1) - Changing title and Background

I would like to learn how to use Ramaze framework. My approach is to start with the default app it creates. Then bit by bit, I modify it to my liking, learning the framework along the way.



1. Resources.


Ramaze: http://www.ramaze.net


2. Create a new Ramaze app.


I would like to create a web app to manage PostgreSQL databases.
So I named my app "dbadmin".
$ cd ~/public_html/apps/                                   # switch to apps folder
$ ramaze create dbadmin                                    # create a ramaze app "dbadmin"
The application has been generated and saved in dbadmin
$ cd dbadmin/                                              # go into the newly created app root
$ ls                                                       # view all default files/directories created
app.rb  config.ru  controller  layout  model  public  spec  start.rb  view
$ ramaze console                                           # launch ramaze console
irb(main):001:0> Ramaze.options.app.name                   # test a ramaze command
=> :pristine                                               # what is pristine?
irb(main):002:0> quit                                      # exit from ramaze console
Ramazement has ended, go in peace.
$
This is the directory of a newly created Ramaze app:























3. Personalizing Ramaze app (Title and Footer).


First, I edit /controller/main.rb to make a title change from "Welcome to Ramaze!" to "DBAdmin".
# /controller/main.7. Now It Looks Like This.rb
#
class MainController < Controller
  def index
    @title = 'DBAdmin'         # 2011.09.10 - changed title
  end

  def notemplate
    @title = 'DBAdmin'         # 2011.09.10 - changed title
    return 'There is no \'notemplate.xhtml\' associated with this action.'
  end
end
Next, I edit /layout/default.xhtml to make changes in footer as follows.
<footer id="footer">
  Copyright &copy;2011 Socrateos | Powered by <a href="http://ramaze.net/">Ramaze</a>
</footer>


4. Personalizing Ramaze app (Background).


I want my background white and most texts black. I want a really simple style.

(A) First, I remove Ramaze's default background (dotted) image.
To do this, I edit /public/css/text.css.
body
{
    <!-- 2011.09.09 - remove default background image
    background:  url('../images/bg.png') repeat top left;
    -->
    border-top:  5px solid #444;
    color:       #444;
    font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, sans-serif;
    font-size:   14px;
}
(B) Second, I want to change the background of the header section
I want to make background color of the header to white, same as the page background.
Since background becomes white, the color of text in the header can no longer be white.
So I edit /public/css/layout.css for the changes.
/* Top part of the website, contains the title and the navigation menu */
#top
{
    /* 2011.09.10 - changed to white backgroud
    background:    #E33F1E;
    */
    background: #fff; 
    height:        70px;
    margin-bottom: 20px;
    padding:       0px 10px;
}

    #top header h1
    {
        /* 2011.09.10 - cannot use white color for text since background is now white
        color:       #fff;
        */ 
        font-size:   38px;
        margin:      10px 0px 0px 0px;
        padding:     0px;
    }
...
            #top nav ul li a
            {
                /* 2011.09.10 - cannot use white color for text since background is now white
                color:           #fff;
                */ 
                display:         block;
                height:          45px;
                padding:         25px 10px 0px 10px;
                text-decoration: none;
            }
In addition, I do not like the square background color when the cursor moves on navigation items.
So I remove it.
...
            #top nav ul li a:hover
            {
                /* 2011.09.10 - remove this color 
                background: #D43919; 
                */ 
            }


5. So far, It Looks Like This.





















6. Personalizing Ramaze app (contents)

Now I modify the contents, both navigation menu bar as well as its body.

(A) Modifying Navigation menu bar.
To make a change to the navigation menus, I edit /layout/default.xhtml. My navigation menu bar has 4 main items: Home, Data, Desin, and System.

...

...

(B) Modifying Body of Contents.
To make changes in the body of contents, I edit /view/index.xhtml.

DBAdmin is a web tool to manage PostgreSQL databases.

Features:

  • Create and Design Databases.
  • View/Add/Edit/Delete Database Records.
  • Generate Reports.
  • Configure System.
DBAdmin is based on Ramaze.


7. Now It Looks Like This.


















8. Summary.

1. To create a skeletal Ramaze app, execute $ ramaze create myapp.
2. To change the page title, edit the value of @title variable in /controller/main.rb.
3. To change the footer, edit the <footer></footer> section in /layout/default.xhtml.
4. To change the background image, edit body{background:...} section in /public/css/text.css.
5. To change the text and background color of the header section, edit edit /public/css/layout.css.
6. To change the navigation menu, edit the <nav></nav> section in /layout/default.xhtml.
7. To change the body contents, edit /view/index.xhtml.

9. Next Step

I only made changes to the main (home) page so far. Next, I want to create other pages: Data, Design, and System pages.
Goto: Learning Ramaze (2) - Adding More Pages

Fixing File System error on startup

I had a bad crash that damaged my filesystem. And my Ubuntu was not starting.
I needed to fix the file system.

1. Start with CD version of Ubuntu.
2. Open the Terminal.
3. List the all disk devices.
$ sudo fdisk -l
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
...
Device   Boot       Start         End       Block   Id  System
/dev/sda1   *           1      121602   976759808    7  HPFS/NTFS
...
Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
...
Device   Boot       Start         End       Block   Id  System
/dev/sdb1               1      121602   976759808    7  HPFS/NTFS
...
Disk /dev/sdc: 1000.2 GB, 1000204886016 bytes
...
Device   Boot       Start         End       Block   Id  System
/dev/sdc1               1      121602   976759808    7  HPFS/NTFS
...
Disk /dev/sdd: 1000.2 GB, 1000204886016 bytes
...
Device   Boot       Start         End       Block   Id  System
/dev/sdd1               1      119825   962492416   83  Linux
/dev/sdd2          119826      121602    14267393    5  Extended
/dev/sdd5          120331      121602    10206208   82  Linux swap / Solaris
/dev/sdd6          119826      120331     4061184   82  Linux swap / Solaris

Partition table entries are not in disk order
My Ubuntu is installed on the 4th disk (/dev/sdd), which is damaged.

4. Fix the 4th Disk
$ sudo umount /dev/sdd1      # unmount just in case
$ sudo fsck /dev/sdd1        # fix errors
...                          # hit y(es) several times to allow fixing..

It worked.I was able to start Ubuntu as usual.

Sunday, September 4, 2011

Setting up User for PostgreSQL

I do not like using superuser 'postgres' as daily database management.
So I want to add my account name 'socrateos' as a postgresql user.

$ su postgres                                             # switch to user postgres
$ psql                                                    # launch psql client
psql (9.0.4)
postgres=# CREATE ROLE socrateos WITH CREATEDB CREATEROLE LOGIN PASSWORD 'secret'; # a new user
CREATE ROLE
postgres=# CREATE DATABASE testdb;                        # create a new database
CREATE DATABASE
postgres=# \q                                             # quit psql
$ exit                                                    # exit from user postgres 
exit
$ whoami                                                  # be sure I am back to myself
socrateos                                                 # yes, I am in my own account
$ psql testdb                                             # now login to postgresql as myself
testdb=> CREATE DATABASE mydb;                            # I can create a database
CREATE DATABASE
testdb=> \c mydb;                                         # connect to the newly created database
You are now connected to database "mydb".
mydb=> CREATE TABLE table1(id SERIAL PRIMARY KEY, name VARCHAR(32)); # test creating a table
CREATE TABLE
mydb=> INSERT INTO table1(name) VALUES('john');           # insert a row
INSERT 0 1
mydb=> INSERT INTO table1(name) VALUES('mary');           # insert another row
INSERT 0 1
mydb=> SELECT * FROM table1;                              # list all rows
 id | name 
----+------
  1 | john
  2 | mary
mydb=> \q                                                 # done with the test and quit psql

OK, so I think I am ready to manage PostgreSQL without using superuser 'postgres'.

Saturday, September 3, 2011

Installing Zen CMS on Ubuntu (11.04)

I stumbled upon a new CMS based on Ruby/Ramaze. It is called Zen (http://zen-cms.com/). Since I have recently switched to Nginx/Unicorn Web environment and found that Ramaze works without any hick up in that environment, I decided to check it out. The following is how I installed it.

1. Resources.
Zen • The Modular CMS: http://zen-cms.com/
Ramaze: http://ramaze.net/
Running a Rack app (Ramaze) with Nginx/Unicorn: http://socrateos.blogspot.com/search/label/unicorn

2. Requirements.
Ruby 1.9.2 (MRI) - This is my default ruby.
Ramaze 2011.01.30 - I recently installed on my Ubuntu
A SQL database supported by Sequel - I am using PostgeSQL.
A Rack server (Thin, Unicorn or Mongrel) - I am using Unicorn.
Libraries required for Nokogiri: libxml2, libxml2-dev, libxslt-dev (libxslt1-dev was one that I found and installed, using Synaptic)

3. Installation.
$ sudo gem install zen
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.5.0
Successfully installed loofah-1.0.0
Successfully installed zen-0.2.8
3 gems installed
Installing ri documentation for nokogiri-1.5.0...
Installing ri documentation for loofah-1.0.0...
Installing ri documentation for zen-0.2.8...
Installing RDoc documentation for nokogiri-1.5.0...
Installing RDoc documentation for loofah-1.0.0...
Installing RDoc documentation for zen-0.2.8...

4. Create Zen Application
$ zen create app2
The application has been generated and saved in app2
$ cd app2/
$ ls
Rakefile  app.rb  config  config.ru  log  public  start.rb  task  theme

5. Setup Config.
The following is my settings for database.rb.
Zen.database = Sequel.connect(
  :adapter  => 'postgres',
  :host     => 'localhost',
  :username => 'socrateos',     # I have createdb, createrole rights
  :password => 'secret',
  :database => 'app2',
  :test     => true,
  :encoding => 'utf8',
  :logger   => Ramaze::Logger::RotatingInformer.new(
    __DIR__("../log/database/dev"), '%d-%m-%Y.log'
  )
)

# IMPORTANT, when running MySQL the engine should be set to InnoDB in order for
# foreign keys to work properly.
if Zen.database.adapter_scheme.to_s.include?('mysql')
  Sequel::MySQL.default_engine = 'InnoDB'
end

6. Failed to Rake DB:Migrate
I ran rake db:migrate. But it failed.
$ rake db:migrate --trace
W [2011-09-06 22:57:42 $2563]  WARN | : Failed to migrate the settings, make sure the database table is up to date
** Invoke db:migrate (first_time)
** Execute db:migrate
rake aborted!
PGError: ERROR:  ...
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:186:in `async_exec'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:186:in `block (2 levels) in execute'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/logging.rb:32:in `log_yield'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:186:in `block in execute'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:166:in `check_disconnect_errors'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:186:in `execute'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:256:in `block (2 levels) in execute'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/connection_pool/threaded.rb:71:in `hold'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/connecting.rb:226:in `synchronize'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:256:in `block in execute'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:277:in `check_database_errors'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/adapters/postgres.rb:254:in `execute'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/query.rb:71:in `execute_dui'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/query.rb:64:tin `execute_ddl'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/schema_methods.rb:372:in `create_table_from_generator'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/schema_methods.rb:97:in `create_table'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/extensions/migration.rb:629:in `schema_dataset'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/extensions/migration.rb:382:in `initialize'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/extensions/migration.rb:542:in `initialize'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/extensions/migration.rb:333:in `new'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/extensions/migration.rb:333:in `run'
/var/lib/gems/1.9.1/gems/zen-0.2.8/lib/zen/task/db.rake:28:in `block (4 levels) in '
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/query.rb:231:in `_transaction'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/query.rb:210:in `block in transaction'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/connection_pool/threaded.rb:84:in `hold'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/connecting.rb:226:in `synchronize'
/var/lib/gems/1.9.1/gems/sequel-3.26.0/lib/sequel/database/query.rb:208:in `transaction'
/var/lib/gems/1.9.1/gems/zen-0.2.8/lib/zen/task/db.rake:27:in `block (3 levels) in '
/var/lib/gems/1.9.1/gems/zen-0.2.8/lib/zen/task/db.rake:12:in `each'
/var/lib/gems/1.9.1/gems/zen-0.2.8/lib/zen/task/db.rake:12:in `block (2 levels) in '
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:90:in `block (2 levels) in top_level'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:90:in `block in top_level'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:62:in `block in run'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/var/lib/gems/1.9.1/gems/rake-0.9.2/bin/rake:32:in `'
/var/lib/gems/1.9.1/bin/rake:19:in `load'
/var/lib/gems/1.9.1/bin/rake:19:in `
' Tasks: TOP => db:migrate