Skip to content
accelerando

How to change the image assets path in Rails

01-Nov-09

I found no other way to change the path of “images” in a Rails application than monkey patching the AssetTagHelper like so:

module ActionView
  module Helpers #:nodoc:
    module AssetTagHelper
      def image_path(source)
        compute_public_path(source, 'static_images')
      end
      alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
    end
  end
end

Redcloth / Textilize :hard_breaks is broken in Rails 2.3.4

24-Sep-09

The default behavior for quite a long time was :hard_breaks in the textilize helper method in rails.

Hard Breaks means: All line breaks are turned into <br />’s.

Somebody changed the textilize helper in “actionpack-2.3.4/lib/action_view/helpers/text_helper.rb” in 2.3.4 and added the ability to pass some options but broke the default behavior here:

def textilize(text, *options)
        options ||= [:hard_breaks]
        # ...

Options will never be null.

I fixed this by monkey patching the module through the following code in config/initializers/textilizepatch.rb

module ActionView
  module Helpers
    module TextHelper
      def textilize(text, *options)
        options = [:hard_breaks] if options == nil || options.size == 0
 
        if text.blank?
          ""
        else
          textilized = RedCloth.new(text, options)
          textilized.to_html
        end
      end
    end
  end
end

Such changes should be tested… *grml*

SVN: Revert to previous version

09-Sep-09

Just a quick reminder:

To revert a complete working copy or a single file use:

svn merge -rHEAD:PREV .
# or
svn merge -rHEAD:PREV path/to/file
svn commit -m "reverted"

Logging in Rails outside a controller

25-Aug-09

You can use

RAILS_DEFAULT_LOGGER.log "foobar"
# or
Rails.logger.log "blah"

outside a controller for logging.

Creating a self containing mvc application with Sinatra

29-Jul-09

I recently presented biking.michael-simons.eu, a Sinatra based application written in Ruby.

Its main purpose for me is to keep track of my milage in 2009.

Although the application is completely self containing it has some nice features:

Simple setup

The application is simple and i didn’t want to use a “big” database like PostgreSQL or MySQL, therefore i choose SQLite. Together with DataMapper i can use the following and have a running SQLite database in no time:

configure :development do
  DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/biking.dev.sqlite3")
end
 
# A milage is stored for a bike at the beginning of the month.
# Its the milage of the bike at this point of time.
class Milage
  include DataMapper::Resource
  property :id,          Integer, :serial => true
  property :when,        Date, :nullable => false
  property :value,       BigDecimal, :nullable => false, :precision => 8, :scale => 2
  property :created_at,  DateTime
 
  belongs_to :bike
 
  validates_is_unique :when, :scope => :bike
 
  is :list, :scope => [:bike_id]
end
 
DataMapper.auto_upgrade!

Creating charts with the google_chart gem

Google has a nice api for creating charts, the Google Charts API. The charts are created through URL parameters and as we know, most browsers limits the length of a query string. And for that, the parameters must be encoded.

Nobody wants to do it themselves so there is gchartrb that i use:

  # Create the google chart
  # All will include all float values
  all = Array.new
  @gc = GoogleChart::BarChart.new('800x375', nil, :vertical, false) do |bc|
    @bikes.each do |b|
      # Help is an array of floats for all periods
      hlp = b.milage_report.collect{|mr| mr[:milage]}
      # and gets added to the chart for that bike
      bc.data b.name, hlp, b.color
      all += hlp
    end
 
    hlp = Array.new
    # Get all periods. It sure can be done via a group by but 
    # the app should be database agnostiv
    periods = Milage.all.collect{|m| m.when.strftime '%m.%Y'}.uniq
    # Get all other trips in this period and also add them to the chart
    periods.each {|period| hlp <<= (AssortedTrip.sum(:distance, :conditions => ['strftime("%m.%Y", "when") = ?', period]) || 0.0)}
    all += hlp
    bc.data "Assorted Trips", hlp[0,hlp.length-1], "003366"
 
    # Define the labels
    bc.axis :x, :labels => periods, :font_size => 16, :alignment => :center
    # and the range from 0 to the highest value
    bc.axis :y, :range => [0,all.max], :font_size => 16, :alignment => :center
    bc.show_legend = true
    bc.grid :x_step => 0, :y_step => (100.0/all.max)*25.0, :length_segment => 1, :length_blank => 0 if all.size > 0
  end if @bikes.size > 0

The gc object will be used as simple as

%img{:src => @gc.to_url, :alt => 'gc'}

Using libxml for parsing RSS feeds

It seems that i’m not going to write an application without including my daily faces project dailyfratze.de in some way.

So i decided not only to present numbers but also my biking pictures. The images are available through a Media RSS feed. The feed itself contains pointers to other pages of that very same feed.

One of the fastest ways to parse XML is LibXML and luckily, it’s available for ruby through LibXML Ruby.

Together with memcached it can be used to efficiently handle feeds like so:

def BikingPicture.random_url
  # Check if pictures are cached...
  biking_pictures = @@cache['biking_pictures']
  unless biking_pictures then
    # Start retrieving the feed
    url = @@uri.parse("http://dailyfratze.de/michael/tags/Thema/Radtour?format=rss&dir=d")
    next_page = false
 
    Net::HTTP.new(url.host).start do |http|
      # Its a media rss feed that defines previous and next feeds
      while url
        req = Net::HTTP::Get.new("#{url.path}?#{url.query}")
        xml = http.request(req).body
        # Parse the data
        doc = LibXML::XML::Parser.io(StringIO.new(xml)).parse
        # Unless this url hasn't been retrieved...          
        unless @@cache[url.to_s]            
          doc.find('/rss/channel/item').each do |item|
            # Get all pictures and store them if not already grapped
            biking_picture = BikingPicture.first :url => item['url']
            unless biking_picture
              biking_picture = BikingPicture.new({:url => item.find_first('media:thumbnail')['url'], :link => item.find_first('link').content })
              biking_picture.save
            end
          end
          # Mark this url as seen
          @@cache[url.to_s] = next_page
          next_page = true                          
        end
        # Check if there are more feeds...
        next_url = doc.find_first("/rss/channel/atom:link[@rel = 'next']")
        url = if next_url
          @@uri.parse next_url['href']
        else
          nil
        end
      end
    end
    # Get the data...
    biking_pictures = BikingPicture.all()
    # ...and store it
    @@cache.set 'biking_pictures', biking_pictures, 3600
  end
  biking_pictures.sort_by{rand}[0]
end

Sinatra, Passenger and Memcached

Speaking of being efficient, the application certainly runs through Phusion Passenger aka modrails.

Running a Sinatra app under passenger is as simple as this: Create a directory for a new vhost, setup a structure like

biking
\- public
 - tmp
 - config.ru
 - biking.rb

and let config.ru contain the following:

root_dir = File.dirname(__FILE__)
 
require 'biking.rb'
 
set :environment, ENV['RACK_ENV'].to_sym
set :root,        root_dir
set :app_file,    File.join(root_dir, 'biking.rb')
disable :run
 
run Sinatra::Application

The apache vhost is configured like so:

<VirtualHost *>
        DocumentRoot "/path/to/the/applications/public/folder"
        RackBaseURI /
</VirtualHost>

I have turned off the Rack and Rails Autodetect features (RailsAutoDetect off, RackAutoDetect off) so i need to explicitly turn them on for a vhost.

After that, the application is running and thats all there is.

But wait. I’ve written before about problems with memcache-client and Passenger and this problems needs to be addressed in a Rails as well as Rack application.

I handle them in Sinatra as follows:

configure do
  # Create a global memcache client instance...
  @@cache = MemCache.new({
    :c_threshold => 10000,
    :compression => true,
    :debug => false,
    :namespace => 'some_ns',
    :readonly => false,
    :urlencode => false
  })
  @@cache.servers = 'some_server:11211'
 
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      @@cache.reset if forked
    end
  end
end

If PhusionPassenger is available, install an event handler that resets the freshly forked memcached connection to avoid corruption.

Using Geonames.org

Google Maps is great but one thing that’s often forgotten is reverse geocoding. Google doesn’t offer such api (as far as i know) but GeoNames does.

I’ve written a mobile J2ME client around JSR 179 api that can push my current location to this server when i’m biking so my girlfriend can follow my rides on the map.

The client pushes latitude and longitude and the server replies with the coordinates in angles and the name of the place. The name is retrieved through the Ruby Geonames API like so:

#
# Adds a new location
#
post '/locations' do
  require_administrative_privileges
  location = Location.new params
  if location.save then
    begin
      country_subdivision  = Geonames::WebService.country_subdivision(location.latitude, location.longitude)
      places_nearby = Geonames::WebService.find_nearby_place_name(location.latitude, location.longitude).first
      (location.description = "#{places_nearby.name}, #{country_subdivision ? country_subdivision.admin_name_1 + ', ' : ''}#{places_nearby.country_name}"[0,2048].strip) and location.save if places_nearby
    rescue Exception => exc      
    end
  end
  "#{location.to_s}\n"
end

Inline templates, external resources

The thing started as a small application. PHP was out of the question but Rails was overkill, too. I just wanted one single file.

I can use the great Haml syntax as inline templates. This goes for creating HTML as well as for CSS through Sass.

To get some basics, i also include one of the W3 core styles.

The only resources that lives outside biking.rb is JQuery for easy creation of the autorefreshing code and some images, namely the cute green bikers from Greensmilies.

Summary

It’s absolutely possible to write an application with thee MVC Pattern in mind without one of the “big” frameworks in just one single script.

I would go much further with this app using just one script, but for it’s current purpose, it’s great, i think.

I hope you enjoyed reading my little annotations as i enjoyed writing them and the application. The full source code is available right through biking.michael-simons.eu and it’s published under the BSD license.

Javas String.replaceAll

21-Jul-09

The following statement

"foo baz".replaceAll("baz","$bar");

will present you an java.lang.IllegalArgumentException: Illegal group reference exception as the replacement string can contain backreferences to the search pattern as stated in Mather#replaceAll:

This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match are appended directly to the result string; each match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

Luckily, there is an easy solution:

"foo baz".replaceAll("baz",Matcher.quoteReplacement("$bar"));

Turn ruby heredocs from double-quoted into sinqle-quoted strings

17-May-09

By default, Rubys heredocs are interpreted as double-quoted strings, that is #{something} is evaluated, \r turns into newline and so forth.

You can change this behaviour by single quoting the heredoc identifier like so:

s = <<-'SINGLE_QUOTED'
#{i'm not interpreted}
SINGLE_QUOTED

Batchconvert ascii to utf8

12-May-09

Next time i see umlauts in source, I’ll scream. Loud.

In the mean time I try this:

find . -iname "*.java" -exec sh -c 'iconv -f cp1252 -t utf-8 {} > {}.utf8' \;
for i in `(find . -name "*.utf8")`; do mv $i ${i/.utf8/}; done

Before you try this, make a backup of your files. It worked for me but i don’t guaranty that your files won’t vanish.

Understanding

08-May-09

I’d love to understand what makes people create abominations such as this:

if((foo ? !bar : true))
    if(!somethingelse)
        dosomething()

I don’t want to judge anyone, the thing actually worked, but i really want to understand why people don’t see that this is crap. I’m really aware of the fact, that things tends to organically grow, but how can it be achieved to raise awareness to this fact? I mean, i’m probably only a not-totally-bad engineer, but i see things that will break or are just ugly as hell and i want my colleagues to be more aware of pitfalls.

Would make life much easier and probably more conflict free.

JAX2009: Impressionen einer Konferenz (ohne Bilder)

24-Apr-09

Wie bereits im letzten Jahr war ich auch dieses Jahr 2 Tage auf der JAX, die dieses Jahr in Mainz statt fand.

Nach zahlreichen Konferenzen in den letzten Jahren, u.a. einige DOAGs, werden meine Gefühle bzgl. dieser Veranstaltungen immer gemischter. Ich habe den Eindruck, dass die Schere zwischen guten und schlechten Vorträgen immer größer klafft. Vielleicht eine Folge dessen, dass versucht wird, immer mehr Slots zu füllen.

Bemerkenswert an der JAX dieses Jahr waren natürlich Rails Day an einem Tag, Grails / Groovy Day am nächsten.

Stefan Tilkov hielt einen gut strukturierten Vortrag über REST Technologien im Rahmen des Rails Day. Das, was er präsentierte, hätte von der Thematik auch gut in einige andere Frameworks gepasst, aber trotzdem: Die Präsentation war gelungen. Gute Mischung aus wenigen Folien und Livecoding. Spätestens danach musste dem Zuhörer klar sein, was REST bedeutet. Im selben Track präsentierte Jonathan Weiss “Advanced Deployment mit Rails” in einer Art und Weise, wie ich mir keinen Vortrag vorstelle: Viel zu viele textlich überladene Folien und zumindest fürs Publikum, eine ungünstige Themenwahl. So fiel auch leider das “advanced” weg, was im Rahmen von Fragen wie “Was ist besser, JRuby oder Grails?” (Was ist besser, Apfel oder Birne?) aus dem Publikum wohl auch gut war.

Der Vortrag Rails 3 von Gregg Pollack wurde zwar in einem gänzlich anderem Stil gehalten als Stefans, war aber trotzdem hörenswert: Frei und sicher gesprochen, ohne zu langweilen.

Auch Keynotes können langweilen: Brian Kim von Liferay sprach zum Thema “Architecting your way through recession: an open source survival kit”. Ich habe nicht eine Aussage aus diesem Vortrag behalten.

Im Gegensatz dazu “Fette Maschinen brauchen schlanke Software” von Klaus Alfert, der sehr anschaulich dokumentierte, warum funktionale oder hybrid funktionale Sprachen in den nächsten Monaten und Jahren immer wichtiger werden. Auch er langweilte nicht mit Textwüsten. Passend dazu, Ted Newards “Busy Java Developer’s Guide to Scala”. Ted ist immer wieder das reinste Vergnügen: “What’s your name? – Rüdiger. – Ok, Walther.”

Was ich nicht verstehe, dass teilweise einige Redner augenscheinlich vollkommen unvorbereitet zu einer Konferenz kommen oder aber die Zuhörer mit endlosen Slides, die aussehen wie ein “man ” in Powerpoint langweilen, anstatt Slides zur Motivation zu nutzen und zur Abwechslung einmal frei zu sprechen. Die Vorträge, die mich wirklich innerlich aufgeregt oder mir wörtlich die Augen zufallen ließen, erwähne ich mal nicht namentlich.

Im Gegensatz zur DOAG ist das Publikum auf der JAX jünger und durchmischter, aber auch teilweise deutlich unhöflicher, was jedes Mal bei der Nahrungsmittelausgabe offensichtlich wurde: Sturm auf das Büffet. Oh wie ich es hasse, wenn Menschen komplett alles an Erziehung vergessen, wenn irgendwo kostenloses Essen rumsteht. Generell zum Essen: Liebe Jaxcon Menschen, lasst doch die Gimmicks wie Taschen, Rucksäcke und Zeugs sein und bestellt etwas höherwertiges Catering :)

Trotzdem, einige nette Gespräche habe ich geführt, u.a. mal ein paar Kollegen von Codecentric kennen gelernt und einen Vortrag von Mirko gehört (Flush and Clear: O/R Mapping Pitfalls), nachdem ich immer noch über die Sinnhaftigkeit eines select distinct nachdenke am manyToOne Ende einer 1-n Beziehung, um das zusätzliche Select zu sparen ;) . Desweiteren traf ich witziger weise einen Kollegen aus Ausbildungszeiten, die Welt ist wirklich klein.

Alles in allem: Eine gute Veranstaltung, 3 oder 4 Tage wären mir aber definitiv zu viel oder ich müsste mir angewöhnen, aus Vorträgen, die mich aufregen, einfach raus zu gehen. Mal schauen, wie es nächstes Jahr wird… Eventuell kann man ja bis dahin DOAG und JAX zusammenwerfen ;)

Close
E-mail It