Git and Local / Server Hooks

I have been a proponent of pre-commit hooks for a very long time. After doing some searching, I found this lovely gem. It consists of a bunch of checks before you commit your code into the repository. A couple of my favorites:

  • php – Runs a PHP syntax check on all PHP files
  • merge_conflict – Makes sure you aren’t about to commit merge conflicts (you know, >>>>>>>> HEAD, stuff like that)
  • ruby_symbol_hashrockets – Prevents you from committing old-style Ruby hashes ( :hello => 'there' )

And much more. Highly recommended!

Turbolinks and WordPress

Rails Turbolinks is pretty cool, right? I thought it would be pretty cool to use it on WordPress, too.

There are probably a lot of bugs, and it probably doesn’t work well with JavaScript-heavy sites. I’ve included the jQuery compatibility layer, but it’s still not perfect. Use at your own risk.

To install, just search the WordPress plugin repository for “turbolinks”

wp-turbolinks

Or, available for download here:

http://wordpress.org/plugins/wp-turbolinks/

Contributors welcome!!!

Vagrant, WordPress, and Theme Development

I’ve been playing around with Vagrant recently. It really is a great tool for setting up development environments quickly and cleanly – no more local MySQL databases with 100 separate databases!

There are a few ways to solve this problem that many WordPress developers have:

  1. Use WordPress Multisite mode.
  2. Regularly clean your databases up and delete old ones.
  3. Use a common WordPress install, switching themes.
  4. Use Vagrant.

I’m going to be talking about the last option, Vagrant, in this blog post. I’ll list out a few reasons why Vagrant was attractive to me in the first place:

  • Isolation – this was appealing not only to reduce my database clutter, but also to be sure that the development and production environments were as similar as possible (within reason – of course).
  • Portability – another killer feature of Vagrant. Check out the repository, vagrant up, and you’re ready to go.
  • Coolness – don’t you love the idea of having contained, automatically managed environments for your projects? No? Well, I do.

Here’s what I came up with: vagrant-wp-theme-template.

It’s a template based on Underscores (_s), a template theme for WordPress themes. I’ve made two modifications: convert the CSS to SCSS, and the JavaScript to CoffeeScript. Grunt, an excellent automation tool, is used to compile the sources into CSS and JavaScript.

I’ve made the template compatible with _s, so just follow the instructions for _s regarding naming your theme, then the directions for getting your development environment set up. If you want to use an existing theme, just drop it inside the theme folder (and don’t forget to update the name in the Vagrantfile!)

I’m always open to new ideas and pull requests – please don’t hesitate to contribute!

Finally, this wouldn’t have been possible without the help of @miya0001‘s vagrant-chef-centos-wordpress, which this template is built off of. Thanks!

Marshal.dump and load with ActiveRecord

If you aren’t familiar with Marshal.dump and Marshal.load, you probably should be. It’s used to serialize Ruby objects into binary data – mostly caching.

Now, if you’re trying to implement a Russian-doll caching system with objects, you probably have run into the issue of eager-loading associations using #includes not being cached.

my_data = Rails.cache.fetch('an_object') do
  MyData.where(condition: 1).includes(:user).first
end

my_data.user
# On cache miss:
# => SELECT "my_datas".* FROM "my_datas" WHERE "condition" = 1 ORDER BY "id" ASC LIMIT 1
# => SELECT "users".* FROM "users" WHERE "id" = 1
# On cache hit:
# => SELECT "users".* FROM "users" WHERE "id" = 1

So, I wrote a quick module to make Marshal.dump and Marshal.load dump and load the association data, as well.

https://gist.github.com/keichan34/6448987

NB: This hack works with Rails 4, but I haven’t tried it on any other versions. YMMV.

Moved!

Just writing to let you know that I’ve switched servers for this blog! I’ve been meaning to get this on to a faster server that I have control over for awhile, and I finally got around to doing it.

For those of you who are curious, this site is being served by WordPress Multisite, on a Sakura 2G VPS.

And because everyone likes talking about stacks:

Stack.

Kidding.

That’s it! There is some moderate caching in most layers (WordPress Object Cache, APC, NGINX, etc), but nothing too drastic. It’s much faster now than it was on the previous server – we’ll see how this one holds up.

Thoughts during the Migration Process

MariaDB

I can’t say this enough – really, really, really smooth. Just stop mysql, remove, install MariaDB, then start it up again. All my data was migrated automatically.

NGINX + PHP + WordPress

There were a ton of guides for this kind of setup, but the two that most helped me were the WordPress Codex and rtCamp’s excellent WordPress + Nginx tutorials. Reading through both is highly recommended.

S3 Uploader

There have been more than a few times where someone needs to send a big file to me. So, I made a quick and dirty tool to allow anyone to upload files directly to a S3 bucket.

No more shuffling links around, worrying about them expiring, and wondering whether the data stored is safe or not.

Screen Shot 2013-08-06 at 5.17.27 PM

Fork away!

Source code

Ruby 2.0.0 p195 PSA

In Ruby 2.0.0 patchlevel 195, you can no longer mix old- and new-style hash syntaxes in method arguments.

For example:

Wrong

method hello: 'there', :goodbye => 'goodnight'

Right

method { hello: 'there', :goodbye => 'goodnight' }

or

method :hello => 'there', :goodbye => 'goodnight'

Apparently this has been fixed in another patch to Ruby 2.0.0, but that doesn’t really matter, since p195 is out right now.

Source: https://gist.github.com/stephencelis/5595842

My Experiences with Rubinius

Rubinius is an implementation of the Ruby language spec. I’ve been using it recently for a project, and I’ve been liking it so far. Here’s a few thoughts I’ve been having while using it.

Philosophy

The Core

Rubinius, in its core, is written in C++ and uses LLVM (Low Level Virtual Machine). Without getting too technical, it translates the Ruby code that you write into efficient machine code, then executes the machine code directly on the CPU. This architecture is very similar to Google’s V8 (and one of the reasons that Google Chrome is a fast browser).

The Concept

Now for the concept of “Ruby”. “Ruby” is a programming language specification, not a program or compiler. The standard reference implementation is called MRI (Matz’s Ruby Interpreter, not magnetic resonance imaging). MRI is used in many production environments, and especially with the latest 2.0.0 release, introduces many performance improvements, and is very stable (all of our new Rails apps are on 2.0.0).

MRI is written mostly in C.

Rubinius’ tagline “Rubinius: Use Ruby™” summarizes the intent of Rubinius. Use Ruby! Because the Rubinius core is as fast as it is, and Ruby code is basically machine code (from the computer’s standpoint), standard libraries – the basic functions of the language – can be written in Ruby. Use Ruby!!

Speed

In development, Rubinius seems to be a little slower than MRI – especially in the first load. Rails is a big library.

In production, especially with a threaded app server (Rubinius has no “Global Interpreter Lock” and supports real threads), however, Rubinius is extremely fast. I’ve been using Puma.

You do need to write thread-safe code, but the payoffs are enormous.

Conclusion

This little project I’m working on will probably not see a “real production” environment anytime soon, so I really wanted to try out some alternative Rubies (there are a few). All in all, my experience with Rubinius has been very good. Can’t wait for the production release of Rubinius!

The AppleLanguages switch

The Google Chrome(tm) title bar in English, Japanese, Russian, and Chinese

In the past few weeks, I’ve found myself demonstrating how to do things on my computer for other people. You know, screenshots, screencasts, the like.

I ran into a small problem: Most of the people I need to teach don’t understand English.

And my default user interface is in English.

So, I ran into this little trick:

$ [path to app]/Contents/MacOS/[app name] -AppleLanguages '([language code])'

That little bit of Terminal code will launch that app in the specified language code you entered, if supported by that application. For those of you who don’t have 10 terminal windows open at any given time, don’t worry! I’ve made something for you too. 🙂

langchooser.app

Drop an app onto “langchooser”, and it will show you all the languages that app supports. Choose the language, then click “OK”.

Now, I can continue with my screenshots and screencasts without having to switch user accounts! やったー!