Category Archives: Development

I recently came across a small problem in RSH whereby on loading a page with a state in the url (e.g. localhost.com/test_page#test) would not trigger the history listener function on it’s initial load. To fix this, I needed to modify my window onload function to include a call to my history listener, as follows:


history_listener: function (newLocation, historyData) {
...
},

// Window.onload calls this
initialize_history: function () {
       	dhtmlHistory.initialize();
       	dhtmlHistory.addListener(this.history_listener.bindAsEventListener(this));

       	// Added this line to force a call to my history listener
       	this.history_listener(dhtmlHistory.getCurrentLocation());
}

This ensures that when the page is freshly loaded, the history listener is called with the contents of our initial location.

OK, so say you have a form as follows:


<% form_tag '/', :method => :get do -%>
<p><%= text_field_tag 'q', params[:q] ||= '' %> <%= submit_tag 'Search' %></p>
<% end %>

That form will send the contents of the text box to the page at ‘/’. Unfortunately, it will also send through the value of the submit button, which is ‘Search’ in this case. The url generated will be:

/?q=something&commit=Search

In this case, we would want only the ‘q’ parameter passing through, and in order to do this, all we need do is nullify the name parameter:


<% form_tag '/', :method => :get do -%>
<p><%= text_field_tag 'q', params[:q] ||= '' %> <%= submit_tag 'Search', :name => nil %></p>
<% end %>

This will create the following url on submission:

/?q=something

Just a quick tip I recently came across:

If you want to see the docs for any of the gems installed on your dev machine, type the following in the command line…


gem_server

…and then browse over to http://localhost:8808/!

Given that the back button has been around for many years, and users have grown accustomed to it’s use, it’s a shame to see so many web applications neglecting this feature. As an example, I’ll pick on Lightbox, a tool that I like, yet one that is flawed in it’s disregard for the back button. For example:

  1. You navigate to a page that contains a thumbnailed image
  2. You click the image, and Lightbox shows it centered in your browser, full size
  3. You click back, press the back button on your mouse or whatever

Now, I realise that Lightbox has a close button in the image dialog box, but I’m not looking at the box, I’m looking at the image, and furthermore, my natural path back to the originating page is to press the back button, not to close the Lightbox. I therefore end up at the page I was on before I saw the thumbnail.

I’ve seen this with many users - the back button has been scarred into our mind, and to break it now is frivolous, and will ultimately lead to frustration, and lost users.

It’s with relief therefore, that I found Really Simple History, a JavaScript library for dealing with this exact problem - to create a JS history system that can run across many browsers, and provide developers with a little hope that our apps will no longer break the most fundamental user assumptions.

We tried to get the browser history working in Bouldr, and to an extent, we succeeded. The difference between coding something proprietary, and using an open source solution, though is very great indeed, so without doubt, we’ll be refactoring to use this library in the future.

Here’s a little snippet that I use to quickly form tests to ensure that my models validation is working correctly from within unit tests:


  # In test_helper.rb
  # Give this function an array of invalid values, an array of valid values, the object and
  # attribute name to test, and it will ensure that only valid values are allowed
  def validation_tester ( valid_values, invalid_values, attribute_name, test_object )
    valid_values.each do |val|
      test_object.[]=(attribute_name, val)
      assert test_object.save, "#{attribute_name} should be valid: #{test_object.[](attribute_name)}"
      assert test_object.valid?
      assert !test_object.errors.invalid?(attribute_name)
    end

    invalid_values.each do |val|
      test_object.[]=(attribute_name, val)
      assert !test_object.save, "#{attribute_name} should NOT be valid: #{val}"
      assert !test_object.valid?
      assert test_object.errors.invalid?(attribute_name)
    end
  end

Then, use it as follows in your unit tests:


  def test_email_validation
    valid_emails = ['matt@test.com', 'matt.hall@test.com', 'matt@test.ing.com', 'matt.hall@test.ing.com']
    invalid_emails = ['@test.com', 'test.com', 'matt@', 'matt.hall@com', '.matt@test.ing.co.uk', 'matt@@test.com', '', '.@.']

    u = create_user
    validation_tester( valid_emails, invalid_emails, 'email', u)
  end

Can anyone suggest any improvements?


Creative Commons License


This work is licensed under a
Creative Commons Attribution 2.0 UK: England & Wales License.

The problem is simple; I wanted a method by which a model in my app could use a table from another MySQL Database (on the same server). The solution is equally simple:


  class MyModel &lt; ActiveRecord::Base
    set_table_name "other_db.table_name"
  end

Where other_db is the database name, and table_name is the table you want to use.

Just a quick bit of CSS that we came up with to display a percentage star rating that I’d like to share. This method requires two images - one with all of the stars highlighted, and one with them all greyed out:

Five Stars

Zero Stars

Note that these images are both 100 pixels wide (for simplicity) .

CSS


.starCont {
width: 100px;
height: 16px;
display: -moz-inline-box;
display: inline-block;
background-image:url(/images/zero-star.gif);
background-repeat: no-repeat;
background-position: bottom left;
vertical-align:text-bottom;
}
.stars {
display: -moz-inline-box;
display: inline-block;
height: 16px;
background-image:url(/images/five-star.gif);
background-repeat: no-repeat;
background-position: bottom left;
vertical-align:text-bottom;
}

.stars span {
visibility: hidden;
font-size: 1px;
}

HTML


<span class="starCont"><span class="stars" style="width:<%= percentage %>px"><span><%= percentage %>%</span></span></span>

Serving Suggestion

Use as part of a voting system whereby the percentage values are injected into the page via the value of *percentage*. If your star images are larger than 100px in width, then you will need to adjust the value that is calculated by an amount proportianal to the actual width.

I recently had to create a cron job that controlled the delivery of newsletters to a set of subscribers. Unfortunately, the code required some refactoring, but up until it could be modified, the task still needed firing at specific intervals.

I had a little trouble finding much on this subject, so here is the code I eventually used:

./script/runner -e production "app = ActionController::Integration::Session.new; app.get 'account/send_newsletters'"

With any luck, this will help another person save a couple of minutes!

Update

Since Rails 1.2, this method of executing rails controller actions has been throwing an “uninitialized constant ActionController::Integration” exception. Adding the following line to your environments.rb file fixes this problem:

require ‘action_controller/integration’

Thanks, Dimitry Hristov!

I came across a few pitfalls in my attempt to get my Textdrive account talking to my StrongSpace account over SSH without needing to enter my StrongSpace password at the prompt. I am setting out all of the steps necessary to get this working here so that others can avoid these same problems.

Prerequisites

You should be familiar with gaining access to and using BASH on your Textdrive account before you go any further - if you don’t then I suggest you go here for more information. I accept no responsibility for any damage or problems you may cause when using this tutorial. Make sure you take adequate backups and check any scripts before you run them.

Step 1: Creating the keys

To get started, we need to create a set of keys:

  1. Log into your Textdrive shell, and navigate to /users/home/USERNAME/.ssh/
  2. Run the following command
    ssh-keygen -b 1024 -f ss -P '' -t dsa
  3. This will create two files - ss and ss.pub

Step 2: Uploading the public key (ss.pub) to your StrongSpace account

  1. Download ss.pub to your local machine
  2. SFTP to your StrongSpace account
  3. Create a new directory called .ssh under your account
  4. chmod this directory to 700
  5. Upload ss.pub to this new folder
  6. rename it to authorized_keys
  7. chmod authorized_keys to 600

Step 3: Test!

OK, you now have your private key on your Textdrive account, and the corresponding public key on your StrongSpace account. We are good to test.

Go back to your Textdrive shell, and type:

ssh -i ss SS_USERNAME@SS_USERNAME.strongspace.com

You should now log directly into your account without needing to enter a password.

Notes:

If you already have an keys on separate lines.

If you are a Textdrive user, and you have one or more WordPress installations under your control, you might find the following BASH script to be of use. The script is fairly straightforward in that it manages the backup of your existing WordPress installation and database to StrongSpace, then it automatically downloads and extracts the latest version of WordPress.

Read More »