Changes to ActiveRecord::Dirty in Rails 5.2

A while ago I wrote a small post on how to manually mark an ActiveRecord model attribute as dirty. Since Rails 5.2 has some fundamental changes to ActiveRecord::Dirty.

Specifically, three methods have been deprecated and changed in 5.2. These are:

  • attribute_changed?
  • changes, and
  • previous_changes

Using these methods in a Rails 5.2 application will now result in the following message (or a variation of it) being outputted in your logs:

DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead.

attribute_changed? and changed? remain the same when called within a before_save callback. But when used in an after callback there are new methods you should use instead, which the Rails Core Team believe should reduce code ambiguity of changed? before saving to the database: will_save_change_to_attribute? and has_changes_to_save?


In summary, if upgrading to Rails 5.2 and after modifying an object and after saving to the database, or within after_save:

  • attribute_changed? should now be saved_change_to_attribute?
  • changed? should now be saved_changes?
  • changes should now be saved_changes
  • previous_changes has no replacement, since the behavior for it changes.

And (optional; less ambiguity, more readable, but longer) after modifying an object and before saving to the database, or within before_save:

  • attribute_changed? should now be will_save_change_to_attribute?
  • changed? should now be has_changes_to_save?
  • changes should now be changes_to_save

NotImplementedError Must Have Battered Wife Syndrome

I’ve been doing something very wrong my whole life.  I think it’s time to confess and seek help.

You see, like so many developers I see the NotImplementedError class in various languages and think, to my self “I haven’t had a chance to fully implement this class, so in my methods that my colleagues call that I didn’t finish yet, I will raise that exception to let them know.”

I am ashamed of myself.  And it’s time to get help.

You see the actual purpose of the NotImplementedError (as defined by the Ruby docs) is:

Raised when a feature is not implemented on the current platform. For example, methods depending on the fsync or fork system calls may raise this exception if the underlying operating system or Ruby runtime does not support them.

ERMERGERD!  I am a terrible person. Abusing this error class just because  my sub classes cannot meet the contractual obligation of the super class.

And I know I am not alone.

So whats the best practice, then?

Well, in Ruby’s case it turns out the answer is super simple; don’t raise an error at all! Instead, document the expectation and if a subclass fails to meet it a NameError, or often its subclass NoMethodError, will be raised automatically.

 

Converting TEXT columns to JSONB in PostgreSQL

Recently I had a text field being used to store JSON data from my web application. I hadn’t yet discovered the amazingness of the PostgreSQL JSONB datatype.

JSONB is an amazing datatype because Postgres understands how to query the structure and the keys and values can be indexed.

So if you have a text fields you want to convert to JSONB (provided the existing data is in properly formed JSON already) You can change the datatype and convert the existing data using this simple SQL command:

alter table tbl_name alter column col_name type jsonb using col_name::JSON;

You may be inclined to covert the text using the to_json function like: ‘USING to_json(col_name);’ – BUT DON’T DO THAT! What happens with that is you get a single string encapsulating the valid JSON, containing your JSON. Effectively a double-encoding bug.  The former (casting the column to JSON is the correct way).

Playing Day of the Tentacle…Again (with ScummVM)

UPDATE: There is literally no reason anyone should or would do this anymore, since Double Fine already HD remastered this classic! Go buy it – NOW – http://store.steampowered.com/app/388210/


One of my favourite teenage memories is that of playing Day of the Tentacle on my old 386 PC. I still remember how much the art and animation (quite excellent for it’s day) impacted on me; and even at today’s standards I feel warm affection whenever I see old screen-shots of the LucasArts classic point-and-click adventure game.  Others prefer others, such as Monkey Island or the Original Sam & Max, but for me, the pinnacle of adventure game goodness was the time traveling story of how the 3 protagonists (Bernard, Hoagie, and Laverne) stop (evil) Purple Tentacle from taking over the world and enslaving humanity.

I’ve been wanting to replay this game for over a decade, but as operating systems advanced, it seemed like short of a re-make my wishes were likely to remain a fantasy.  Until I discovered ScummVM.  From the ScummVM website:

ScummVM is a program which allows you to run certain classic graphical point-and-click adventure games, provided you already have their data files. The clever part about this: ScummVM just replaces the executables shipped with the games, allowing you to play them on systems for which they were never designed!

ScummVM supports many adventure games, including LucasArts SCUMM games (such as Monkey Island 1-3, Day of the Tentacle, Sam & Max, …), many of Sierra’s AGI and SCI games (such as King’s Quest 1-6, Space Quest 1-5, …), Discworld 1 and 2, Simon the Sorcerer 1 and 2, Beneath A Steel Sky, Lure of the Temptress, Broken Sword 1 and 2, Flight of the Amazon Queen, Gobliiins 1-3, The Legend of Kyrandia 1-3, many of Humongous Entertainment’s children’s SCUMM games (including Freddi Fish and Putt Putt games) and many more.

You can find a full list with details on which games are supported and how well on the compatibility page. ScummVM is continually improving, so check back often. Among the systems on which you can play those games are Windows, Linux, Mac OS X, Dreamcast, PocketPC, PalmOS, AmigaOS, BeOS, OS/2, PSP, PS2, SymbianOS and many more…

But hands down the BEST news about this is that the ScummVM has binaries available to run on virtually every OS you can imagine (except iOS obviously, or at least, not to my knowledge) and the original DOS versions of all these original adventure games seems to run on any platform.  I’ve only tested this with Day of the Tentacle, but sure enough an ISO of the original DOS game, was simply copied to a folder on my Mac, and the ‘DOTT’ directory in that folder selected as the game folder for ScummVM which detected the game and added it to a list.  Form there, I just selected Day of the Tentacle from the list and hit ‘play’ – which launched right into the game, with sound and everything.

I did the same thing on my kids Windows 7 PC’s and it worked well, although I found that I needed to turn the subtitles and voices on, because the speech audio sample-rate is very low and occasionally difficult to understand.  But then the game is 18 years old and under 300Mb. The menu can be accessed in-game by pressing the F5 key, and the game paused with the space bar.

Other than that, its still as good as the day it was released and hopefully ScummVM will get me through my itch for this classic long enough until a possible remake comes along.  Let’s hope the rumors of a Telltale remake are true!

 

Users Manual for an LG Split System Air-Conditioner (LST243H-2)

When we moved into our house 5 years ago, the previous owners had installed a LG Split System Air-Conditioner (LST243H-2) but had failed to leave the users manual behind. Through trial and error we eventually discovered the important features (a.k.a how to turn the damned thing on) but there was a whole bunch of modes for which we simply couldn’t fathom. The remote control had very little text, and more hieroglyphics than an Egyptian Tomb. It never occurred to me before to Google the model number until today.

Sure enough, I found a PDF version of the users manual for a LG LST243H-2. Woot!