Author: francis

Take what the defence gives you – coding

I’ve recently listened to Steven Pressfield’s Turning Pro about 10 times driving up and down the motorways. I can’t recommend it highly enough. One of the things he talks about are the things that get in the way of getting things done, of pursing your art and doing what you have to do when you turn pro.

So, some days you just can’t get your mojo on. The pro doesn’t let this stop the work happening. So you take whatever you can get today and work with it. In sporting terms you’re having a day where you can’t make progress against your opponent. So you take what the defence gives you and push on. 

So when you look at what you need to get written today and don’t know how to start do the little things you can do and let the ideas percolate through your head. This is similar to the technique where writers just start writing anything that comes into their heads. When you do this it gets you moving and when you are moving you are not stuck any more.

So take what the defence gives you, take the steps you can take. You can’t do the big stuff all the time. But keep moving, that’s what works.

Android and mobile, purity, who cares?

Been doing Android dev full time for a few weeks now. I have to say I’m enjoying it. I also put myself through iOS training and have been doing some of that. Really enjoying that too.

I think I’m fed up with web apps, and all the arsing about you have to do to get things going and stoping useless bastards breaking your site. I can just fire up an IDE, and I’m there with mobile.

Beginning to think that the web will become the cloud (ha ha) sooner or later and most things will generally run as native mobile apps talking to servers (that used to be web servers). This will also take web apps away from the current Jesus-it’s-JavaScript-with-bouncy-shit car crash, the (web) interfaces will become simple again, because most of the time you’ll only go onto a site where a phone screen is too small to do something complicated.

I have to confess that I’m not missing Scottish Ruby either. I’d like to be there, but I’m enjoying writing apps in Objective-C and Java. I like the feel of this. Rails is becoming an over-complicated PITA to install and run. I mean, WTF, we replace JavaScript with something we can’t even debug? Please!

I’ve also discovered that my hatred of Java is really a hatred of J2EE and all that shoot yourself in the head dependency injection crap. For putting GUI components together in a coherent way it’s fine. But this makes sense, as it was originally designed to run set top boxes, and build desk top GUIs with Swing. It was only because it was the new kid in town and the web was new at the same time that it ever got popular, i.e. it was the only place to go for corporates who didn’t want to go the Microsoft route. But it was designed by a committee of computer scientists who’d didn’t understand what real people need, so it was always going to suck as much as the MS stuff. Sigh.

I’m not missing Ruby. I’m not missing BDD. I’m writing simple apps with a much faster turn around. Fuck me. I’m having FUN. Who’d a thunk it? Ruby was fun, it was radical. Rails made the web much easier and quicker. But now? I’m just bored by it, dunno why. I like to get things in the hands of folk who need them. Don’t care about the pedantic stuff. In fact the computer science types have taken over again. Sigh.

If you want to create a ListView adapter that searches on the string arbitrarily I’ve created a gist here, by the way. Enjoy.

error: Permission denied sending UDP broadcast packets

Sorry, gentle reader, this one’s a bit technical.

I was reusing some of the code Apple supply for sending UDP packets between servers, but the system I’m working with listens for broadcast packets. I set it up to use the address 255.255.255.255, but got the EACCESS error: Permission denied

I found an example that did work (without using Apple’s CFSocket class) and did a line by line what’s different. You need this to do broadcast

int broadcastEnable=1;

int ret=setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));

if (ret) {
    NSLog(@”Error: Could not open set socket to broadcast mode”);
    close(sd);
    return;
}
If you edit this into Apple’s UDP echo project then it will allow broadcast. Give thanks and praise at http://splinter.com.au/sending-a-udp-broadcast-packet-in-c-objective

Laments to the 10 years of Agile

Laments to the 10 years of Agile

PGError: ERROR: date out of range for timestamp

Note – this assumes you’ve enabled the pgbackup plugin in Heroku – as it does the full database backup for you for free – I suggest you do it right now!

Oh, the fun I just had with this one. One of my clients’ systems started throwing this message over the last week or so.

I’m lazy so I was using sqlite for my development database and Heroku’s Postgres for live.

So, thinks I, time to switch to using Postgres in my development environment, because it must be the database, mustn’t it?

I’d installed it on my Macbook ages ago but hadn’t actually used it with Rails. So I found a couple of blog posts and changed things around. I had already told Postgres to use passwords for validation.

It took me ages to realise I needed to set a password for the postgres Unix user – this was the password I needed to connect to the database. Then I followed these instructions and got my dev database set up.

So – pull stuff down from heroku and then import it to my dev environment.

Right – everything works perfectly – right!

So where is the error? 

I googled for it and found a forum posting suggesting that you could have a date stamp that is outside the range of time stamps and the coercion of date stamps is going boom. Did a lot of messing around in the heroku console to no avail, trying to issue commands like analyze table and count things.

Also, the column is a timestamp anyway, so you can’t store nonsense in it in the first place.

So, I put the app into maintenance mode, took another backup, restored that backup over the existing database, and all was well.

This might be a better solution than trying to rescue the database in any event, because all of the statistics will have been reset too.

I was a little bit hampered by heroku not letting you connect to a shared database because I was trying to issue commands using Active Record – but hey – it’s free. I do wish that heroku console was slightly more informative than server error

Rails for Designers

This is a short post to help designers who aren’t familiar with Rails understand their way around it enough to change things and work with developers. It assumes that you know CSS and HTML and aren’t scared of HTML that has embedded code in it.

The structure of a Rails app

Rails is a Model/View/Controller system. 

  • Models are business logic, and storing things in databases
  • Views are displaying the contents of the models
  • Controllers co-ordinate the Model and the View

Here is a picture of the directory structure of a Rails app:

The files you care about live under app and public:

And views also breaks down like this:

This application has an extra top-level directory, artwork, that isn’t standard. I used it to keep the artwork and design files under version control.

What the directories do:

  • Public. This is where the root of the application is for static content like CSS and JavaScript files.
  • App/Views. This is where the files are stored for rendering dynamic content from the application.
  • App/Views/Layouts. This is where the templates that surround the dynamic content are stored.

The example we show here is from before Rails introduced the performance enhancement of the asset pipeline, it complicates things a little and I will discuss it later. What I want do do first is explain the principles.

What things do

A layout file looks something like this:

<!DOCTYPE html>

<html>

<head>

  <title>Favorbite</title>

  <%= stylesheet_link_tag :all %>

  <%= javascript_include_tag :defaults %>

  <%= csrf_meta_tag %>

</head>

<body>

  <div id=”header”>

    <div class=”span8 offset8”>

      <h1>

        <a href=”/”><img src=”/images/favorbite-logo.png” alt=”Favorbite logo”></a>

      </h1>

   <%= yield :header %>

    </div>

  </div>

    <%= yield %>

</body>

</html>

This is a very simple wrapper. It’s HTML 5, of course. The <%= %> tags are used to embed code. The :all in stylesheet link tag lets us include all of the files in the public/stylesheets directory without having to specify them individually, or you can include them one by one if you need to. This changes with the asset pipleline (but let’s not worry about that now).

You can also just put in plain old HTML code to include whatever CSS you like, the helpers are a convenience and also help with cacheing and other wondrous magic web things. 

You can see two lines that I have made bold. Each contains the word yield. This is a Ruby construct that you can find out more about if you’re interested. The one that doesn’t have any arguments will render the appropriate dynamic content file. The one with :header will render any content that is in a block that has that name. For example

Hello, this is ordinary content

<% content_for :header do %>
   This will appear where the yield :header tag tag is, or not appear at all if there isn’t one.
This is how you define a block, using ‘do’
<% end %>
If we carry on here it will just add into the main text.

<%= “the string here will show in the text, without the = sign nothing will happen, the results of any Ruby code need %= to show” %>

Working out what gets displayed

Rails apps have routes go to controllers, and from there to the view depending on what the controller decides to do to render the request. The surrounding layout is set by the controller. For example http://myapp.com/chickens/1/edit is asking the router to find a controller called chickens, and call the method edit in it. This will then find the chicken with ID 1 and render it to be edited if all goes well.

The dynamic code for the edit will be in app/views/chickens/edit.html.erb (ERB being a way of embedding Ruby code), inside that file you may find code that looks like this:

<%= render :partial => “form” %>

Partials a little chunks of reusable code, because no directory was given it will be assumed to be in the same directory as the other chickens code, here app/views/chickens/_form.html.erb

The convention for a partial is it has a leading underscore in its name. If the controller doesn’t explicitly say what dynamic content to render the file that matches the route will be used:

  • …/chickens – This will go to index method and will look for a file called index
  • …/chickens/1 – This will try and show the chicken with ID 1 – method/view called show
  • …/chickens/new This will render a form where you can create a new chicken
  • …/chickens/1/edit Guess!

When you save a new chicken you land in the method create, when you update an existing chicken you land in the method update. If these methods succeed the convention is that it puts a flash message saying it succeeded and takes you to the show method. Sometimes you will need to look in the controller code to unpick this.

For example if you see:

render :action => “edit”

It will use the edit file to render, rather than whatever the default one would be for the method you are in.

Picking the layout

If the controller doesn’t explicitly set a layout it will use the one defined in app/views/layouts/application.html.erb.

Configuration by convention

As you can see Rails uses convention to say where things are and you don’t have trawl through configuration files (Java) or wade through heaps of confusing code where it’s hard to work out where the application ends and the data begins (PHP guys, looking at you here). 

The asset pipleline

This was introduced in Rails 3.2. There is an extra directory under app called assets. Rather than repeat the documentation I suggest you go here to find out how it works.

I hope this helps. As always if I have erred or not explained things to your satisfaction, please contact me and I will do my best to explain it better or correct any mistakes.

Thanks for reading.

Dead Time at East Didsbury

Sometimes time is dead time; you have to wait and hope that it will pass

Dangerous thought – how good it would be to edit your life away, but how would that be good?

Once lost it doesn’t come back, whatever it might be

So this moment is of waiting and not exactly enough, or not moving forward to where you would like to be:

So what?

Is it really that different from all the other moments?

This body waits for the next thing – driven by this mind

This mind is what cares about forward and back

This mind judges and measures

This mind does not see its own nature

So what is waiting?

What is impatient?

What is hurting?

What is sad?

Does it have a colour, shape or presence anywhere?

Can you touch it? Except maybe indirectly?

Why does it want so much?

Why does it need anything?

The thing it needs is not there.

It is closed and also open

The warm train arrives – the moment dies