20070726

Jonas' Crazy Adventures in RoR - Part IV - Test Data, will_paginate, Page Security

Okay, now we have a semi-well built model, let's see some UI action shall we? It won't be as fancy as you may think, but we're only focusing on the functionality right now and all that good stuff. in part deuce of the jonas series, we covered how to create a login acount using Acts as Authenticated (AAA). So that pretty much means that we have a user in the DB.

How do we verify this? script/console ofcourse! Below is a screenshot on how to verify if I have anything on my users table.

As you can see we have the one record - The account we made back in the days.

So let's go create some test data shall we? Again, I will be using the script/console style to insert my data... I'm going to create three new projects, and the first two will be assigned to the only user.

Here's how you do it:

Project.create :name => 'First Project', :description => 'This is my first project', :start_date => Date.today
Assignment.create :user_id => 1, :project_id => 2
Project.create :name => 'Second Project', :description => 'This is my second project', :start_date => Date.today
Assignment.create :user_id => 1, :project_id => 1
Project.create :name => 'Third Project', :description => 'This is my third project', :start_date => Date.today


Now let's check the database to show that I ain't bullsh*ttin'


Again, I just wanna let you guys know that I am intentionally making "Third Project" not assigned to any user. What I want to later is for the current user to be able to view his assigned projects.

Wait a sec. Jonas, I'm assuming u cannot add the same user to the same project more than once - How are you handling this?
Check this out suckah!


I want to see some UI action!!!!


I am going to cheat... I will be using script/generate scaffolding to generate my UI... But hey, at least I won't be coding a lot of stuff! The only difference is that I will be using a different method of pagination. I will be using will_paginate from errtheblog.

Lets just run our page for now, and see what we have:


Yay! we have a page! I will create 4 more test data to see the pagination stuff, and we will have 7:



Not bad, it works... but lets take a look at the list method that does the pagination stuff

"
You will notice that it returns two items! Soo ghetto! After talking with sideline. he pointed me to a rails cast about pagination found here. That will be the "new" way of pagination because in Rails 2.0 they will get rid of the ghetto-classic paging.

note: I semi-like will_paginate. the thing I don't like is that my model knows the implementation of paginate. If i used a presentation layer that handles the pagination, it would be better...

So what do we have to change?
list.rhtml, & product_controller.rb in the list method. Check out the before and after shots of the changes:

list.rhtml before



list.rhtml after



def list before


def list after


Not much change so it's all good!

Page Security


I will cover it briefly. Basically right now, we have a login page that does nothing, and our projects can be viewed by anyone. What we want to do is implement AAA's security stuff on our new project controller and here's how you do it:


That frikken ezed! now your anyone accessing project controller pages are required to login if they haven't yet.

This is jonas saying "alll right!"


Next time I will cover displaying only the projects a user is assigned to after login.

0 comments: