Archive

Posts Tagged ‘RubyOnRails’

Super Simple Authentication Plugin and Generator

August 24th, 2007

I hereby proudly announce my Super Simple Authentication plugin and generator.

All right, what does it do? Sometimes you need to protect your actions and controllers, but you don’t want to go about installing restful_authentication or anything like that. Adding a simple password for certain actions would suffice. So, I wrote a little plugin that can generate some code for you that allows you to easily protect your app with a simple password.

To get started, you must first install the plugin in your rails application:

script/plugin install http://svn.ariejan.net/plugins/super_simple_authentication

When the plugin is installed, you may generate your SSA controller. This controller verifies your password and makes sure you stay authenticated for the duration of your visit.

script/generate super_simple_authentication sessions

Your password is located in config/super_simple_authentication.yml. Change it.

In the SessionsController, you’ll find an include statement. Move this include to your application controller:

include SuperSimpleAuthenticationSystem

The generator automatically added routes to your config/routes.rb file. If you want easy access to login and logout functionality, add these two lines to your config/routes.rb file as well:

map.login  '/login',  :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy', :method => :delete

You can now protect you actions and controllers with a before_filter:

# Protect all actions in the controller
before_filter :authorization_required
 
# Protect all actions, except :index and :recent
before_filter :authorization_required, :except => [:index, :recent]
 
# Protect only :destroy
before_filter :authorization_required, :only => :destroy

In your views, you can check if you are authorized or not with authorized? E.g.

<% if authorized? %>
    # ... do secret admin stuff
<% end %>

Please visit http://trac.ariejan.net to report bugs. Ariejan.net will keep you updated on new major version. Please subscribe to the RSS Feed.

I hope you enjoy this plugin. Please post a comment if you use it in your project, or if you just like it. Bugs, feature requests and support requests should go into Trac

Features, General

Using Iconv to convert UTF-8 to ASCII (on Linux)

August 21st, 2007

There are situations where you want to remove all the UTF-8 goodness from a string (mostly because of legacy systems you’re working with). Now, this is rather easy to do. I’ll give you an example:

çéß

Should be converted to

cess

On my mac, I can simply use the following snippet to convert the string:

s = "çéß"
s = Iconv.iconv('ascii//translit', 'utf-8', s).to_s # returns "c'ess"
s.gsub(/\W/, '') # return "cess"

Very nice and all, but when I deploy to my Debian 4.0 linux system, the I get an error that tells me that invalid characters were present. Why? Because the Mac has unicode goodness built-in. Linux does not (in most cases).

So, how do you go about solving this? Easy! Get unicode support!

sudo apt-get install unicode

Now, try again.

Bonus

If you want to convert a sentence (or anything else with spaces in it), you’ll notice that spaces are removed by the gsub command. I solve this by splitting up the string first into words. Convert the words and then joining the words together again.

words = s.split(" ")
words = words.collect do |word|
    word = Iconv.iconv('ascii//translit', 'utf-8', word).to_s
    word = word.gsub(/\W/,'')
end
words.join(" ")

Like this? Why not write a mix-in for String?

Features, General ,

How to write a Rails Plugin (for controllers)

July 6th, 2007

A few days back I posted my very first Rails plugin, Acts As Exportable. Although writing a plugin is rather easy, you must know a few tricks on how to get things going.

This article will show you how to develop a plugin that adds functionality to a controller (other plugins, e.g. for models) will follow later. In fact, I’ll explain to you how I developed my Acts As Exportable plugin.

Let’s take a basic Rails application for starters. You have setup a model with some attributes and a scaffolded controller that allows you to CRUD your items. In this tutorial I’ll be working with books. The model is named ‘Book’ and the controller ‘BooksController’. Start your web server now and add some random data to play with.

Before you dive into writing a plugin for the controller to export data to XML you should have some basic functionality in your controller first. I’ve found it easier to develop my code in the controller first, and then port it to a plugin.
Read more…

Features, General

How to force data to be downloaded as a file from your Rails app

July 2nd, 2007

In the essence of every application is data. One way or another your application manages data and at some point, you need to get that data out. Either you want to synchronize the data with another application or device. Or you want to move your data to another system all together. Either way, you’ll need to gather your data and send it from your application to the client… as a file.

Downloading files is not the hardest thing around. But the problem is that some formats, like XML, are automatically parsed by the browser and this makes it harder for users to download files like that.

So, what you want to do is, ignore the browser and offer your data (in XML or whatever format you want) as a file that can be downloaded directly. The solution is rather easy, as always with Rails.
Read more…

Features, General

ActiveScaffold + acts_as_taggable + Auto Complete

July 1st, 2007

I’ve talked before on how to use ActiveScaffold with acts_as_taggable_on_steroids.

The problem with that solution was that, although the checkboxes for every tag are very nice, you couldn’t easily add new tags. For some people, this may be fine, for others, it is not.

Together with a colleague (who wishes not to be named), I found a solution that is quite elegant. Instead of using check boxes, and creating all kinds of subforms in ActiveScaffold, we opted for an auto_completing, comma-separated list of tags.

This article descripes the solution we found. I think you’ll like it very much!
Read more…

Features, General

Rails production server setup and deployment on Ubuntu/Debian

June 20th, 2007

Please digg this story to spread the word! Thanks!

Okay, this is a big one! This article will show you (and explain to you) how to setup a Ruby on Rails production server with Ubuntu 7.04 or Debian 4.0 and how to deploy your Rails application there.

First, what’s getting installed:

  • Ruby 1.8.5
  • Ruby on Rails 1.2.3
  • Subversion 1.4
  • MySQL 5.x Server
  • Apache 2.2.x
  • Mongrel Cluster

I assume that you have just installed a fresh system with Ubuntu Linux 7.04 or Debian 4.0. If you haven’t, do so now! You don’t need to install the “DNS” or “LAMP” server in Ubuntu. Just a minimal system is enough for this tutorial.

I’ll be deploy an imaginary Rails application named “myapp” which uses MySQL and is stored in Subversion. More on that later on.

Well, let’s get going and get that Ruby on Rails server ready.
Read more…

Features, General , ,

Action Mailer: All mail comes from MAILER DAEMON

June 20th, 2007

Today I was trying to send mail from my Rails application through Action Mailer. This is quite simple, but I wanted to use a custom from-address. So, I create a setup_email method in my UserNotifier class that sets some defaults for every email sent out:

class UserNotifier < ActionMailer::Base
  protected
    def setup_email(user)
      @recipients  = "#{user.email}"
      @from        = "My Application <no-reply@example.com">
    end
end</no-reply@example.com">

May you spotted the problem already, but I didn’t. All the mail sent came from “MAILER DAEMON”.

From: MAILER DAEMON

The problem was that @from didn’t contain a properly formated from-address. It is missing the closing >, and so my email server ignores it.

If you have this issue, double check the from address, and make sure it’s valid! Cheers.

Features, General ,

ActiveScaffold, Acts_as_taggable_on_steroids

June 11th, 2007

Update: also read Active Scaffold + Acts_as_taggable + Auto Completion.

This is kind of an advanced topic, but I think it may be useful to a lot of people.

ActiveScaffold is a great plugin to start building a user interface. The great thing about AS is, that is automatically recognizes associated models. When editing a model, you can easily add or select another model that you want to associate with is.

The best example of this is an Article, where you can select the author (the associated User model) with a drop down box.

There is only one point where I ran into trouble with ActiveScaffold: acts_as_taggable_on_steroids.

Acts_as_taggable_on_steroids allows you to easily attach tags to models and do all kinds of crazy stuff with them. But, if you want to integrate in into AcitveScaffold, you’re in for a tough ride.
Read more…

Features, General ,

Ultimate List of Ruby Resources

June 10th, 2007

This is the first post, named “Ruby”, in a series of “Ultimate List of … Resources”. I’m going to compose several lists for different topics I encounter during my development work. To start, I begin with Ruby. Later, I will add “Ultimate Lists” about Ruby on Rails, Subversion, AJAX and some other topics.

Feel free to let me know if I missed an important resource. I’m also open to suggestions about other “Ultimate Lists”.

For now, you’ll have to settle for the “Ultimate List of Ruby Resources”.

Read more…

Features, General , , ,

Installing RMagick Ruby Gem on Mac OS X 10.4.9

May 29th, 2007

When you want to manipulate images with Ruby (or your Rails application) you’ll probably want RMagick installed. This is no easy feat on Mac OS X.

The official guide suggests installing X11 and using darwinports to install everything. This guide shows you how to easily install RMagick on you Mac OS X system. In this case I use Mac OS X 10.4.9.
Read more…

Features, General , ,