I apologise to those who are uninterested for the rant that follows:
I can not stand nor hardly believe that the current government of my otherwise awesome country would choose to completely ignore the advice and expertise of industry professionals and the interests of the voters.
Today Steven Conroy, the Australian Minister of Communications, managed to get a piece of legislation passed introduced that will require all Australian ISPs to participate in the mandatory filtering of content delivered to Australian citizen. Welcome to the likes of Iran and China.
The legislation has been disguised as a measure to protect children from porn and the likes, despite repeated advice from industry professionals that it will do little to curb illegal content, cost the tax payers millions of dollars and ultimately make legal browsing a slower and inferior experience than it already is.
Furthermore, the government has made a effort to actively suppress the contents of the block list, keeping those affected (read: everyone) completely in the dark at to how it will affect their experience. The block list was leaked and shown to include content about which the government should have no say in how or why it is viewed such as some youtube videos, topics on Euthanasia, a gay dating site, flash and downloadable games, and sexual education material (contraceptives, etc).
To the Rudd government:
This is not good enough. I am a voter and a tax payer and I will not stand for this. I will actively participate in removing this ridiculous action and I will march the streets to show my support for an unrestricted flow of information like the rest of the world will continue to enjoy. I will show other tax payers how you have wasted $40 million (or more) of their money on a piece of technology no one wants and that will not work and remind them that this is done in a time of financial uncertainty instead of focusing on real issues like our carbon footprint as a nation. I have the knowledge and the will to undermine this pointless act, and I will use them to do so.
In the process of going through the redesign of http://mocra.com, I keep finding better ways to make the HTML more semantic.
Jack Chen and I have been turning the design into HTML over the last few days and we’ve been striving to be as semantic as possible by taking advantage of new HTML5 elements and by separating the content further from the design code with all the new CSS3 selectors.
Here’s an example of something we just did to make the HTML cleaner and more meaningful, whilst still maintaining the look and style.
Here’s what we are styling:
Usually your HTML (or HAML in this case) would look something like this:
The other day we got some HTML from the designer of one of our clients to implement as the new UI for an app we are building at Mocra. Overall the HTML was good but implementing it started to feel a little moist (i.e. not DRY).
For example, around all the apps form the client’s design had a rounded white box, the HTML of which went something like this:
It’s a lot of div tags to have to repeat around every form that has this style. Luckily a nice feature of Rails partials can help out. Create a shared partial file that will act as a layout like this:
Then in your views that need to have this white box, simply wrap your content in that layout like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%render:layout=>'shared/fieldset'do %><divclass="frow"><label>Currency:</label><selectclass="fs"><option>EUR</option></select></div><divclass="frow frlast"><label>File:</label><inputtype="file"name="" /></div><%end%><!-- Or, if the inner content is already in a partial --><%=render,:partial=>'form',:layout=>'shared/fieldset'%>
http://ftp2.au.postgresql.org/pub/postgresql/source/v8.4.1/postgresql-8.4.1.tar.bz2 | tar xjf -
cd postgresql-8.4.1
./configure
make && sudo make install
The files are now all installed in the right places, onwards to making them usable!
Create a postgres User and Group
This is adjusted from the instructions in the comments here).
That’s the clean way of making a user (so it doesn’t show up in your Accounts preference pane, etc.
Clean up
1
2
3
4
5
6
7
sudo touch /var/log/psql.log
sudo mkdir /usr/local/pgsql/data
sudo chown -R postgres:postgres /usr/local/pgsql/data /var/log/psql.log
# Put Postgres in your pathexport$PATH="/usr/local/pgsql/bin:$PATH"
sudo sh -c 'echo /usr/local/pgsql/bin > /etc/paths.d/pgsql'
Thanks to Apple we have a nice way of setting the PATH across the entire system so every app should now know how to find the Postgres binaries and the data directory is now set up to store the database files — now, we just need to create them.
# Become the postgres user
sudo su - postgres
# If this throws next commant throws an error about shared memory, # try putting "these lines":http://gist.github.com/224815 into # /etc/sysctl.conf, rebooting, and trying again:
initdb -D /usr/local/pgsql/data -E UTF8
# Start the database
postgres -D /usr/local/pgsql/data >/var/log/psql.log 2>&1&# Create a test database and check you can connect to it
createdb test
psql test# Just create a superuser for general dev tasks
createuser postgres
# we don't want to be the postgres user anymoreexit
Your done with the Postgres setup now!
PostGIS
1
2
3
4
curl http://postgis.refractions.net/download/postgis-1.3.7SVN.tar.gz | tar xzf -
cd postgis-1.3.7SVN
./configure --with-geosconfig=/Library/Frameworks/GEOS.framework/unix/bin/geos-config --with-projdir=/Library/Frameworks/PROJ.framework/unix
make && sudo make install
Phew! That one was easy.
Postgres Ruby Gem
This is the part that everyone cares about the most. If this works your Rails apps should start working like a charm.
Dr Nic’s latest post, “hash bang cucumber”, reminded me of a piece of hax I whipped up a few weeks ago with Ruby and Fish Shell.
Fish has an event system that allows you to register functions to be auto-run after or during certain events (such as when a particular environment variable is changed). One of this events is called fish_command_not_found. It is triggered whenever you type a non-existant command into the prompt.
With this in mind, you can trigger certain commands to run by matching what fish couldn’t manage to run automatically by catching this event.
For instance:
1
2
3
4
5
function __fish_method_missing --on-event fish_command_not_found
method_missing $argv
end
funcsave method_missing
Given that function is created (simply paste the whole code block into your Fish terminal), I can now create a command called method_missing (or whatever you call inside your __fish_method_missing) and place it somewhere in your $PATH — I like ~/.config/fish/bin.
My method_missing binary is simply something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env ruby
command =ARGV.shift
defrun(cmd)
puts "Running #{cmd.inspect} instead"
system(cmd)endcase command
when/^git(@|:\/\/).*\.git$/
run("git clone #{command.inspect}")when/^(?:ftp|https?):\/\/.+\.t(?:ar\.)?gz$/
run("curl #{command.inspect} | tar xzv")else$stderr.puts "No default action defined in #{__FILE__.inspect}"
abort
end
Each command you want to register just becomes a new when statement. For instance, to implement the functionality Dr Nic was trying to achieve, I simply modify the case block as such:
1
2
3
4
5
case command
when/^[a-z0-9_\-\/]+\.feature(:\d+)?$/
run("cucumber #{command}")# ...end
The existing entries I have in my method_missing command will auto-clone the repository of a pasted Git URL and download and expand a URL for a tar file, respectively. Not too shabby, and dead easy to implement
Apologies readers — I just realised that the code formatting is kinda screwed up in RSS feeds because I am using some HTML5 elements to represent my code blocks and using CSS to style them appropriately on the site. The RSS feeds obviously lack these stylesheets and so look pretty crap. I am not sure what the best solution is yet. Perhaps I embed a <style> in each RSS post or simply have the RSS feeds only show an excerpt of the full post.
For a project I am working on at the moment, I needed a Hash that returned a different default value, i.e. not nil. Specifically, I needed it to return another Hash, and for that internal Hash’s default value to be an Array, like so:
1
2
3
4
hash =# ...
p hash.class #=> Hash
p hash[:non_existant_key].class #=> Hash
p hash[:another][:non_existant_key].class #=> Array
Naïvely, I threw this into my code, and went along my merry way: entries =Hash.new(Hash.new([])). A few days later, I came back to this code to finish it and realised that something was awry with the values I was getting out of the Hash. It took me a while to figure it out because the values were all integers being summed together and I was never looking or setting the values directly.
However, eventually I saw a pattern — many of the values were the same. For instance, given the following assignments, I should expect p entries[153][:monday]#=> [12.hours]:
p entries[153][:monday]#=> [12.hours, 3.hours, 7.hours, 2.5.hours]
p entries[153][:tuesday]#=> [12.hours, 3.hours, 7.hours, 2.5.hours]
p entries[87][:monday]#=> [12.hours, 3.hours, 7.hours, 2.5.hours]
p entries[87][:tuesday]#=> [12.hours, 3.hours, 7.hours, 2.5.hours]# And in fact:
p entries[:any][:thing]#=> [12.hours, 3.hours, 7.hours, 2.5.hours]
Let that soak in for a second. Hash.new({}) uses the same instance of the internal Array as the default value for each of the inner-most keys. It also doesn’t set the key you want so p entries still printed {}. In retrospect, it is blatantly obvious that it does this, but the ramifications are still huge.
The way to get the intended effect is of course to use the block syntax of Hash.new which, while much uglier, definitely works as expected:
This article isn’t named ideally so if you have a better title, tell me.
This is the scenario: we have a simple HTTP resource on the internal network at Mocra. This resource is a closed source tool which is great except that it has one drawback — there are only two security modes: basic authentication on, or basic authentication off. There is only one username/password and you can’t control when or by whom it is required.
Now, my preferred behaviour here is to be able to give out multiple login/passwords and also to only require authentication outside of our local network. I set out to get this working.
I devised this solution a few months ago (which is why it’s using Apache at all) but I thought it might be of some use to someone. I created an Apache VirtualHost to act as a proxy between this resource and its clients. It looks like this:
<VirtualHost*:80># This virtual host proxies requests for the internal resourceServerName external.resource.com# Set auth header to base64 encoded 'username:password' # where username and password are the credentials for internal resourceRequestHeader set Authorization "Basic dXNlcm5hbWU6cGFzc3dvcmQ="ProxyPass /resource_path/ http://192.168.1.123:1337/resource_path/ProxyPassReverse /resource_path/ http://192.168.1.123:1337/resource_path/<Location/>Order allow,denyAllow from 192.168.1.0/24 # our local subnetAllow from 127.0.0.1 # duhAllow from 1.3.3.7 # our external router IPAuthType basicAuthName"Our Internal Resource"# where all the external login/passwords areAuthUserFile /var/www/.htpasswd Require valid-user# this is the secret sauce that allows EITHER# local access or Basic Auth accessSatisfy Any </Location>RewriteEngine onRewriteRule^/?$http://external.resource.com:80/resource_path/[R]</VirtualHost>
A few weeks ago, I was modifying my clone of Enki (my blogging platform) to use Ultraviolet instead of CodeRay, as it supports using both TextMate themes and syntaxes. This means I can extend it fairly limitlessly and use TextMate itself to modify the themes an syntaxes.
However, Ultraviolet doesn’t come with the best tools to turn the TextMate syntax and theme files into files Ultraviolet can use, and the ones that it does provide puts those translated files INTO the gem on your system. This means that you can’t easily deploy or share them.
So, to remedy this I made a small gem Ultraviolet Tools that provides two command line tools: uv-create-syntax and uv-create-theme.
They aren’t very complicated, nor very documented, but they are straight-forward to use:
Install
Install Ultraviolet Tools with sudo gem install bjeanes-ultraviolet-tools -s http://gems.github.com.
Usage
Themes
Run uv-create-theme path/to/theme.tmTheme
Syntaxes
Run uv-create_syntax path/to/syntax.plist or uv-create_syntax path/to/bundle/with/syntaxes.tmBundle
Using the resulting files
Use my fork of ultraviolet as it has an option to change the load path for themes and syntaxes so that you can load them out of your application files.
Just a quick post so show you how fields_for form helper can cause invalid HTML when used in combination with accepts_nested_attributes_for in your views.
If you are for any reason editing/creating your child objects in a tabular form, your view might look something like this:
<%form_for(@todo_list)do |f|%><%= f.label:name%><%= f.text_field:name%><h3>To Do Items:</h3><table><thead><tr><th>To Do Item</th><th>Due Date</th><th>Done?</th></tr></thead><tbody><% f.fields_for:itemsdo |item|%><tr><td><%= item.text_field:content%></td><td><%= item.text_field:due_date%></td><td><%= item.check_box:done%></td></tr><%end%></tbody></table><%= f.submit'Save'%><%end%>
At first glance this looks fine. However, when the ToDoList model has accepts_nested_attributes_for:items, the fields_for helper also outputs a hidden field for each existing Item instance with it’s ID.
This post is mostly a reminder for me in the future. However, others may find it useful also.
I love Postgres but more often than not I get frustrated and confused trying to get authentication working with a new database. By looking through the Deprec source code, I pulled out these two lines of code and so far they haven’t failed me:
1
2
su - postgres -c "createuser -P -D -A -E $DBUSER"
su - postgres -c "createdb -O $DBUSER$DBNAME"
Of course, replace $DBUSER and $DBNAME with your desired username and database name, or define them as shell variables. The $DBUSER creates that user as the owner of the database (so it has permission to read/write etc).
Anyone visiting my actual site will realise quite quickly that there is a new design. It was designed by my girlfriend and put together by me over the last week or two. It is still not done, but it’s close enough for me to put it live for now.
One thing of note is that it is entirely HTML5 and CSS3. This is done partly as an intellectual exercise and partly as a statement affirming my belief in a semantic web. I am still learning what elements are right to use where, so if you look at the HTML source and say to yourself: “Hey, that’s not how that element is supposed to be used,” please do tell me and I’ll restructure it.
I’ve tried to keep classes and IDs to a minimum, using the full power of CSS3 selectors instead. In order to get the design out, I’ve cut a few corners I am not happy with and used some classes, but I plan on refactoring these occurrences soon.
So far the design looks and works perfectly in Safari 4 and FireFox 3.5. Before implementing comments, it was working perfectly in Safari 3 and FireFox 3, but I haven’t checked those yet. I am quite content ignoring IE and even older versions of FireFox and Safari, as my target audience is developers—web developers, specifically—and I fully expect them to be using the cutting edge of browser technology.
I’ve hacked Enki apart quite a bit to get the code highlighting the way I want, replacing CodeRay with Ultraviolet, then further modifying it to add my own line numbers in a way that made the line numbers not get selected when selecting the code. Now, my sin: I used tables. I’m sorry—I based it off CodeRay’s existing table code for line numbers but find this an utter violation of the semantic goal of this entire re-design. This will be very shortly replaced with <figure> elements and figure pre{display: table-cell;} to emulate the same design.
Also, somewhere between finishing the code highlighting, and working on the rest of the site, I broke code highlighting in FireFox. Will fix soon.
Please send screenshots of the design broken in some browsers. I do want to have a stab at making it work in as many browsers as possible. But for now, go modern or go bust!
Today my Snow Leopard install was going so slow it felt like I was trying to swim through molasses. The system was literally so slow that typing text into some text fields (especially TextMate) lagged at about a rate of 1 character per second, and opening files (or even running touch on them) took a couple of seconds.
After a few fruitless restarts and a keen eye on my logs I started noticing these errors in Console.app:
1
2
3
4
5
6
7
8
9
10
11
9/07/09 3:01:37 PM com.apple.launchd[1] (com.apple.dynamic_pager[2445]) Exited with exit code: 1
9/07/09 3:01:37 PM com.apple.launchd[1] (com.apple.dynamic_pager) Throttling respawn: Will start in 10 seconds
9/07/09 3:01:47 PM com.apple.dynamic_pager[2481] dynamic_pager: cannot open swap directory /private/var/vm
9/07/09 3:01:47 PM com.apple.launchd[1] (com.apple.dynamic_pager[2481]) Exited with exit code: 1
9/07/09 3:01:47 PM com.apple.launchd[1] (com.apple.dynamic_pager) Throttling respawn: Will start in 10 seconds
9/07/09 3:01:57 PM com.apple.dynamic_pager[2546] dynamic_pager: cannot open swap directory /private/var/vm
9/07/09 3:01:57 PM com.apple.launchd[1] (com.apple.dynamic_pager[2546]) Exited with exit code: 1
9/07/09 3:01:57 PM com.apple.launchd[1] (com.apple.dynamic_pager) Throttling respawn: Will start in 10 seconds
9/07/09 3:02:07 PM com.apple.dynamic_pager[2547] dynamic_pager: cannot open swap directory /private/var/vm
9/07/09 3:02:07 PM com.apple.launchd[1] (com.apple.dynamic_pager[2547]) Exited with exit code: 1
9/07/09 3:02:07 PM com.apple.launchd[1] (com.apple.dynamic_pager) Throttling respawn: Will start in 10 seconds
I don’t know how or why they started but I was able to fix it by simply running sudo mkdir /private/var/vm in Terminal. Within a few seconds of creating the directory, the errors ceased and I could see a swapfile created inside the directory.
I have no idea what caused the directory to be deleted in the first place or why OS X isn’t smart enough to try creating it, but the fix is simple and instantly effective.
Note: I am not sure if this is a Snow Leopard specific issue or not, but it very well might be. Also, these started right after installing Adobe CS4, so I feel pretty confident blaming that for now.
I had an “Organization” creation form which allowed an administrator to create an Organization and an initial user for it. However, even though I had filled in all the required fields to pass the validations I was constantly getting the following error: Organization can not be blank.
Digging deeper I found that when an object is added to an association via the association.build method, the parent object isn’t actually set:
1
2
3
4
5
6
>> o =Organization.new(:name=>"Test")=>#<Organization id: nil, name: "Test">>> u = o.users.build(:name=>"Test User")=>#<User id: nil, organization_id: nil, name: "Test User">>> u.organization
=>nil
Apparently this flaw also applies to accepts_nested_attributes_for. When I send through the User params through from my form normally with the Organization params, it creates the User object in the users association as you’d expect, but raises a validation error because the User instance doesn’t recognise that it is has a parent Organization
My quick 5 minute fix was to modify the Organization model thusly:
Related Rails bugs seem to be here and here. This patch will also solve the problem and is currently in edge, but didn’t make it in time for Rails 2.3.