My Rails Enviroment

If you have developed in rails, you probably love the language, but hate to set up the environment.
I know I do.

I've tried to use it in windows (it is hell) and linux (also not so good).
I thought the problem was just with me. My system is always with problems to run rails, and every time I had to work on something, I had a lot of work making it work again.

Finally a friend found a solution for that.
What he did is:
  • he created a virtualbox vm with ubuntu.
  • Installed ruby, gems, rails, etc
  • configured it to be accessible through ssh, and redirected to the port 3000
This sounds just perfect.
So all I had to do is to download this vm, install the editor I like(scite) and use it.
But it was made in a mac, with support to the x system, and windows doesn't have that.
Looking for that in a few blogs I found out about Xming, which is a software that allows you to run linux x applications on windows.

It was quite easy to set up and use.
  • Open putty
  • fill host name and port
  • click connection \ ssh \ X11
  • check Enable X11 forwarding
  • type :0.0 in x display location
like this:

after that you just have to open the file like this: scite index.html.haml
As you can see, the application looks different than a windows regular application, and that is because it is. This is running right from the vm, and the file being edited here is inside the vm.
I don't know about you guys, but to me, this is really impressive.

By the way, he wrote a post about it, and it is very nice to read if you want to know how to do it.
Here is the link for his page: Giorgenes' post about virtual ruby environment



PetaPoco. Said whaaaat?


So, maybe some of you guys checked out my dead database project, the dNet.Db. It is free and open source @ http://dnetdb.codeplex.com/
I started developing that framework because I always thought  it is important to automatize the simple and boring part of database. And by this I mean that it load an object values should be as easy as object.load(), or object.LoadList() if you want an array.
 And it was working fine. But we all know how a project like that requires a lot of maintenance, adding new databases support, fixing bugs and things like that.

A few weeks ago I was talking about this to a friend and he told me about the project massive.
Massive is nice, but it doesn't support everything I needed (mainly because of the stored procedures), so trying to go further into that, I found the PetaPoco(http://www.toptensoftware.com/petapoco/).

This is the kind of framework I consider that fit all my needs:
- really easy to install
- easy to fetch objects from the db
- supports sqlite
- allows you to use plain old SQL for all the other situations.

So I've been using it for a bit now, and it hasn't let me down. I am not thinking about using my framework anymore. So I decided to write a little tutorial of how it works.

Install: There is a nuget for that. If you don't know what a nuget is, here is a link, probably I'll talk about this someday, but this is really cool, check it out.


public class Project
      public int Id { get; set; }
      public string Name { get; set; }

Voil'a... Easy as pie...
then you could say:-Hum... the loading part might be tricky....
not really... this is the code:

var db = new PetaPoco.Database("Base");
ViewBag.Projects = db.Fetch<Models.Project>("");

and that's it.
Ok, that's not it. Well it is if you have a really simple object, but you don't.
So... now comes the tricky part?
Yes. Compared to the other part it is. but not so much

The loading bit is exactly the same, but now I'll add some more code to the model:
    public class Feature
        public int Id { get; set; }
        public int ParentId { get; set; }
        public int ProjectId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public Project project { get; set; }
    public class Project
        public int Id { get; set; }
        public string Name { get; set; }
        public Dictionary<long, Feature> Features { get; set; }
    class ProjectFeatureRelator
        Dictionary<long, Feature> features = new Dictionary<long, Feature>();

        public Project MapIt(Project p, Feature f)
            Feature fExisting;
            if (features.TryGetValue(f.Id, out fExisting))
                f = fExisting;
                features.Add(f.Id, f);

            f.project = p;
            p.Features = features;
            return p;

Now, this looks a lot, but let's understand what happened there:
1 - I've added a new class (feature). This class have a primary key Id, and I want it to load the project associated with it.
2 - I've added a new property to the project class:Features. this is because I want the list of features to be loaded as well. Note that I've annotated the [PetaPoco.ResultColumn] to make the connection.
3 - So far so good, but now comes the ugly part: the join. I admit it is not an elegant code as the rest, but it is not so bad, and it is very easy, you just have to copy it, and use it in your code.

I don't know if it is a good idea to use this in a big project, but for the small ones, I will definitely use it.


What about mvc?

So seems like asp.net mvc is finally popular?
That's perfect, I mean, this is how it should have been since the beginning.

Let's start with what is mvc:
Mvc is to build the application as model, view and controller.
What does that means?
It means that the view should be only responsible for showing the interface, the data access should be in the model layer, and everything else should be done by the controller.

Sounds nice, right? Old but gold... But the thing is... and how about the web?
 Usually we had a asp / aspx / php / jsp / etc file. This file is requested, and then, from this point on you can call your controller logic.
But this isn't the proper way to do it. This way your view is mixed with the controller.

And then, there was the light! true MVC to the web applications.
Is this hard to implement? off course not, my little grasshopper....
You just have to think your application in a different way. and what is that way?

And your routes file should look like this:
public static void RegisterRoutes(RouteCollection routes)

            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

And how do you use it?
 Easy: you set it as your controller name, and action, so every time the user types that address the method defined as action is called.
so when the user types website/home/index, the method index inside the controller home is called.

Ok, we've got the code, but how to display it to the user?
In the controller method, there should be a return View(). You can return View("name-of-the-view")

Now you have the request processed in the right place, and you have your view. Sweet! :D
Buuuuuuuuut, something is still missing... and that's because you have to connect those 2.
And this is called the mighty ViewBag!

View bag - speaking like mr jobs would say - is a magical, fantastic... the better ever created object to carry values between controller and view.
So how do you use this thing?
ViewBag.YourVariable = "The title";

and in the view you can use it like this:

By the way, this brings up another topic:RAZOR

Once again: this is magical, fantastic, amazingly simple. you can put C# code in the view just by adding a @ in front of it.
And, if you want to put a chunk of code, just use
  //several lines of code

and how about the model?
Well... the model is the same as before, I won't talk about it here.
But that's it for now.
I'll write more about razor.

Hello world!

Hello world!

To start with, I'll explain why I am creating this blog. Maybe you know my old blog. It is dead now.
It was in my old server and I decided not to keep it, but what makes me mad is the domain. it expired and now I don't even have email.
Anyway, new blog, new posts, new life.
I don't hate java anymore, yay!
Just kidding... I still do.
But not as much.
You see, now the servlets seems to work. The IDEs are decent. And java is so far behind .Net, they don't even try to compete anymore... Maybe that's the best part.
So this is my first...ish post... at least the first in this blog.