Rails3 and Datamapper quick start May 21st, 2010



Rails3 is almost released and I decided to try it out with Datamapper to see how things work. Things work AWESOME!

Here's a rundown of the steps I used:


gem update --system # you need rubygems >= 1.3.6
git clone http://github.com/rails/rails.git
cd rails
rake install
cd ..
rails my_dm_rails3_app -m http://github.com/jeremyd/rails-templates/raw/master/dm_rails_master.rb
cd my_dm_rails3_app
bundle install
Note: I cloned snusnu's repository here and modified to install a Gemfile that uses prerelease rubygems instead of git repository checkouts. This seems much faster and there were less dependency problems for me..

acts_as_serializable March 15th, 2008

You heard it here first folks. Acts_as_serializable is making it's public debut today! Yes it may be horrifically stuck in a brain twister of recursion that could bring your server to it's knees when used improperly, AND THAT'S why I feel it is finally READY.

Are YOU ready? Here we go:

script/plugin install http://svn.rubyonlinux.org/acts_as_serializable/trunk

Haha, I know no one is even reading this. That's ok because I need someone to help me finish coding it first :) It's really coming along though! Even as I'm writing this, I would describe it to you but instead I'm going to work on the documentation and tests.

...

OK, I think the way I was approaching it was too crazy. I think this serializable thing is better suited to be some kind of helper like the TestFactory. Mocks etc should be used to stub out required relationships. Building a generic solution is way less effective than teaching how to export your data serialized in yaml form (custom).

Example: ..coming soon If what you see in the SVN interests you, drop me a comment.

Textmate style rails plugins for VIM. October 11th, 2007

"Textmate is cool, but VI is forever"
Here's how to get some textmate like potential out of VIM using plugins. I have the following plugins installed: project.vim, genutils.vim, rails.vim, multvals.vim, rubycomplete.vim and surround.vim.
non-insert mode

what to typewhat it does
:help surroundshows help for the surround plugin
yss=surrounds the current line with <%= %>
yss CTRL-Eturns the current line into a block <% -%>newline char<% end -%>
yss-surrounds the current line with <% -%>
CTRL-X CTRL-Obrings up the OMNI complete window for the current object/method
yssecreates an ending for the current block. ie: an 'end' or closing brace
CTRL-G ecreates an ending for the current block. use in insert mode
insert mode
what to typewhat it does
:help surroundshows help for the surround plugin
CTRL-G =surrounds the current line with <%= %>
CTRL-G CTRL-Eturns the current line into a block <% -%>newline char<% end -%>
CTRL-G -surrounds the current line with <% -%>
CTRL-X CTRL-Obrings up the OMNI complete window for the current object/method
CTRL-G eputs an 'end' on the next line

Generating KML / XML on the fly in Ruby on Rails June 7th, 2007

KML can now be used both in Google Maps and Google Earth.
I wrote a simple KML generator using Builder and Rails.
I also used an RSS parser to read geographic coords from an RSS feed which is in use now at Baleen.org, but that is a code story for another day..

I wanted to post some code snips from the project, cause it's cool.

The view "kml.rxml"

xml = Builder::XmlMarkup.new(:indent => 2)

xml.instruct! :xml

xml.kml( "kmlns" => "http://earth.google.com/kml/2.1" ) do
  xml.Document {
    xml.name("someplaces")
    xml.description("someplaces.org locator")
    xml.Style( "id" => "highlight" ) {
      xml.IconStyle {
        xml.Icon {
          xml.href("http://URL to transparent 32x32 png image.png")
        }
      }
    }
    Place.find_all.each do |p|
    xml.Place {
      xml.name(p.tooltip)
      xml.description {
        x=p.popup.dup
        xml.cdata!("#{x.slice!(1..250)}<br><a href=#{p.ext_url}>..Read more</a>")
        }
      xml.styleUrl("#highlight")
      xml.Point {
        xml.coordinates("#{p.x},#{p.y},4")
      }
    }
    end
  }
end
In the controller, you must specify the following headers.
def kml
  render_without_layout
  @headers["Content-Type"] = "application/vnd.google-earth.kml+xml kml; charset=utf-8"
  @headers["Content-Disposition"] = "attachment; filename=someplaces.kml"
end
Here is the migration I used: "001_create_places.rb" For storing latitude/longitude and a few other useful Google Earth / Maps data to be used in the KML
class CreatePlaces < ActiveRecord::Migration
  def self.up
    create_table :places do |t|
      t.column :x, :string
      t.column :y, :string
      t.column :ext_url, :string
      t.column :tooltip, :string
      t.column :popup, :text
    end
  end

  def self.down
    drop_table :places
  end
end
Use a route to behave as a downloadable file.
map.connect '/someplaces.kml', :controller => 'places', :action => 'kml'
That's all you need to start generating KML on the fly with Rails. Have fun!

Bonus Code !! Example Javascript for Google Maps API that loads up KML from an URL.

var geoXml = new GGeoXml("http://URL to someplaces.kml");
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(37.12755479789267,-112.7487179140864),3);
	map.setMapType(new GMapType([G_SATELLITE_MAP.getTileLayers()[0]], G_NORMAL_MAP.getProjection(), "test", {}));
	map.addOverlay(geoXml);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
      }
    }
Also, make sure you have 2.x as the version string where you delcare the Javascript, otherwise your placemarks might go missing!
<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=YOURKEY>

Leave a comment if this tutorial helped you. Thanks!

Using DRb and ruby to control mplayer and record/playback HDTV. March 14th, 2007

I wanted HDTV broadcast in Ubuntu, so I bought a PC-HDTV 5500 tuner. It needs a 2.6.18 or higher kernel to work.

I’ve been working on some scripts to control my TV. So far I have a rudimentary server and client along with a HDTV record script that uses a direct copy from the device using the linux tool ‘cat’. My system @kernel 2.6.19 had a bug which causes cat to overflow.. So there is error detection and resume recording after a crash.

I’ll be using linux CRON to schedule recordings.

It is a work in progress. Here is the SVN repository: http://svn.rubyonlinux.org/mplayerctl/

I used Kazuki Takemura’s code to control mplayer, and extended it to use DRb connections.

Go ahead and comment if you are interested in helping with the project. I want it to become a simpler MythTV style thing eventually.. SIMPLER is better.

New commits, March 2008. Rudimentary recording and scheduling via cron are now possible. Need to improve the actual mplayer part now but it’s working so that’s great.