Sat Aug 02 21:54:00 UTC 2008

Introducing Summer Reboot - Radiant Documentation

Posted in Radiant CMS, Ruby on Rails at 09:54 PM by mohits

According to their website, Radiant CMS is a no-fluff, open source content management system designed for small teams. In my opinion, it is quite cool and offers a clean codebase, a fantastic extensions system and has a very good community.

The Radiant community has just embarked on a joint project to create a set of documents for Radiant. The project started near the end of June, based on a rough table of contents that would read like a book. It’s hosted off the wiki for the time being. Now, it’s the start of August, so I had sent out a brief update and request to people. This is the update.

[1] As you (may) know, Summer Reboot is targeted as being the starting point documentation for Radiant in the future and so far, we’ve come up with a list of documents that we’d like to create and some authors have claimed some of the articles as things that they would like to write. A few articles are already up!

If you feel there should be more topics, please edit the home page of the documentation to add it in

If you feel you can write some of it, please claim, write and upload

Current authors/ claimants: THANKS for your support.

[2] I understand that some (many? most?) of us are busy and are doing this as a side-project/ free time effort, so I’m thankful to all of you for that. That said, if you think you have some scribbled notes electronically but don’t have the time to upload it, I’m willing to try to do that. I don’t mind spending some time to push out more things. I’ll rewrite what I get, try to replicate it on my side, capture some screenshots and push it out as and when I have the time.

Contact me: tech {at} onghu.com or mohits {at} onghu.com with what you have/ email. If you can do this TODAY (i.e. Friday), I shall try my best to push out what you pass me during the weekend. Once the work week starts again, it gets difficult :(

[3] Everyone reads :) Help us edit the wiki. Go to the site and see the articles that are there. Take a read.

If there are grammatical errors/ missing words, please FIX it

If the explanation is not clear and you can rewrite a part of it, please FIX it

If more information is needed and you can write it, please FIX it

If more information is needed and you can’t write it, please ADD comments into the article, preferably in a color like orange to distinguish it from the main text. Hopefully, someone can address it later.

[4] Ideas from the List: This list has some good ideas and bug fixes or workarounds. If you have the time and spot something that really helps you, go to the wiki and add in at least a small note to the related article. For example, the article on Copy_Move at http://wiki.radiantcms.org/Copy_and_Move has this line at the bottom:


> Known Problems
> There seems to be a problem with using CopyMove with Radiant 0.6.7 in the Development mode.
> This problem does not occur when in Production mode.
> A sample screenshot of the problem is at: http://www.ruby-forum.com/attachment/2380/Picture_2.jpg

If you can, please add such stuff so that people know where to look and when not to panic on ‘known problems’

We’re targeting 31st Aug as the date of launch!!

The Summer Reboot site is at: http://wiki.radiantcms.org/Summer_Reboot

Sat Oct 27 12:22:00 UTC 2007

Online books about Ruby/ Rails

Posted in Ruby, Ruby on Rails at 12:22 PM by mohits

Here’s a small list of online/ free books about Ruby/ Rails. This is just to keep the references in one place. Hope this helps someone else – it certainly helps me!

The PickAxe Edition 1 (Programming Ruby)

http://www.rubycentral.com/pickaxe/

Why’s Poignant Guide about Ruby

http://poignantguide.net/ruby/

Mr. Neighborly’s Humble Little Ruby Book

http://www.humblelittlerubybook.com/

A Little Ruby, A Lot of Objects

http://www.visibleworkings.com/little-ruby/

Quick References on the Language

http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html

http://www.math.hokudai.ac.jp/~gotoken/ruby/ruby-uguide/uguide08.html

Mon Sep 10 22:28:00 UTC 2007

Radiant CMS - Showing the date of articles

Posted in Radiant CMS, Ruby on Rails at 10:28 PM by mohits

One of my sites (http://tec.onghu.com/) is powered by the Radiant CMS. As I continue to build that site, I shall put up some information about how simple things are done in Radiant.

One of the things that I have is a running list of articles that have been published on different dates. I wanted a simple way to print out the date next to the articles.

Here’s what you need to do for that:



<ul>
<r:find url="articles">
<r:children:each order="desc" status="published">
   <r:unless_content part="no_menu"><li><r:link /> [<r:date format="%b %d, %Y" />] 
</r:unless_content></r:children:each>
</r:find>
</ul>

Notice 2 interesting things in the code sample above.

  1. r:date format – this expects a formatting string similar to what strftime in Ruby/ Rails expects.
  2. r:unless_content part=”no_menu” – this tag is a bit that excludes some pages from being shown in the listing. Any page that has a part called ‘no_menu’ will not be listed. This is a common way to prevent CSS files from showing in the list.

That’s all there is to it. Leave a comment if you want any bit above clarified.

Sat May 26 13:55:00 UTC 2007

"Hello World" using Radiant CMS

Posted in Radiant CMS, Ruby on Rails at 01:55 PM by mohits

If you’re as slow at getting started as I can be at 2:45AM and still insist on trying out Radiant CMS at exactly that time, this small entry may help you getting around to making your first page with Radiant show up.

Pre-requisites

1. Install Radiant as per the instructions

2. Create one project for any of the blog types (this is where we shall steal from) – remember to create the database and adjust the database properties in the database.yml

3. Create your ‘empty project’ – remember to create the database and adjust the database properties in the database.yml

If there’s a need, I could elaborate the first 3 steps further but I think it should be quite easy to follow those steps through.

Adding to the Empty Project

Now, let’s go through the rest. If you read the Radiant mini handbook you will see that it mentions that if you are starting with an empty project, every thing needs to be created – right from the first few layouts, the snippets and of course, the pages. The ‘empty project’ is just that: empty.

Since it took me a while to figure out how to create the first Radiant page, I decided to document that process here for reference.

If you have created a Radiant site, run the bootstrap rake script, started the server, and are logged in, we’re ready to roll.

Step 1: Create a new layout

Click on the “Layouts” tab. This brings up an empty list of layouts.

Then, click on “New Layout”. Give your new layout a name such as “page_style” and fill in stuff that you can copy from a standard HTML web page. Ideally, you should also use some Radius tags in the creation. To cut short the guesswork at this point, I basically stole from the Blog Radiant project that I had installed. This is the cut down code that I copied from there.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
  <head>
    <title><r:title /></title>
    <link href="/rss/" rel="alternate" title="RSS" type="application/rss+xml" />
    <link rel="stylesheet" type="text/css" href="/styles.css" />
  </head>
  <body>
    <div id="page">
      <div id="main">
        <div id="content-wrapper">
          <div id="content">
            <r:unless_url matches="^/$"><h1><r:title /></h1></r:unless_url>
            <r:content />
            <r:if_content part="extended">
            <div id="extended">
              <r:content part="extended" />
            </div>
            </r:if_content>
            <r:if_url matches="^/articles/\d{4}/\d{2}/\d{2}/.+"><r:unless_url matches="-archives/$"><p class="info">Posted by <r:author /> on <r:date /></p></r:unless_url></r:if_url>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

You could also use Snippets, but we’re not going to go there yet. Click on “Create Layout” to, err, create the layout. It should now show the “page_style” layout in the list of layouts.

Step 2: Create a layout for the stylesheet

Click on “New Layout” and give it a name such as “stylesheet”. Then, click on the “More” link below the Name field and enter text/css in the Content-type field there. In the Body of the layout, simply enter the following which means that the content of the file that uses this layout is passed up.


<r:content />

After you click on “Create Layout” it will be saved and will be shown in the list.

Step 3: Add the main page

You already have a layout that can be used as the base layout for pages in your site. Go to the “Pages” tab of your admin site and click on “New homepage” to create the first page. Give the page a title, something like “Hello, World” and then fill out things that will be rendered as the page. I have set the formatting filter to “Textile” and added some text as below


h1. Hello!

This is the first page in the sample site.  
It uses Radiant and uses @Textile@ for the markup.

h2. Creating a List

Using @Textile@, a list is easily created.  I'd like to say Hello to

 * My family
 * My friends
 * My world

h1. Another Level 1 heading


Now, in the bottom bar, select the Layout as “page_style”, the Page Type as “” and the Status as “Published”. With that done, you can click in the top right corner to “View Site” – it will bring up a simple page that shows that things are working! The formatting will look like the default formatting that your web browser applies to the various headings and body.

Step 5: Add a style sheet

In modern web design, it is essential to format the pages with styles. So, that’s a step that we must also look at. There’s only one more thing that needs to be done.

In the “pages” view, we need to create a page that will be the stylesheet for the site. If you look at the layout that we created, there is a line that says:


    <link rel="stylesheet" type="text/css" href="/styles.css" />

That means the page is already set up to serve up a stylesheet called styles.css which should be applied to the page. Only, we haven’t created it yet.

In the “pages” view, click on the “Add Child” item of the “Hello World” page to create a new page. This will be the stylesheet. Give it the name “Styles” and click on “More” below the text field for the name to show the other properties. Very importantly, change the Slug to styles.css – this is the name that it will be served up as. Use no filter, set the layout to “stylesheet” and the status to “published”. Click on the “extended” tab and then on the ”-” sign in the right hand corner to delete that tab. Put the following styling code into the body. To keep it simple, I just added the styling for the headings since we have a few in our default page.


p {margin: 0px 0px 0.5em 0px; padding: 0px; line-height: 1.3em; font-family: arial, sans serif; font-size: 120%; color: rgb(88,144,168)}
h1 {margin: 1.0em 0px 0.5em 0px; font-weight: bold; font-size: 160%;color: rgb(64,64,255)}
h2 {margin: 1.0em 0px 0.5em 0px; font-weight: bold; font-size: 150%;color: rgb(128,128,255)}

Click on “Create Page” to save the stylesheet.

Now, we are done. Click on the “View Site” button to see your site in color!! You might need to refresh the page in your browser just in case it’s being cached. I know it doesn’t look fantastic but it gives you a feeling on how to go on from here. Hope this helps you!

Thu Mar 29 02:50:00 UTC 2007

Using Textile Editor Helper and acts_as_textiled

Posted in Ruby on Rails at 02:50 AM by mohits

One of the ways to allow lean markup for text is to use Textile for the text fields. Using acts_as_textiled can make this process even easier. The only thing that was missing was to have a way to allow users to enter Textile-formatted text with greater ease… until now, that is!

Enter the Textile Editor Helper to ease this step. This article just takes you step-by-step to show you how to get a Textile-enabled field working in your Rails app.

Install RedCloth if needed

To use Textile, you must install the RedCloth gem first.


1
2
3
4
5
6
rails_apps>gem install RedCloth
Attempting local installation of 'RedCloth'
Local gem file not found: RedCloth*.gem
Attempting remote installation of 'RedCloth'
Updating Gem source index for: http://gems.rubyforge.org
Successfully installed RedCloth-3.0.4

Create a new Rails app

I want to assume nothing, so I’ll just start from scratch here. Create a new Rails app by using the usual way.


1
2
3
4
rails_apps>rails ted_sample
      create
      create  app/controllers
      ... 

Create the database for it

Using whatever method you prefer, create a database (command line, phpMyAdmin, etc.) for the application. In my case, I added a database called ‘sample’ to MySQL running on my localhost.

Adjust the connection properties

Go to

rails_app\ted_sample\config
and edit
database.yml
to point to the actual database. I’m using development mode only (this application has a life of 10 minutes) and the database properties need to be set as above.


1
2
3
4
5
6
development:
  adapter: mysql
  database: sample
  username: <put db user name>
  password: <put password>
  host: localhost

Install the plugin – acts_as_textiled

You can get the details to install the

acts_as_textiled
plugin from the link above. But, it’s quite simple to do.


1
2
3
4
5
6
rails_apps\ted_sample>ruby script/plugin install svn://errtheblog.com/svn/plugins/acts_as_textiled
A    E:\InstantRails\rails_apps\ted_sample\vendor\plugins\acts_as_textiled
A    E:\InstantRails\rails_apps\ted_sample\vendor\plugins\acts_as_textiled\test
A    E:\InstantRails\rails_apps\ted_sample\vendor\plugins\acts_as_textiled\test\memory_test_fix.rb
A    E:\InstantRails\rails_apps\ted_sample\vendor\plugins\acts_as_textiled\test\setup_test.rb
...

That prepares you to do the first bit. But while we are at it, let’s also put in the Textile Editor Plugin, shall we?

Install the plugin – Textile Editor Helper

This one is equally simple. From the root directory of your Rails app, just run the following:


1
2
3
4
5
6
7
rails_apps\ted_sample>script/plugin install http://svn.webtest.wvu.edu/repos/rails/plugins/textile_editor_helper/
+ ./textile_editor_helper/MIT-LICENSE
+ ./textile_editor_helper/README
+ ./textile_editor_helper/Rakefile
+ ./textile_editor_helper/assets/images/textile-editor/background.png
+ ./textile_editor_helper/assets/images/textile-editor/blockquote.png
...

Now, run the install script for it.


1
2
3
4
5
6
7
rails_apps\ted_sample>rake textile_editor_helper:install
(in rails_apps/ted_sample)
 * Copying /assets/images/textile-editor/background.png       to /public/images/textile-editor/backg
round.png
 * Copying /assets/images/textile-editor/blockquote.png       to /public/images/textile-editor/block
quote.png
...

Create the first model

OK, from the root directory of your Rails application and generate the first model


1
2
3
4
5
6
7
8
9
rails_apps\ted_sample>ruby script\generate model item
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/item.rb
      create  test/unit/item_test.rb
      create  test/fixtures/items.yml
      create  db/migrate
      create  db/migrate/001_create_items.rb

Edit the migration file

The next step is to add in the details about the model into the migration file. Open up

db/migrate/001_create_items.rb
and modify it so it looks something like this.


1
2
3
4
5
6
7
8
9
10
11
12
class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.column :name, :string, :null => false
      t.column :descr, :text
    end
  end

  def self.down
    drop_table :items
  end
end

And apply the migration as always.


1
2
3
4
5
6
rails_apps\ted_sample>rake db:migrate
(in rails_apps/ted_sample)
== CreateItems: migrating =====================================================
-- create_table(:items)
   -> 0.0900s
== CreateItems: migrated (0.0900s) ============================================

OK, time to go to the application now and actually use the plugins.

Identify the field for Textiled data

Open your model file

apps\models\item.rb
in an editor and change it to something like the one shown below, marking
descr
as the textiled field.


1
2
3
class Item < ActiveRecord::Base
    acts_as_textiled :descr
end

Create the controller and view for the model

OK, I’m going to cheat here and just use a scaffold to get this done faster.


1
2
3
4
5
rails_apps\ted_sample>ruby script\generate scaffold item
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/items
      ...

Include prototype.js

The Textile Editor Helper relies on

prototype.js
for some features. So, you need to ensure that it is included in the final page that is rendered. One of the easiest ways (for this sample) is to include it in the layout for the
item
class. Edit
ted_sample\app\views\layouts\items.rhtml
to include a call to
javascript_include_tag
in the header part of the file.


1
2
3
4
5
6
7
8
...
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Items: <%= controller.action_name %></title>
  <%= stylesheet_link_tag 'scaffold' %>
  <%= javascript_include_tag 'prototype' %>
</head>
...

Modify the form to use the Textile Editor Helper

(This information is direct from the Textile Editor Helper website)

Navigate to the

app\views\items\_form.rhtml
so that we can add in the code for the helper.

Replace the text area tag with <%= textile_editor 'object', 'field' -%> This accepts the same options as the regular text area tag. Also, at the end of the form, put in the following code: <%= textile_editor_initialize -%>

Once this is done, the

app\views\items\_form.rhtml
file will look something like this.


1
2
3
4
5
6
7
8
9
10
11
<%= error_messages_for 'item' %>

<!--[form:item]-->
<p><label for="item_name">Name</label><br/>
<%= text_field 'item', 'name'  %></p>

<p><label for="item_descr">Descr</label><br/>
<%= textile_editor 'item', 'descr' -%>

<!--[eoform:item]-->
<%= textile_editor_initialize -%>

One small change

Since we are using the scaffold, it escapes all the data when it outputs it. What this means is that if you use it as it is, the textiled fields will display the actual HTML code rather than rendering it. So, we can change the show.rhtml and list.rhtml files to render the actual text. I’m changing just the

app/views/items/show.rhtml
file to the code below – you can change the other one if you want.


1
2
3
4
5
6
<p><b>Name:</b> <%=h @item.name %></p>
<p><b>Description:</b></p> 
<%= @item.descr %></p>

<%= link_to 'Edit', :action => 'edit', :id => @item %> |
<%= link_to 'Back', :action => 'list' %>

Start the Server


1
2
3
4
rails_apps\ted_sample>ruby script\server
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
...

Go to http://localhost:3000/items/ to see how it works.