Knowing order of Rails 4 Engine or Railtie Initializer execution and their duration

Februar 2nd, 2014

In Rails 3 i found a nice article to know how to hook in your engine or railtie initializers.

It printed the order of initializer execution. With Rails 4 this did not work any longer.

But i since i did not need it any longer, i removed it.

Now i wanted to know how long did my initializers take? Reason: Somehow it got slower with some changes i did.

So i came up with this little piece of code that prints the name of the executed initializer and how long it took. So now i can detmine in the logs when a initializer was executed and which took too long.

module RailsInitializerTimeLogging
  module TimeLoggedInitializer
    def self.included(base)
      base.send :alias_method, :run_without_timelogging, :run
      base.send :alias_method, :run, :run_with_timelogging

    def run_with_timelogging(*args)
      beginning =
        puts "executed initializer: #{name} took #{ - beginning} seconds"

Rails::Initializable::Initializer.send :include, RailsInitializerTimeLogging::TimeLoggedInitializer

And require it before your application:

require 'rails_initializer_time_logging'

module YourApplication
  class Application < Rails::Application

Stale PID-Files in development

Februar 2nd, 2014

Somehow in my Development-Environment (Windows with Eclipse) webrick comes up with stale PID-Files.

I stop the Server, start it again and sometimes (not always) i get the message:

A server is already running.

This happend since i updated to rails-Version X.

But now i finally figured out (after googling and googling and finding nothing helpful) how to make it work again.

In application.rb before declaration of your own application insert:

require 'stale_pid_cleaner'


class StalePidCleaner
  def self.check
    fName = "./tmp/pids/"
      puts "found pidfile: " + fName.to_s
      pid =
      if(pid < 1)
        puts "invalid pid? " + pid.to_s
          puts "cleaning stale pidfile"

  def self.alive?(pid)
      Process.kill(0, pid)
     rescue Errno::ESRCH, TypeError # Process is dead

Hope it helps also someone else

Paperclip 4.0 and non deactivatable MediaType Spoofing

Februar 2nd, 2014

Paperclip 4 has an enhancement to detect MediaType Spoofing.

I’m with them, security is good and it’s better something like this is integrated.

But it was a change that you cannot turn off and when it does not work, you’ve got to live with it.

In my case:

  • Tests started failing, even when type was correctly set
  • The command that was executed did not work / was not found (did not look deeper into it)
  • SWFUpload and Paperclip and Spoofing Detection did not seem to work together.

So till it can be officially turned off (found an issue where it seems they want to integrate this) i came up with this as an solution:

module SpoofingFix
  def self.included(base)
    base.send :alias_method, :original_spoofed?, :spoofed?
    base.send :alias_method, :spoofed?, :fixed_spoofed?

  def fixed_spoofed?


Paperclip::MediaTypeSpoofDetector.send :include, SpoofingFix

We just say it is always unspoofed and so we can work like before 😉

Additions to Eclipse MemoryAnalyzer for hunting Memory Leaks with Swing

Januar 4th, 2014

Some Additions of mine for Eclipse MemoryAnalyzer to hunt leaks in Swing have now been made public for you to use.

Visit this page for further information.


Hope you like em. Feedback welcome.

detecting bots by user-agent in rails

Dezember 31st, 2013

First i thought using the browser-gem and would give me the correct answer. But somehow it did not work. So i am now using this litte snippet to detect bots.

class BotDetection

    matches = nil
    matches = agent.match(/(facebook|postrank|voyager|twitterbot|googlebot|slurp|butterfly|pycurl|tweetmemebot|metauri|evrinid|reddit|digg|sitebot|msnbot|robot)/mi) if agent
    return (agent.nil? or matches)


In my Base-Controller:

def bot_request?
  uAgent = request.env["HTTP_USER_AGENT"]

A list of user-agents may be found at

ActiveRecord disallow changes using unchangeable validation

Dezember 31st, 2013

Not allowing changes to be made to e.g. the login of the user only by the UI is in my eyes not enough.
The model should also ensure, that changes cannot be made once assigned.

So i tried to find an validation that does this for me.
But the once i found did not quite work as i expected.

So i wrote my own that i want to share:

class User < ActiveRecord::Base
  validates_unchangeable :login


require 'active_support/concern'
require 'active_model'
require 'active_record/base'

module UnchangeableValidation
  extend ActiveSupport::Concern

  class UnchangeableValidator < ActiveModel::EachValidator

    def initialize(options)

    def validate_each(object, attribute, value)
      if(!object.new_record? && value.present?)
        abChanged = attribute.to_s + "_changed?"
          object.errors[attribute] << (options[:message] || "cannot be changed once assigned")

 module ClassMethods

   def validates_unchangeable(*attr_names)
     validates_with UnchangeableValidation::UnchangeableValidator, _merge_attributes(attr_names)

ActiveRecord::Base.send(:include, UnchangeableValidation)


require 'test_helper'

class UnchangeableValidationTest <  ActiveSupport::TestCase

  def test_login_unchangeable
    instance =
    instance.login = "login"!
    instance.login = "changed"
    assert_equal instance.errors[:login].first, "cannot be changed once assigned"


Squirrel SQL Client and SQLite Databases

Dezember 31st, 2013

Working with RubyOnRails i use sqlite in development.

In my dev-environment (windows) i used to use SQLiteBrowser to look into the DB. But the usability was not what i was used to.

So i tried to get Squirrel SQL Client to work with SQLite and had my problems integrating the two together.


Because i googled and googled and did not find a working answer.

In the end it was easy.

  1. Download Squirrel SQL Client
  2. Get this jdbc-Adapter for SQLite:
  3. Throw the lib in the lib-Directory of SQuirrel
  4. Startup Squirrel
  5. Create new Driver
    1. Name: SQLite
    2. Example-Url: jdbc:sqlite:<PATH>/mydatabase.db
    3. Website-Url:
    4. Class-Name: org.sqlite.JDBC
  6. Add Alises for your DB and connect to them

Manually Creating HeapDumps in Java honoring +XX:HeapDumpPath JVM Option

Dezember 30th, 2013

+XX:HeapDumpPath is a JVM-Option that is honored by the HeapDumpOnOutOfMemory JVM-Option.

With it we can define where HeapDumps are written to the Disk.

Why do we need this?

Well, running multiple servers we may want to have them dumped centrally on a mapped network drive specifically for logfiles or dumps. Also maybe we sized the Partition for the JVM-Process too small so that a dump will not be able to be written into it (ok, disk space is cheap, even with a raid, but often we save money at the wrong end).

But if i trigger an heapdump directly (like with jconsole) i often only write the name of the dump into it or want to have it generated. Normally those dumps will now be written into the working directory of the jvm.

To avoid this, we can use the following Code to have it honored and registered as our own MBean that we will use from now on.

Read the rest of this entry »

delayed_job and localization (i18n)

Dezember 30th, 2013

delayed_job is a nice gem for executing work in the background in Ruby On Rails.

But what happend to me is that mails did not get send correctly. I was expecting to get them send in german and not english (default locale).

What happend?

Read the rest of this entry »

1blu VServer Neuinstallation und EMails

Dezember 30th, 2013

Mails, das ganz alltägliche Kommunikationsmedium.

Blöd wenn keine Mails ankommen, oder?

Nach Neuinstallation meines 1blu VServers mittels Template sind keine Mails mehr angekommen. Von der Konfiguration her nutzte ich keinen eigenen Mailserver, sondern den default (globaler von 1blu).

Nur warum sind dann Mails die über WordPress gesendet wurden nicht angekommen?


Ursache: Das Template mit Plesk hat leider einen kleinen Haken aktiviert, der dafür sorgt dass der VServer selbst sich als Mailserver sieht. Und weil keine Mailaddressen dort eingerichtet waren (warum auch) droppte er diese.


Lösung: Im Plesk Control Panel zu folgendem Eintrag navigieren:

E-Mail => E-Mail Einstellungen

Hier den Haken „E-Mail-Dienst auf Domain aktivieren“ deaktivieren.


Dann schlägt auch nicht:

Vorgehensweise bei E-Mails an nicht-vorhandene Benutzer

mit Standardwert zurückweisen zu.