Rails cheat sheet: Active Record Callbacks

Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state.

Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:

Creating an Object

before_validation
after_validation
before_save
around_save
before_create
around_create
after_create
after_save

Updating an Object

before_validation
after_validation
before_save
around_save
before_update
around_update
after_update
after_save

Destroying an Object

before_destroy
around_destroy
after_destroy

Delphi – File IO with TStringList

TStringList in the Classes namespace provides a really quick and  easy way to read (LoadFromFile) and write (SaveToFile) files.  For instance, if you want the entire file contents read in to a TStringList you can do the following.

function TSomething.ReadFile(filePath : string) : TStringList;
begin
    if FileExists(filePath ) then
    begin
        result := TStringList.create();
        result.LoadFromFile(filePath);
    end
    else
        result := nil;
end;

Ubuntu Uncomplicated Firewall Setup

Uncomplicated Firewall (ufw) is the default firewall configuration tool on Ubuntu, providing an easy to use wrapper around IPTables which makes dealing with firewall rules a breeze.

For this basic example, we want to allow access to our server on ports 22 (SSH), 80 (HTTP) and 443 (HTTPS).

Warning.

It is possible to firewall yourself out of your server when changing rules, always make sure you have either physical access to the machine or an alternate point of entry such as a web shell in the case of a VPS.  It will save you a support ticket!
Step 1. Tell ufw to create our allow rules

sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443

Step 2. Enable ufw

sudo ufw enable

Now that ufw is up and running, test the ports to ensure you have access.
If you want to view the rules created by ufw, they can be found in

/lib/ufw/user.rules

For more in-depth information about ufw configuration check out the Ubuntu server guide documentation.

Luhn Validation Ruby Gem

Luhn_Validation

Introduction

Luhn_Validation is a ruby gem implementing the Luhn (Modulus 10) algorithm commonly used for credit card validation.

Important

This software can only be used to check if a given number passes the Luhn algorithm. In cannot determine if a given number belongs to a credit card that is legitimate, in service or in any way valid as it does not interact with any financial institutions.

Refer to the License for further details.

Installation

gem install luhn_validation

Use

Require the gem:

require 'luhn_validation'

Instantiate a validation object:

luhn_val = LuhnValidation.new

Validate the credit card number. Note that basic string sanitising is performed, acceptable formats include:

  • Alpha character separated strings such as hyphens (4444-3333-2222-1111) or whitespace (4444 3333 2222 1111) etc.
  • FIXNUM

valid? returns true if the number passes luhn validation, false otherwise.

luhn_val.valid?('4444-3333-2222-1111')

You can also access the validation object’s more recently processed number via the cc_dirty and cc_clean attributes.

luhn_val.cc_dirty # => '4444-3333-2222-1111'
luhn_val.cc_clean # => 4444333322221111

Links