Avoiding Ruby hash conditionals in Ruby on Rails

This gets really old:

if params[:teacher] && params[:teacher][:id] ...

so instead, do this:

if params[:teacher].try(:[], :id)

or do it a lot more:

name = params[:company][:owner][:name] if params[:company] and params[:company][:owner] and params[:company][:owner][:name]

turns into:

name = params.try(:[], :company).try(:[], :owner).try(:[], :name)

Yay for Stack Overflow!

Illinois Home Bakeries – for farmer’s markets only

Interesting tidbit about a new law that is being passed. IL Senate Bill 840 allows for “Cottage food operations” (i.e. you can use your home kitchen), with several stipulations, if and only if you are selling goods at a farmers’ market. So if I wanted to sell my cakes at a farmer’s market and got the appropriate paperwork, I could, but I still couldn’t sell them to individuals specifically out of my home. I’d have to have a table at a farmer’s market. Which is pretty much useless to me. Oh well, it’s a step in the right direction.

Article from the News-Gazette
Article from the IL Stewardship Alliance
Senate bill 840

jsTree: adding Expand All and Collapse All buttons

The documentation for jsTree is thorough, but not particularly easy to read. If you are looking for an easy way to add “Expand All” and “Collapse All” buttons, here’s one way:

<input type="button" value="Collapse All" onclick="$('#tree_container_id').jstree('close_all');">
<input type="button" value="Expand All" onclick="$('#tree_container_id').jstree('open_all');">

where ‘#tree_container_id’ is, of course, the ID of the container node for your tree.

Uploadify: changing scriptData with dropdowns

Uploadify is a pretty awesome jQuery/flash uploader. It’s made even more awesome by the stuff you can do with it on the fly.

For example, if you want to pass a variable chosen from a dropdown via the uploader, you can use uploadifySettings() to do so. Some people appear to have problems with this part, so here’s how I did it. (I’m not going to show you all the settings, since the docs give you a pretty good idea how to set it up. )

Say you have a dropdown in your form with the id of ‘upload_type’:

<select id="upload_type" name="upload_type">
<option value="1">First type</option>
<option value="2">Second type</option>

In your jQuery $(document).ready function (but outside the $(‘#upload’).uploadify function), put something like this:

$('#upload_type').change(function() {
var type_val = $(this).val();
$('#upload').uploadifySettings("scriptData", {"upload_type" : type_val});

Which means “Whenever the form field identified with ‘upload_type’ changes, update the ‘upload_type’ variable in scriptData if it exists; if it does not already exist in scriptData, add it.”

Note: I’ve only tried this in Uploadify 2.1.4.

Music industry shifts to being a service? Good news, bad news…

Here’s an interesting read from Ars Technica:

Did file-sharing cause recording industry collapse? Economists say no

The most interesting paragraph I see in this article is this one:

So what is emerging is an increasingly “ephemeral” global music culture based not upon the purchasing of discrete physical packages of music, but on the discovery and subsequent promotion of musicians through file sharing. The big winner in this model is not the digital music file seller, but the touring band, whose music is easily discoverable on the ‘Net. As with so much of the rest of the emerging world economy, the shift is away from buying things and towards purchasing services—in this case tickets to concerts and related activities.

So I have to wonder – to my friends (and my husband) who count on selling merch at their unpaid performances – is this a game changer? Will musicians from now on be ultimately unable to make a living unless they go on the road – and stay there?

Testing CKEditor in Ruby on Rails with Cucumber/Capybara

I have a textarea with the id of “request_details”, made into a CKEditor instance by jQuery: $(‘#request_details’).ckeditor();

(Check this page if you need some help getting CKEditor to work with jQuery, it’s dead simple)

Feature looks like this:  And I fill in "Here are some details" in the CKEditor instance "request_details"

Step looks like this:

When /^I fill in "([^"]*)" in the CKEditor instance "([^"]*)"$/ do |value, input_id|
browser = page.driver.browser

My least favorite phrase

I’ve decided on my least favorite phrase in the English language:

“Can’t you just…?”

Why is this my least favorite phrase? Because:

  1. It’s always in conjunction with a task that the questioner does not understand, but assumes should not be difficult at all;
  2. it is inherently second-guessing the person being asked, which is insulting;
  3. and, rather than asking if something is possible, as in “Can you?” or “Could you?”, the speaker is really saying, “This must be possible, and if you can’t do it, you’re an idiot.”

If you don’t understand how the system works, or what is involved with doing a task, please, ask if something is possible. Don’t demean the craftsman or browbeat them into doing whatever it is you want. A resentful craftsman is not on your side, and will not go out of his/her way to help you.