AR create_with

I knew there was AR’s find_or_create_by  method but I didn’t know it had a very useful companion create_with .

This will either find a record  SomeModel.where(foo: "bar")  or create it with SomeModel.create(foo: "bar", some: "stuff") . Very useful.

Render Rails assets to string

If you ever needed a way to render a Rails assets to a string, Hongli Lai from Phusion describes how. 🙂

I prepared a Gist wrapping it into a nice helper. 😀

 

Sortable Columns Across Tables

If you follow Railscast 228 you get sortable table columns for your model. But what if you don’t want to expose the name of the actual database columns or more interesting if you want to sort across tables? Here is how I do it.

In your controller add order or reorder if you already have an order clause in one of the used scopes (default_scope counts too).

As I’m using this mechanism in different controllers I added the common functionality to the application_controller.rb file.

This will use the ALLOWED_SORT_COLUMNS hash to map between user visible and actual database sort columns. Adding sort_query also allows us to sort by multiple columns at once. navigation_params is a shortcut I use when generating URLs (e.g. in link_to) and I want to preserve pagination, sorting, filters/searches, etc. across pages.

Note that sort_icon assumes you are using Bootstrap.

Now we can have sortable columns in our views:

How to to set up Gitlab on Debian

Update: This howto is outdated. GitLab has changed a lot since it was written and a lot of it is not applicable anymore (e.g. since GitLab 5.0 it doesn’t depend on Gitolite any more and only needs one system user to be setup). So you are probably better off using the official installation guide. 🙂


If you want to install Gitlab on Debian you can easily follow their installation steps for Ubuntu. But be careful there are a few gotchas nobody is talking about.

The following steps will assume you are root.

Preparations

First make sure you have all the latest updates installed.

Then we have to install a few packages.

Install Ruby

If you have not installed ruby you might want to consider using RVM.

Install it with

It will be installed into /usr/local/rvm.

Ask it for the requirements for installing MRI and install them.

Install ruby and make it the default.

You should install a minimum set of gems. Add “passenger” if you are running Apache as your web server or “thin” if you are using Nginx.

Install Gitolite

First of all we want to create a dedicated user for Gitolite and Gitlab. This will also be the user the Rails processes will be running in (this is important later).

Configure git for the new user.

Generate the ssh key for the git user. It will be saved in /home/git/.ssh/id_rsa. We will run Gitlab as the git user so it will use this key to authenticate against Gitolite.

Copy the public part of the key for later use when we setup Gitolite.

After that we install Gitolite. In contrast to the Gitlab documentation I installed it from the Debian repositories.

It will not be fully installed as it will tell you something like:

No adminkey given – not initializing gitolite in /var/lib/gitolite.

So we do this by using dpkg-reconfigure and using our previously prepared account.
When prompted, answer as follows:

  • Gitolite user: git
  • repositories directory: /home/git
  • admin key: /home/git/rails.pub

Now you should have Gitolite set up in the /home/git directory. But we will still have to tweak it a little.

Edit /home/git/.gitolite.rc and find the line that reads “REPO_UMASK = 0077;” and change it to “REPO_UMASK = 0007;” (i.e. three zeros).

You now need to change the directory privileges on the /repositories directory so Gitlab can use them

Gitolite should be ready now.

You can test it by cloning the admin repository:

Install Gitlab

Install a few prerequisites.

Clone Gitlab

We create a gemset for Gitlab to not pollute the global gemset. To automate this we will use a .rmvrc inside the Gitlab directory. RVM will make sure it will be loaded automatically whenever you enter the directory.

cd into directory to make rvm use the .rvmrc and accept with “y”.

Check your current gemset with

It should show something like “ruby-1.9.3-p0@gitlab”.

Now you might need to update Gitlab’s Gemfile (e.g. add the mysql2 gem for MySQL databases).

Now install the gems necessary for running Gitlab.

You may need to run “bundle install –no-deployment” to pick up changes to the Gemfile and rerun the previous command.

Edit config/gitlab.yml to configure Gitlab. If you have followed this howto you should only need to update the “email” section and the “host” option in the “git_host” section.

You might want to edit config/application.rb and update the time zone and locale configurations.

Edit config/database.yml and set up your database configuration.

Now set up and initialize your database.

Install with Passenger + Apache

(todo)

Install with Thin + Nginx

(todo)

Result

😀