Fixing corrupt position in acts_as_list

When there is no uniqueness in mysql, things can go wrong….so we fix em…

module ActsAsList
  # positions can get mixed up when users click like crazy -> reorder them if necessary
  # ActsAsList.reorder_positions!(current_user.categories)
  def self.reorder_positions!(objects)
    objects.each_with_index do |object, index|
      new_position = index + 1
      next if object.position == new_position
      object.update_attributes(:position => new_position)
    end
  end
end

sh without rake

Just built this little sh script, that can be used when rake is not installed (we use it to setup a new/clean system)

def sh(cmd)
  puts cmd
  IO.popen(cmd) do |pipe|
    while str = pipe.gets
      puts str
    end
  end
  $?.success?
end

Hash in response + purge.hash for Varnish 2.1

Varnish 2.1 made req.hash unreadable / does not store it. So purge.hash will no longer work.

Getting hash output back

sub vcl_hash{
  set req.http.X-Hash = req.http.host;
  ... more stuff ...
  set req.http.X-Hash = req.http.X-Hash "#"; # add trailing '#' to be compatible to varnish 2.0
  set req.hash += req.http.X-Hash; # store in unreadable req.hash
  return(hash);
}

sub vcl_deliver {
  ... more stuff ...
  # store hash in response for matching / testing that hashing works
  set resp.http.X-Hash = req.http.X-Hash;
  return (deliver);
}

Purging on hash

varnishadm -T 127.0.0.1:6082 'purge req.http.X-Hash ~ something'