Archive for the ‘Ruby On Rails’ Category

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

Sonntag, 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
    end

    def run_with_timelogging(*args)
      beginning = Time.now
      begin
        run_without_timelogging(*args)
      ensure
        puts "executed initializer: #{name} took #{Time.now - beginning} seconds"
      end
    end
  end
end

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

And require it before your application:


require 'rails_initializer_time_logging'

module YourApplication
  class Application < Rails::Application
    ....
  end
end

Stale PID-Files in development

Sonntag, 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'
StalePidCleaner.check

StalePidCleaner:


class StalePidCleaner
  def self.check
    fName = "./tmp/pids/server.pid"
    if(File.exists?(fName))
      puts "found pidfile: " + fName.to_s
      pid = File.read(fName).to_i
      if(pid < 1)
        puts "invalid pid? " + pid.to_s
      else
        if(!alive?(pid))
          puts "cleaning stale pidfile"
          File.delete(fName)
        end
      end
    end
  end

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

Hope it helps also someone else

Paperclip 4.0 and non deactivatable MediaType Spoofing

Sonntag, 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?
  end

  def fixed_spoofed?
    false
  end

end

Paperclip::MediaTypeSpoofDetector.send :include, SpoofingFix

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

detecting bots by user-agent in rails

Dienstag, Dezember 31st, 2013

First i thought using the browser-gem and browser.bot? 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

  def self.bot?(agent)
    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)
   end

end

In my Base-Controller:


def bot_request?
  uAgent = request.env["HTTP_USER_AGENT"]
  if(uAgent.nil?)
    false
  else
    BotDetection.bot?(uAgent)
  end
end

A list of user-agents may be found at http://www.user-agents.org/

ActiveRecord disallow changes using unchangeable validation

Dienstag, 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
end

unchangeable_validation.rb

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

module UnchangeableValidation
  extend ActiveSupport::Concern

  class UnchangeableValidator < ActiveModel::EachValidator

    def initialize(options)
      super(options)
    end

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

 module ClassMethods

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

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

unchangeable_validation_test.rb

require 'test_helper'

class UnchangeableValidationTest <  ActiveSupport::TestCase

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

end

Squirrel SQL Client and SQLite Databases

Dienstag, 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.

Why?

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

In the end it was easy.

  1. Download Squirrel SQL Client http://squirrel-sql.sourceforge.net/
  2. Get this jdbc-Adapter for SQLite: https://bitbucket.org/xerial/sqlite-jdbc/overview
  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: https://bitbucket.org/xerial/sqlite-jdbc/wiki/Usage
    4. Class-Name: org.sqlite.JDBC
  6. Add Alises for your DB and connect to them

delayed_job and localization (i18n)

Montag, 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?

(mehr …)