traq updated

written by Raphael

Just recently traq reached v0.1. There have been three notable changes since its initial release:

  • support for bash autocompletion. The setup instructions have been updated accordingly.
  • the generated data files are now stored in a separate directory, which can be configured using the TRAQ_DATA_DIR environment variable. Pascal Hartig was kind enough to suggest this separation and also issued a pull request on github.
  • the directory structure contains a new subdirectory, grouping entries by the year they were created in.

If you are using traq you'll need to follow the setup instructions a second time and move all your data files to the newly created data directory.

traq is getting better every time the code base changes, and I really like the ability to track times using my CLI.

bottom line: if you like a CLI you should probably give traq a try.


Using will_paginate with Arrays

written by Raphael

I really like will_paginate for Ruby on Rails because it's easy to setup and the intent of pagination is communicated well.

The only thing that bothers me is that it can be used with ActiveRecord only. My blog runs on Rails without ActiveRecord and all posts are kept in a git repository as simple markdown files. So I needed to write a wrapper that returns a WillPaginate::Collection and works on Arrays.

I came up with this:

# encoding: utf-8
require 'will_paginate'
require 'will_paginate/collection'

module WillPaginateArrays
  def paginate params
    params[:per_page] ||= 5
    params[:page] ||= 1

    return ::WillPaginate::Collection.create(
         params.fetch(:page),
         params.fetch(:per_page),
         self.length) do |pager|
       first_index = (pager.current_page - 1) * pager.per_page
       last_index = pager.current_page * pager.per_page

       pager.replace self[first_index...last_index]
    end
  end
end

The test_unit tests for the above snippet looks like this:

# encoding: utf-8
require 'will_paginate_arrays'
require 'test_helper'

class WillPaginateArraysTest < ActiveSupport::TestCase
  test "usage" do
    array = Array.new
    array.extend WillPaginateArrays
    assert array.respond_to?(:paginate)
  end

  test "#paginate follows per_page option" do
    array = (1..10).to_a
    array.extend WillPaginateArrays

    assert_equal 2, array.paginate(per_page: 2).length
    assert_equal [1, 2], array.paginate(per_page: 2)
  end

  test "#paginate follows page option" do
    array = (1..10).to_a
    array.extend WillPaginateArrays

    assert_equal (6..10).to_a, array.paginate(page: 2)
  end

  test "#paginate has total_pages" do
    array = (1..10).to_a
    array.extend WillPaginateArrays

    assert_equal 3, array.paginate(per_page: 4).total_pages
  end

  test "#paginate handles last page correctly" do
    array = (1..10).to_a
    array.extend WillPaginateArrays

    assert_equal [9, 10], array.paginate(per_page: 4, page: 3)
  end
end

As you can see usage is fairly easy:

  1. create a new array.
  2. extend the array with the WillPaginateArrays module
  3. call #paginate just like you normally would and use the will_paginate helper in your views.

Done!


a Review of 2012

written by Raphael

Today 2012 ends. Time to review the past year. This is just a short list of things I found most important in my life:

  • on September 6th I've married Katharina Bauer. She is by far the most important person in my life. I can talk to her about anything and everything. She's always helping me out in some kind, and I'm really lucky to have her as my wife. Thanks for the great time, Katharina - and thanks for your support. To many many more lucky years. I love you :-*

  • on September 3rd I've been working at weluse GmbH for a whole year now. I've enjoyed working here. For one because of my great colleagues, and of cause my great boss. Thanks a lot, Oliver! You've been really supportive this past year, whenever I needed a little more time to get my private life together.

  • in August the 23rd Wacken Open Air festival happened - and for the very first time I've gotten a tripple A wristlet. I've been working for ICS Festival Service GmbH multiple years now and 2012 was by far the best. Thanks for the great opportunity, great people and great work we've been doing. You guys rock. Seriously.

Many more things happened, and I'm really greatful for all the support I've had. Thanks goes out to all my friends and my family, all people who are close to me and kept on walking with me. I love you guys. Thanks!