Lukas Z's Blog

Javascript Popup Windows With Rails

Javascript popups still have their uses. Here is a tiny snippet that one can use to create Javascript popup-windows in a Ruby on Rails application in an elegant fashion.

In your application.js (or whatever .js-file you prefer) include this:

$('a[data-popup]').live('click', function(e) { 
  window.open( $(this).attr('href'), "Popup", "height=600, width=600" ); 
  e.preventDefault(); 
});

Then, in your views add the data-popup-attribute to the link_to-helper:

<%= link_to( 'Open Popup-Window', popup_window_path, 'data-popup' => true ) %>

This is basically taken from [here](http://stackoverflow.com/a/8828841 “”). I have just added two extra parameters to window.open().

The Gist of GIS With MySQL and Ruby on Rails

I am currently working on a small prototype application that requires my rails app to store and process geospatial information. Basically, I am storing and querying longitude/latitude coordinates.

I have been working on projects that had similar use-cases before, but when I joined everything was set up already. Also, usually it was PostgreSQL with PostGIS. This time though, I had to do it myself and with MySQL.

So, since other people might have the same idea, I figured I’d put a few code snippets here so you can get started quicker.

First, here’s my Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.11' # you should update this to 3.2.13 now! ;)

gem 'mysql2'
gem 'spatial_adapter', :git => "https://github.com/descentintomael/spatial_adapter.git"
gem 'geokit-rails3'

# ... some other gems here

This is how a migration might look like (I don’t think you still need the ENGINE=MyISAM line with a current MySQL version):

class CreatePoints < ActiveRecord::Migration
  def change
    create_table(:points, :options => 'ENGINE=MyISAM') do |t|
      t.string :name
      t.column :lat, :decimal, :precision => 15, :scale => 10
      t.column :lon, :decimal, :precision => 15, :scale => 10
      t.timestamps
    end
  end
end

I also added indexes on both decimal-fields.

And here is a bit from my model including two simple queries.

class Point < ActiveRecord::Base
  attr_accessible :lat, :lon, :name

  acts_as_mappable :default_units => :miles,
                   :default_formula => :sphere,
                   :distance_field_name => :distance,
                   :lat_column_name => :lat,
                   :lng_column_name => :lon


  def self.nearest_points( lat, lon )
                Point.within( 10, :origin => [lat, lon] ).order( "distance asc" )
  end

  def self.at_point?( lat, lon )
                Point.within( 0.02, :origin => [lat, lon] ).order( "distance asc" ).first
  end

  # ... etc.
end

Check out the geokit-rails3 README on Github for some examples.

I hope I just saved someone an hour or so of googling.

sSMTP on an Ubuntu Server

Usually when I decide to “quickly set up something on a server” it takes much longer than anticipated. Not so with sSMTP.

Basically it’s a simpler way to send emails from the the commandline. In my case with Gmail. Check out this article on how to set it up. I have also combined it with Logwatch and Fail2Ban and it worked immediately.

In the case of Fail2Ban just one extra line in the config-file was needed:

mta = ssmtp

All of this was inspired by this post (via Hacker News).

Shutterbug

Just a picture I took a few weeks ago in Hamburg. This was taken with my phone. It shows a bit of the harbor on a cold January morning. The light was great. There is ice on the water.

Picture of Hamburg harbor

Open the image in a new window to see the picture in high resolution.

Quick Tip: Suppress Output in Rails Console and Irb

Let’s say you have a collection with 10.000 records in your database and you want to put them all in a large array for some reason. With Rails, you can write something like this:

records = Thing.all

Now you can go get yourself a coffee, because the content of all your records will be output to the console window and it will do so for a while.

Here’s the quick tip: If you want to suppress the output, simply do something like this:

records = Thing.all; 0

Problem solved. Easy, but I didn’t think of it immediately..

Update XCode in the App Store

A screenshot of XCode in the App Store

I attempted to update XCode using the App Store app yesterday and was presented with the message “To update this application, sign in to the account you used to purchase it.”. This message however did not point to the real reason why it didn’t work.

What I did was disabling Spotlight Search indexing some time ago, because it affected performance. Turns out, the App Store app uses Spotlight search and in order for XCode to update, it needs to be turned on.

To turn Spotlight Search on, open a terminal and type the following:

sudo mdutil -a -i on

Also make sure you are signed in to your iTunes-Account.

Ember.js Code-Walkthrough (Video)

Because I was working a lot in the (Rails-) backend lately, I decided to find myself a small frontend-centered side-project that I could work on in my spare time. Because I think that Yehuda, Tom and the others are clever people, I chose Ember.js.

The idea was a small application for salespeople. And now, after a week, I am actually pleasantly surprised how little code I had to write to make it work.

However, I did have to really do a lot of digging through their docs, the ember-sources and ask silly questions on StackOverflow and in the emberjs irc-channel, in order to figure out how to make my app work. There isn’t too much good documentation available now, it seems. Even the Peepcode-tutorial (naturally) didn’t answer all my questions.

So I thought it would be good idea to make a little code-walkthrough video myself. To, hopefully, help the next guy or girl who wants to learn Ember.js.

So yes, here it is:

UPDATE: Contact me if you want a high resolution version of that video. I’ll send you a link.

Ha. I seem to sound a little Russian when I speak English!

And here’s the source-code if you want to download it.

I am obviously a total Ember.js-noob, so don’t take my word for it that I am doing things properly. (I am probably not.) Consider this a little snapshot of one man’s learning progress.

Perhaps I will write an update or make an updated video to show you new things I have learned.

I also have some links with regards to ember.js here.

EDIT: (March 26th , 2013)

I have reverted a merged pull request, because it introduced a bug. Example-App should work fine now.

Multi-Clipboard in OSX

The registers in Vim were always one of my favourite features. It’s like having multiple clipboards. You can copy a string into a register and then copy another. Then both strings are available for pasting. (Not just the last one.)

Today I was going back and forth between applications and decided to find something similar for OSX. It turns out there are quite a few apps one can use.

I have decided to try Clip-Menu, because it’s free, because it handles more than just text and because has a built-in snippet manager.

A screenshot of ClipMenu

Snippets are great. You define keywords for text that you use often.

For example I could define a snippet with the key “hello” and a text “Hello, how are you?”. Then, in the case of Clip-Menu, I just open the Snippet-Manager with ⌘⇧B and select the snippet “hello”, which in turn pastes the “Hello, how are you?” into whatever input field is active at the moment. Useful.

But back to multiple cliboards..

It’s simple. You just copy things like you would normally do using ⌘C and ClipMenu remembers all of them. Then when you want to paste the last thing you copied, you can still use ⌘V, but you can also press the ClipMenu-Hotkey ⌘⇧V (easy to remember, I think) and a popup appears that lets you select one of the things you copied before.

I recommend you give it a try.

As always, there are alternatives for Linux and Windows, which you’ll have to google yourself.