Archive for the 'Ruby on Rails' Category

New month, new beginning. Welcome Cubicle Apps DMS!

I am glad to announce that my newest project developed “in the clouds” is now available for public beta testing. Welcome Cubicle Apps DMS!.

CubicleApps DMS

You are all invited to create an account, use the system to the maximum and send me your feedback.

What is all about?

Cubicle Apps DMS is designed as a document management system for tracking and storing electronic documents, images of paper documents which have set an expiry date, so you can organize them and get useful reports and alerts in regard to the expiry dates and renewal procedures.

What type of documents? Well any expiring documents. Read more in this introduction, which will be part of the end user manual.

Why?

During my career, I have been frequently asked by my employers and later on by my partners and clients to find and or craft a system which is dealing with expiring documents. So, I know my competition, I know the document workflow in typical small and medium sized companies and yes, I have the strong believe that this territory is still not well exploited and CubicleApps DMS found its niche.

In conclusion, please vist http://www.cubicleapps.com, register with an account, play around and let me what you think. In the same time, I’ll keep you updated with the progress and write more about the concepts and philosophy behind CubicleApps DMS. Enjoy!

Ctags and Vim for Ruby on Rails Development

Tagging is a feature that gives vim IDE-like code browsing powers.

:help tags

A tag is an identifier that appears in a “tags” file. It is a sort of label that can be jumped to. For example: In C programs each function name can be used as a tag. The “tags” file has to be generated by a program like ctags, before the tag commands can be used.

ctags -R *

is probably the most simple usage of ctags command from the command line, issued from the root of the Rails project. The result is a file named tags which indexed all objects, so it makes easy to jump, for example, to a Ruby method definition just by using CTRL-[ shortcut anywhere in the code where that method is used.

However, ctags can be told to exclude some of the files and directories and it makes sense to ignore the .git and log directories as they do not contain useful information:

ctags -R --exclude=.git --exclude=log *

But it would be more useful if we could have access to the Rails core method definitions and documentation, as well as for the included gems. Bellow, I have included the gem directory of my default ruby (ruby-head under rvm):

ctags -R --exclude=.git --exclude=log * ~/.rvm/gems/ruby-head/*

Navigation tips:

  • :ta belongs_to jumps to belongs_to method definition
  • CTRL-] if you see a call to a method and wonder what it does, position the cursor inside the method name and hit CTRL-]
  • CTRL-T to go back from the method definition

Two other useful shortcuts: CTRL-I and CTRL-O – think of them in and out the method definition.

Navigating through a list of methods with similar names

:ta /^validates_*

will look for all method definitions that starts with the given string. By default it will jump to the first definition found, but we can do more.

Following vim commands can be used to navigate through the method definitions:

  • :ts shows the list
  • :tn goes to the next tag in that list
  • :tp goes to the previous tag in that list
  • :tf goes to the first tag of the list
  • :tl goes to the last tag of the list

Resources:

Getting back sqlite3 to Mac OS X

For some reason, I lost my SQLite version from my Mac OS X 10.5 Leopard. I hardly suspect MonoFramework for the “damage” caused, as /usr/bin/sqlite3 has been replaced with a link to a non-existent file from MonoFramework Library.

A quick and dirty fix is to bring back the sqlite3 source and install it in the *nix fashion (configure, make, make install). For future reference, I am listing the full sequence of commands I’ve typed in Terminal:

curl http://www.sqlite.org/sqlite-3.6.14.tar.gz | tar zx
cd sqlite-3.6.14
autoconf
./configure --prefix=/usr/local
make
sudo make install
# check if SQLite is installed properly
sqlite3 --version
# 3.6.14
which sqlite3
# /usr/local/bin/sqlite3

Note the new path: prefix=/usr/local, so the CLI (command line interface) sqlite3 will be accessible via /usr/local/bin/sqlite3. As sqlite is part of the core Mac, I believe it is wise not to interfere with any of the original libraries (not only CLI).

Do you know a “cleaner”, Mac OS X like (.dmg) way to restore SQLite? Please comment.

Keeping session data across subdomains in Rails 2.3.2

Upgrading to Rails 2.3.2 I came across with the error of not keeping the session data between subdomains. That meant I had to authenticate each time I would change the subdomain.

The fix is simple.

Prior to version 2.3.2, I have had something like this in environments/development.rb file:

ActionController::Base.session_options[:session_domain] = '.w2task.local'

In Rails 2.3.2, I had to change this line into:

config.action_controller.session = { 
  :domain => ".w2task.local" 
}

Of course, same principle applies to development.rb, where:

config.action_controller.session = { 
  :domain => ".w2task.com" 
}

I’ve lost some hours in researching this solution, so I hope this will help others in need. too. As usual, I will be more than happy to hear comments from you.

Happy coding!


Ruby on Rails application could not be started

This is the error message, on my local machine after I upgraded to rails 2.3.2. Well, it was not an instant error, but it appeared after a while, to be more exact after I rebooted my computer.

The error:

no such file to load -- application.rb (MissingSourceFile)

As the new rails renamed application.rb to application_controller.rb, I have immediately sensed the root of the problem, so I’ve tried to

./script/server

and it worked. That means it has something to do with the Phusion Passenger.

But what? I’ve updated all the gems, and passenger was updated too.

Well, yes, but not the apache module, and not the configuration…

This is to remember: Every time, after getting a new version of Phusion Passenger via sudo gem update, I have to:

sudo passenger-install-apache2-module

and go through the setup, than edit the httpd.conf file (in my case: /private/etc/apache2/httpd.conf) and replace the old passenger configuration lines with these new ones:

LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-2.1.3/ext/apache2/mod_passenger.so
PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-2.1.3
PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

Last step is, of course, to restart the apache server:

sudo apachectl restart

Now we are set to go.

I have post this article as a reminder to myself in case something similar would happen in future, and as well I hope to be useful to the readers of my blog whom might struggle with the same type of issues.

For any other tips and comments, it would be a pleasure to see you replying to this post.

Coding is fun!

Ruby on Rails – Tools I Use

Going with the hype, I am coding in Ruby on Rails and exclusively on a Mac.

As a text editor I am using TextMate. It was love at first sight (although shared) – I know, we are talking about community here. But as many of us, I would like to see more often updates and at once that pending version 2. It is something there which says that a version increment will make us much more happy. I have a feeling it is loosing slowly its vibe. For instance: Peepcode is releasing a screencast episode about Emacs, and I am also seeing more and more blogs talking about switching to either Emacs (Carbon or Aqua) or to MacVim.

CSSEdit 2 is filling my coding gear and it helps a lot in cleaning my templates. I am a dirty hand coder in the way I am using quite often something like <div style=” …. “> directly into my code, pushing only at the end for what I need to a style sheet.

I discovered git once with Rails and since it became my right hand in versioning. I never believed it can be so easy, doesn’t matter if you are online or offline, you are alone or working along with a team. More, I agree it is more productive to have that infinite hexa number instead of 1, 2, etc as your file/project version number, because you just code, don’t tend to compare. And because I love luxury, I am using a micro account with github. Yes, I have my own secrets there, too 😉

Ticketing and organizing my staff – I am into a personal level with the lighthouse. And the Lighthouse Keeper is giving me enough support whilst offline.

What about you?

Coding Aloud – Restricting your users to edit other’s profile

restful_authentication plugin made its way to my projects as the “de-facto” authentication system in my Ruby on Rails projects like w2task or gistate.com. I would not insist on how to install it, as it is well explained on its home page at github.

What I want to show is that in particular instances we may not like that one user can access and modify other user’s profile. For example if I try to edit my profile, the URL will end in something like users/2/edit and if I would change it to users/1/edit then I am able to modify this user – most often an undesirable fact.

Here is my workaround:

app/controllers/users_controller.rb
  # ...
  def edit
    if params[:id].to_i == self.current_user.id
      @user = User.find(params[:id])
    else
      flash[:error] = "Not allowed!"
      redirect_back_or_default('/')
    end
  end
  # ...

That’s pretty much everything I need to change.

What is your preferred solution? Are you doing these tests into a – perhaps – before_filter?