>>=?

Thursday, 2 April 2009

Random Thoughts

When you heard a suggestion from some who has more experience than you, try to follow what this person had said (Obviously if it's logical)


Tip 23 from "The Pragmatic Programmer": ( "Always use source code control" Source code control is a time machine for your work -- you can go back)
I used to keep track just of the files which I considered "important", and I didn't listen the PP. Several days ago I deleted something what I had thought wasn't really important, when in fact it was!, since that I have been keeping track of all my stuff.


There is nothing more important that your family, and nothing, nothing in the world could replace it


I have been far from home for a long time now, and is here were I really learnt how important my family is in my life, I'm aware that they won't be alive forever, but while they are , why not enjoy all the possible time with them ?.
Maybe it doesn't apply for you, perhaps you thing you are better without them, but just remember, once someone is gone, there is not yet #limbo room.


I need to stop procrastinating


I have found really hard to keep myself in just one thing, I mean, sometimes I have left important things just to watch a TV show or to do other thing, what happen is that those activities are not really helping me in a positive way, I feel really bad when I'm not
studying, coding, reading (etc), because I know that is time which I could be using to improve myself.


Envy and rivalry between colleagues are not good, they aren't!


Recently I have an argue with a good friend because he was taking some guys as enemies just because they are totally better in some ways that him.I think we shouldn't take anyone as enemy or not even feel envy of the ones who know more than us, rather we should join all together and establish a help chain in with we can combine what others know and what I know to make a big mash-up of knowledge,I'm pretty sure if we do what I say, everyone would be a happy winner, and what is more important we could help another people to be better,I could be better, all of us could be better, let's take the word!.



Four simple things which I had in my head and I just needed to put in somewhere; Whether you agree or not with what I say, I don't really care, but if you have something to say it'd be really nice hearing from you.


peace

Sunday, 8 March 2009

A poor-man's blog in Turbinado

This is yet another 15 minutes blog this time using Turbinado.
For now Turbinado just works with PostgreSQL and Linux/Unix OS.

The Tools

- GHC 6.10.1
- PostgreSQL
- Turbinado ( grab the code : git clone git://github.com/alsonkemp/turbinado.git )

Building Turbinado

Building Turbinado is not going to give you a headache like it used to be. Because installation with cabal is now supported, just doing cabal install will do it for us.

Configuring

In order to generate the models , we have to create the data base, in this case it has two tables which are posts and comments, and its name is blog, the script is here
We need to edit Config/Database.hs, for set up the data-base connection :

databaseConnection = Just $ connectPostgreSQL "host=localhost dbname=blog user=turbinado password=turbinado"

then, models have to be generated, it is done with :
runghc scripts/GenerateModels

A nice explanation of how the ORM (TRM ?) works is here .

Creating the layout

The Layout at /App/Layouts/Default.hs is used by default.
The Layout provides the wrapper for the View, but is basically just a View with some extra functions. In particular, the Layout tells the system where to place the View by adding a call to insertDefaultView
<% insertDefaultView %>

Let say that this is our Default.hs :



We include the (x)hmlt code in the markup function which is searched automatically for the system, each view has to have this function. In line number 12, <;% insertDefaultView %>; is called, so the piece of html which is in the view that we are calling is inserted there. In line number 7 We declare the styleSheet using a helper, <% styleSheet "default" "screen"% , it just replace the code with the equivalent html. The css code,images and js are located in /static/css, /static/images and static/js respectively.
Get all the code for the layout here .

Controllers

For this application We are using 3 controllers :
- Posts.hs : Is in charge of everything that is related with the posts
- Comments.hs : Help to create a new comment, actually we do not eliminate comments.
- Manage.hs : This show us an "admin" panel, which allow us to create,eliminate or edit a post.

The controller Posts has the following actions :
- new : Show a form to write the post .
- create : Save the posts in the database .
- index : List all the posts .
- edit : Show a form to edit a published post.
- updateP : S
- remove : Delete a published post.
- search : List all the post that match with certain string.

I'm just going to explain this controller you will be able to understand the others two after that.
This is the code :



The controllers Comments.hs and Manage.hs are straightforward and are here .

Views

Once we have the Posts.hs,Manage.hs, and Comments.hs in App/Controllers, let's create some views .
Create a new file in App/Views/Posts/ named New.hs , this is the code for that view :

Is haskell code with html wrapped in the markup function.Here we found other helper in line number 5. When the form is sent, the action related with this will generate a new posts and then redirect to its view, to do this we use routes, check the action create and show in the controller "Post.hs"

Then we need a view to show the post. In the same folder, create the a new file, "Show.hs":




Now, we are able to create and show a new post. Let's try it!

First start turbinado :
dist/build/turbinado/turbinado -p 9999

then browse to http://localhost:1111/Posts/New , write whatever you want, and the publish!.


To list all the posts that we have published we write a new view called Index.hs :



Now, if we browse to http://localhost:1111/Page/Index, all the published posts will be showed.

The views that we just wrote will help us to understand how the others work. You can grab them here .

Credits

I'm Just a Haskell newbie, I can say that writing this application helped to improve a bit my understanding of Haskell, but, while I was doing this, lots of doubts came to my mind. I want to say thanks to all the people in #haskell for taking part of their time helping the community and bearing with us!, and Alson Kemp for his work in Turbinado and of course his good will explaining me things that I didn't understand at the beginning (Some times, really silly things!).