Expected x.rb to define X (LoadError)

Posted by Slobodan Kovačević on April 15, 2009

I have been working on extending Rails’ I18n Simple backend to make it work with Serbian grammar (post on that will follow soon), but I kept getting an error:

Expected ./lib/serbian_simple.rb to define SerbianSimple (LoadError)

I’ve just spent an hour trying to figure out why this keeps happening and I found that there’s a lot of people with similar problem.

It seems that the problem appears when Rails tries to autoload files. In my case there was a simple solution – I just added require ’serbian_simple.rb’ in environment.rb to manually load the file.

Share/Save/Bookmark

Simple password protected administration with CodeIgniter 5

Posted by Slobodan Kovačević on March 08, 2009

Last week I’ve taken a break from Ruby/Rails development and I’ve worked on a site that uses PHP with CodeIgniter framework.

Despite the fact that CodeIgniter has a very nice documentation I found it very difficult to find a way to do some simple things, that are more or less obvious, but which can be a problem for someone who hasn’t worked with CodeIgniter before. (for example, I found myself more than once looking at CI code to figure out how it works, so I can use it)

I had to make a simple password protected administration section. One admin user, one password, no user registrations, no roles – simple as possible. As I was using CI framework I decided to find a plugin/library that does this. Unfortunately most CI authorization plugins/libraries are very bloated and too complicated for this simple task. I tried to find some examples how to handle this simple use case, but nothing came up.

Finally I’ve found a small authorization plugin: Erkanaauth.

First you need a user table (must be named ‘users’) which only needs to have an id field and all other fields are optional because you will manually specify what other columns will be used. I opted for simple id, username, password:

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
);

We will need to “install” ErkanaAuth library. You should download it and unzip it.

Next we should create an Admin controller which will handle all administration tasks (remember I’m making simple admin here, so I don’t need to protect multiple controllers).

<?php
class Admin extends Controller {
  function Admin()
  {
    parent::Controller();
    $this->load->database();
    $this->load->helper(array('url', 'form', 'date'));
    $this->load->library(array('form_validation', 'upload', 'Erkanaauth', 'session'));
  }
}
?>

Constructor just connects to database and loads some standard helpers and libraries (including Erkanaauth) that are usually used.

Next step is to add a function which we can call to verify if user is logged in:

  private
  function authorize()
  {
	  if($this->erkanaauth->try_session_login())
	      return true;

	  redirect('admin/login');
  }

Function uses Erkanaauth’s try_session_login which checks if user is already logged in (checks session for user id). If user isn’t logged in we’ll redirect him to our login page:

  function login()
  {
    $username = $this->input->post('username', true);
    $password = $this->input->post('password', true);
    if($username || $password)
    {
      if($this->erkanaauth->try_login(array('username' => $username, 'password' => $password)))
        redirect('admin');
    }

    $this->load->view('admin_login');
  }

  function logout()
  {
    $this->erkanaauth->logout();
    redirect('admin');
  }

Key command here is try_login in login function which tries to find an entry in users table that fulfills given conditions. If you have different users table than the one I made this is the place where you should enter your column names.

Logout function is has just a simple call to Erkana’s logout function. Nothing special there.

Of course we also need a login page template which should contain a simple user/pass form. It’s pretty basic and you can see it if you get the complete code (see at the end).

Finally we have everything needed to protect any page in Admin controller. In order to protect a page all you need to do is to add a call to authorize function to any function you want to protect. Like this:

  function index()
  {
    $this->authorize();
    echo "Do something useful... For now just display logout link: ";
    echo anchor('admin/logout', "Logout");
  }

That’s it. Now you have fully functional administration section which requires username and password authorization.

You can get the complete sample application from Github repository. Feel free to expand on it or use it any way you like.

Share/Save/Bookmark

Restoring superblock on Ubutnu

Posted by Slobodan Kovačević on November 09, 2008

Recently I had a problem on my Torrent box (an old PC that I use as dedicated torrent client) that runs Ubuntu. For some reason my root partition was being mounted as read-only. Everything else seemed to work (all other partitions were mounted properly), but I couldn’t change any of my config or do anything on root partition.

I did the usual stuff:

  • Run fsck checks and it said that everything is fine
  • Used Ubuntu’s live CD to boot, which got me read-write access to root partition. I changed some things in fstab, tried to get it to be rw permanently. No matter what I did as soon as I rebooted the root partition was once again read-only.
  • I tried booting from some repair disks I have, but all checks passed and no problem was detected. :(

Finally, I read somewhere that a similar problem was caused by faulty superblock on hard drive. Fortunately Ubuntu stores superblock backups in different places around disk, so I decided to try to restore it from one of those backups.

It turned out that all I needed was a single command (this Ubuntu forum post helped) to restore superblock:

e2fsck -b 32768 /dev/hdc1

After that my root partition was back to read-write mode. :)

Before you do stuff like that to your computer I suggest that you read man pages for mke2fs and e2fsck. It will prevent you from doing something foolish like deleting your whole hard drive. :)

Share/Save/Bookmark

RESTful admin namespaced controller using scaffolding 17

Posted by Slobodan Kovačević on September 17, 2008

Most of my clients prefer to have a separate admin section. In turn, I like to have separate controllers for admin section and front-end in my Rails app. This is not as straightforward as it might seem, especially if you like to use scaffolding for admin controller.

The goal is to get 2 separate RESTful controllers, admin & front-end controller, one model and for admin pages to have scaffolding.

Here is the easiest way I found so far to accomplish this. This example generates categories model and controllers for it.

./script/generate controller admin/categories
./script/generate scaffold category name:string

This will generate an empty controller in admin namespace and a scaffolded resource for front-end controller.

Now we have everything generated we just need to make it work with admin controller and not with front-end.

  • move all templates from app/views/categories to app/views/admin/categories
  • copy all functions from categories_controller.rb to admin/categories_controller.rb
  • add namespace for admin controller in routes.rb:map.namespace :admin do |admin|
    admin.resources :categories
    end
  • in admin/categories_controller.rb replace in 3 places redirect_to calls to work with admin namespace. It will have something like redirect_to(@category), but to work with namespace it needs to have redirect_to([:admin, @category])
  • make similar changes in all templates, i.e. make it work within an admin namespace. You need to make following changes:
    • form_for(@category) => form_for([:admin, @category])
    • <%= link_to ‘Show’, @category %> => <%= link_to ‘Show’, [:admin, @category] %>
    • categories_path => admin_categories_path
    • edit_category_path(@category) => edit_admin_category_path(@category)
    • new_category_path => new_admin_category_path

That’s it. Now you’ll have /admin/categories for all administrative tasks and you have a free controller for front-end actions.

You might wonder why not just generate scaffold for admin/categories… The reason is that you’ll also get a model that is namespaced in admin (i.e. model would be Admin::Category). Scaffolded views also wouldn’t work as it seems that generator doesn’t take into account the fact that you are using a namespace.

Share/Save/Bookmark

Mosso hosting cloud 5

Posted by Slobodan Kovačević on September 17, 2008

Mosso’s hosting cloud at $100 / month seems like a good solution to get a scalable server. However one thing bugs me, actually two things…

First one: they offer FTP only access. Meaning you cannot deploy sites directly from code repositories (i.e. git or svn). That sucks.

Second thing that bugs me: for $100 you get quite a lot of computing power which can be used to run multiple sites – but you are only allowed to have one Rails app running. Only one. If you want additional Rails apps (for example to have a test server) you need to pay an extra fee.

I know it’s cloud computing and that you have to be able to run it with any additional configuration (that’s why I think it’s ok that you have to freeze your gems in Rails apps, because you cannot install any gems yourself)… but not being able to checkout my code from repository and having to upload the whole app each time you make changes is really annoying.

In their defense, the support guy said that they are working on it, but he could give me an ETA when they’ll allow something like that.

Share/Save/Bookmark