detecting bots by user-agent in rails

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

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

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)

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?

„delayed_job and localization (i18n)“ weiterlesen