<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>ou.jiayong.name</title>
<link href="http://ou.jiayong.name/atom.xml" rel="self" />
<link href="http://ou.jiayong.name/" />
<updated>2010-07-13T15:15:19+02:00</updated>
<id>http://ou.jiayong.name/</id>
<author>
<name>Jiayong Ou</name>
<uri>http://ou.jiayong.name/pages/contact.html</uri>
</author>
<entry>
<title>Preparing a Mac to nicely hand over to another person</title>
<link href="http://ou.jiayong.name/blog/preparing-a-mac-for-handover.html" />
<updated>2010-05-16T14:22:46+02:00</updated>
<published>2009-03-04T00:00:00+01:00</published>
<id>http://ou.jiayong.name/blog/preparing-a-mac-for-handover</id>
<content type="html">&lt;p&gt;So I’m parting with my good old first generation Macbook Pro and being nice as I am, I’d like to hand over the machine to the new owner fully updated and with iLife and iWork installed. So for that, I’ll have to install Leopard, install updates (tons of ‘em), install $whatever, install more updates, delete the user you used to install all that stuff and finally restore the Setup Assistant.&lt;/p&gt;

&lt;p&gt;This post is about the last two points (especially the ‘delete user’ part) since the points before that are pretty boring.&lt;/p&gt;

&lt;p&gt;While Mac OS X &lt;em&gt;does&lt;/em&gt; have &lt;code&gt;/etc/passwd&lt;/code&gt; and &lt;code&gt;/etc/groups&lt;/code&gt;, those files only get used when booted in single user mode. In normal operation, it pulls users, groups and group memberships from &lt;a href="http://en.wikipedia.org/wiki/Apple_Open_Directory"&gt;Open Directory&lt;/a&gt; to make things more complicated.&lt;/p&gt;

&lt;p&gt;While the &lt;a href="http://theappleblog.com/2008/06/22/reset-os-x-password-without-an-os-x-cd/"&gt;restoring Setup Assistant&lt;/a&gt; part is easy and well-known, deleting a user in single user mode is more tricky. I did found hints to &lt;a href="http://www.oreillynet.com/mac/blog/2006/04/deleting_mac_os_x_users_remote.html"&gt;delete users from the shell&lt;/a&gt;, but I need to get the directory service running first when I’m in single user mode. And I need to be in single user mode since I’m going to remove the only account on the machine.&lt;/p&gt;

&lt;p&gt;Well then, let’s get started by booting into single user mode and do the usual thing and remount the hard disk writeable. For this guide, we’ll assume we want to get rid of the user &lt;code&gt;test&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# mount -uw
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Trying to run dscl at this point makes it complain about Directory Services not running&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# dscl . list /users;
For Single User Mode you must run the following command to enable use of dscl.;
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sounds helpful, doesn’t it? So we start the local directory service and try again. But when we use &lt;code&gt;dscl&lt;/code&gt; again, the same (at this point not very) helpful message greets us again:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist;
# dscl . list /users;
For Single User Mode you must run the following command to enable use of dscl.;
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While it tells you that you need to load &lt;code&gt;/System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist&lt;/code&gt;, what it doesn’t tell you is that you also need &lt;code&gt;/System/Library/LaunchDaemons/com.apple.DirectoryServices.plist&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist;
# launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So finally we have access to the directory and do the hacking on it. Yay!&lt;/p&gt;

&lt;p&gt;For now, we assume we want to get rid of the user &lt;code&gt;test&lt;/code&gt;. First we remove all his group memberships with this little bash one liner.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# for group in `groups test`; do dscl . delete /groups/$group GroupMembership test; done;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we get rid of the user and its home directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# dscl . delete /users/test;
# rm -rf /Users/test
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, we make Mac OS X thinking that the Setup Assistant hasn’t been run yet and shut down the box:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# rm /var/db/.AppleSetupDone;
# shutdown -h now
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So now the Mac is in a more or less factory settings, plus the stuff you installed earlier.&lt;/p&gt;
</content>
</entry>
<entry>
<title>ActiveRecord, named scopes, select statements and you</title>
<link href="http://ou.jiayong.name/blog/activerecord-named-scopes-select.html" />
<updated>2010-05-16T14:22:46+02:00</updated>
<published>2009-03-11T00:00:00+01:00</published>
<id>http://ou.jiayong.name/blog/activerecord-named-scopes-select</id>
<content type="html">&lt;p&gt;ActiveRecord’s &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#M002120"&gt;named_scope&lt;/a&gt;s are awesome, but I recently run into something less awesome about it: It doesn’t merge the &lt;code&gt;:select&lt;/code&gt; option of nested scopes.&lt;/p&gt;

&lt;p&gt;So let’s look something like that:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Profile&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Business&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Profile&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Profile&lt;/span&gt;
  &lt;span class="c1"&gt;# ... some code ...&lt;/span&gt;

  &lt;span class="n"&gt;named_scope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:by_popularity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="ss"&gt;:select&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;profile_profiles.*, count(profile_fans.member_id) AS fan_count&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;# ... some more stuff for the scope&lt;/span&gt;
    &lt;span class="ss"&gt;:order&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;fan_count DESC&amp;quot;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# ... some more code ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;And we use the named scope like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Profile&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Business&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;by_popularity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:all&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:select&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;profile_profile.*&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Should work, shouldn’t it? But as you might have guessed, it’s where ActiveRecord is exploding by throwing an &lt;code&gt;ActiveRecord::StatementInvalid&lt;/code&gt; exception:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Mysql::Error: Unknown column 'fan_count' in 'order clause':
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you look at the rails log, you should see something like that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Profile::Business Load (0.017810) SELECT profile_profiles.* FROM `profile_profiles` INNER JOIN [... some more SQL ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Yes, my first though was ‘Dude, where’s my SELECT?’. Turns out ActiveRecord doesn’t merge them it just use the last &lt;code&gt;:select&lt;/code&gt; it encounters. In my case the workaround was easy by moving the &lt;code&gt;COUNT()&lt;/code&gt; from &lt;code&gt;:select&lt;/code&gt; to &lt;code&gt;:order&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Profile&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Business&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Profile&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Profile&lt;/span&gt;
  &lt;span class="c1"&gt;# ... some code ...&lt;/span&gt;

  &lt;span class="n"&gt;named_scope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:by_popularity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="ss"&gt;:select&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;profile_profiles.*&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;# ... some more stuff for the scope&lt;/span&gt;
    &lt;span class="ss"&gt;:order&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;count(profile_fans.member_id) DESC&amp;quot;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# ... some more code ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Crisis averted in this case, but it still leaves an uneasy feeling. And of course, the documentation says nothing about that.&lt;/p&gt;

&lt;p&gt;Oh well… Someone &lt;a href="http://rails.lighthouseapp.com/projects/8994/tickets/1295-making-with_scope-merge-selects"&gt;proposed a patch&lt;/a&gt; for this problem and let’s hope it makes it into a Rails release.&lt;/p&gt;
</content>
</entry>
<entry>
<title>Instant object iterator in PHP</title>
<link href="http://ou.jiayong.name/blog/instant-object-iterator-in-php.html" />
<updated>2010-05-16T14:22:46+02:00</updated>
<published>2009-08-24T00:00:00+02:00</published>
<id>http://ou.jiayong.name/blog/instant-object-iterator-in-php</id>
<content type="html">&lt;p&gt;If you want to have a class iterate over an array member:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IteratorAggregate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$entry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$entry&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getIterator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ArrayIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$foo&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;bar&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$foo&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;baz&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$foo&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$val&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Outputs&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bar
baz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's the most common use case for the &lt;code&gt;Traversible&lt;/code&gt; interface in PHP I'm encountering and it's much easier than implement the whole &lt;code&gt;Iterator&lt;/code&gt; interface.&lt;/p&gt;
</content>
</entry>
<entry>
<title>Hi Jekyll!</title>
<link href="http://ou.jiayong.name/blog/hi-jekyll.html" />
<updated>2010-05-24T21:58:06+02:00</updated>
<published>2010-05-24T00:00:00+02:00</published>
<id>http://ou.jiayong.name/blog/hi-jekyll</id>
<content type="html">&lt;p&gt;Finally arrived on the &lt;a href="http://github.com/mojombo/jekyll"&gt;jekyll&lt;/a&gt; bandwagon and got around re-launching &lt;a href="http://ou.jiayong.name"&gt;http://ou.jiayong.name&lt;/a&gt; with it. I've got to say it's been very nice to work with. In case you don't know jekyll: It's a Ruby gem that generates static pages from a bunch of files containing its layouts, contents and assets. Everything you see here are static pages.&lt;/p&gt;

&lt;p&gt;This blog is intended to supplement my &lt;a href="http://orly.ch/"&gt;tumblr blog&lt;/a&gt; with a low noise, original content one. It focuses on software development, although I might put an off-topic post here every now and then. For the beginning, I've dragged some of my older posts with useful infos in it over.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://github.com/jou/jekyll"&gt;My own fork of jekyll&lt;/a&gt; is based off &lt;a href="http://henrik.nyh.se/"&gt;Henrik Nyh&lt;/a&gt;'s &lt;a href="http://github.com/henrik/jekyll"&gt;fork with Haml support&lt;/a&gt;. The source files of this blog is also &lt;a href="http://github.com/jou/ou.jiayong.name"&gt;on github&lt;/a&gt;. The contents here are under a &lt;a href="http://creativecommons.org/licenses/by-nc/2.5/ch/"&gt;Creative Commons by-nc&lt;/a&gt; license.&lt;/p&gt;
</content>
</entry>
<entry>
<title>JavaScript Blacklist for Chrome</title>
<link href="http://ou.jiayong.name/blog/js-blacklist-chrome.html" />
<updated>2010-07-10T19:47:14+02:00</updated>
<published>2010-07-10T00:00:00+02:00</published>
<id>http://ou.jiayong.name/blog/js-blacklist-chrome</id>
<content type="html">&lt;p&gt;One of the things I missed since I've switched back from Safari 5 to Google Chrome is &lt;a href="http://homepage.mac.com/drewthaler/"&gt;Drew Thaler&lt;/a&gt;'s &lt;a href="http://homepage.mac.com/drewthaler/jsblacklist/"&gt;JavaScript Blacklist&lt;/a&gt; extension. It blocks scripts from a configurable list of domains and is useful to get rid of some of the most annoying things on the internet like &lt;a href="http://en.wikipedia.org/wiki/IntelliTXT"&gt;IntelliTXT&lt;/a&gt; (those doubly underlined text link ads).&lt;/p&gt;

&lt;p&gt;Since I haven't found an equivalent for Chrome, I wrote it myself, with some code taken from the Safari extension.&lt;/p&gt;

&lt;p&gt;Due to limitations in Chrome's extension API, it needs to wait for a &lt;a href="http://code.google.com/chrome/extensions/messaging.html"&gt;message&lt;/a&gt; from the &lt;a href="http://code.google.com/chrome/extensions/overview.html#arch"&gt;background page&lt;/a&gt; with the blacklisted domains before it can start filtering &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;-tags. It might be possible that some script files starts to load before the extension kicks in and one of the scripts that gets loaded is on the blacklist. That can't be avoided until there's a way to do message passing synchronously. That issue aside, I'd say the API is rather pleasant to work with. At least the parts that I needed for this extension.&lt;/p&gt;

&lt;h2&gt;Install &amp;amp; Source Code&lt;/h2&gt;

&lt;p&gt;You can install it from &lt;a href="https://chrome.google.com/extensions/detail/emcepjkdiiaenmoaghcfghjjppbkbhnf"&gt;Chrome extensions gallery&lt;/a&gt;. If you're interested in the source code, it's &lt;a href="http://github.com/jou/chrome-jsblacklist"&gt;on github&lt;/a&gt;. Patches are welcome.&lt;/p&gt;

&lt;p&gt;I'm running &lt;code&gt;dev&lt;/code&gt;-channel on Mac, but I tested it with the current stable version and it seems to work. I haven't tested it on Linux or Windows, but it should work.&lt;/p&gt;

&lt;h2&gt;Feedback&lt;/h2&gt;

&lt;p&gt;&lt;a href="/pages/contact.html"&gt;Drop me a line&lt;/a&gt; or open a bug on &lt;a href="http://github.com/jou/chrome-jsblacklist/issues"&gt;github's issue tracker&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Todo&lt;/h2&gt;

&lt;p&gt;I have no idea of graphics design and the current logo sucks bad. If someone would like to make a decent logo, I'd gladly accept the help.&lt;/p&gt;

&lt;p&gt;Also, the settings page could be made a bit prettier.&lt;/p&gt;
</content>
</entry>
</feed>
