Posts

A collection of 67 articles

Auto Toggle MacBook Internal Keyboard

I prefer using external keyboard with my MacBook. When no external monitors are used, a typical setup is placing the keyboard above the internal one, so I can still use the internal touchpad. But sometimes the external keyboard may press some keys of the internal keyboard. There is a solution to disable the internal keyboard, but it is tedious to run the command manually. # Disable, ignore the warning sudo kextunload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/ # Enable sudo kextload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/ Fortunately, Keyboard Maestro supports executing scripts when a USB device is attached or detached.

Updated  •  1 min read

ActiveRecord includes and preload

ActiveRecord has two query methods to eager load associations, includes and preload. Although the documentation of preload says Allows preloading of args, in the same way that includes does. Indeed the two methods have some differences. In simple words, prefer includes to eager load associations. Use preload only when you want to customize select columns, or you meet error “Can not eagerly load the polymorphic association”.

Updated  •  2 min read

How Rails Assets Prefix Disables the Session

This is original posted on intridea blog. I recently worked in a Rails project with Peter (@sporkd). The project is intended to be used as a sub-site, and should be served under sub-URI. After google, we ended up by setting config.assets.prefix and wrapped all routes in scope. The solution is simple and worked well. But soon, some weird bugs were found, and Peter was successfully isolated the problem to session (see demo sporkd/asset_prefix_test) After several hours debugging, I finally found out the cause. To make a long story short, the routes configured in routes.rb should not start with config.assets.prefix, otherwise session save is skipped. The demo sporkd/asset_prefix_test can be fixed by simply setting config.assets.prefix to /one/assets. You also get a bonus by setting a unique prefix for assets, since it is easy to add expiration header for assets in web server.

Updated  •  3 min read

A trick to use just jasmine gem to test Javascript in Rails

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. It is very easy to integrate Jasmine into Rails, since the team provides the jasmine gem. The jasmine gem also supports assets pipeline, just prepend assets/ to file path. For example, app/assets/javascripts/application.js.coffee can be referred as assets/application.js in jasmine.yml src_files. However, spec_files does not support assets pipeline, so the files in spec/javascripts cannot be written in CoffeeScript. But if spec/javascripts is appended to assets pipeline paths, the spec files can be added to src_files in jasmine.yml. This post explains how to apply such trick, and how to integrate jasmine gem with guard-jasmine and travis. I also created a demo repository, see its commits for integration steps.

Updated  •  3 min read

Rails Compound Input

When I implement time input feature for 19wu (an open source ticket sale system), I want to split the datetime into date and time parts, so JavaScript date picker and time picker can be used. This post introduces two methods I found. Compound datetime input composed_of utilizes assign_multiparameter_attributes trick like datetime_select, and fields_for mocks an association. The github repository doitian/rails-compound-input-demo contains demos for both methods.

Updated  •  3 min read

Use Popup isearch For Yasnippet Prompt

Yasnippet tries functions in yas/prompt-functions when it needs user to select one choice, such as selecting snippets with the same trigger key, such as helper method yas/choose-value. popup is a visual popup interface library extracted from auto-complete by its author. It has better look and feel than all the built-in yas/prompt-functions. Also it is easy to customize, and its isearch mode is very efficient, the items are filtered on-the-fly when typing.

Updated  •  1 min read