Collection Select Helper and OnChange Event in Rails

Given a collection of Active Record objects, you may use the collection_select helper method to produce a select form field. You need to pass in a number of arguments to the helper function.

1) object – your model object used in the collection
2) method – a valid model attribute or method
3) collection – a collection of active record model objects
4) option_value – value being set from the model for the <option value=”option_value”> html element
5) option_name – what is displayed for the user e.g., <option> option_name <
6) option – general options
7) options for the select html element

# helper and arguments...
# collection_select( model, id, collection, option_value, option_name, options, html_options)

&lt;%= collection_select("states", "state_id",
  State.participating,
  "abbreviation", "name",
   {:selected=&gt; get_current_state_or_nil },
   {:onchange=&gt;"document.location='/states/'+this.value"}
) %&gt;

#which will produce something like...

<pre id="line27">&lt;select id="state_id" name="state[id]" onchange="document.location='/states/'+this.value"&gt;
&lt;option value="AL"&gt;Alabama&lt;/option&gt;
&lt;option value="AK"&gt;Alaska&lt;/option&gt;
&lt;option value="AZ"&gt;Arizona&lt;/option&gt;
&lt;option value="AR"&gt;Arkansas&lt;/option&gt;
&lt;/select&gt;

Get Child Categories of Parent Category on Single Post in WordPress

I was hoping to find a function in the WordPress API, that goes something like this…

  the_child_categories("Vermont");
  // print Burlington, Brattleboro... etc if this post has any child categories of Vermont

But I could not. The result that I did find, from various forums goes something like this…

$parentcat = get_category_by_slug('Vermont');
foreach((get_the_category()) as $childcat):
  if (cat_is_ancestor_of($parentcat, $childcat)):
    echo get_category_link($childcat->cat_ID);
    echo $childcat->cat_name;
  endif;
endforeach;

If you’re in the in the Loop, you can use the get_the_category() function to retrieve the category objects of the current post, without printing the results to the screen. You can loop over the contents from what is returned by the function.

This condition uses the API function “cat_is_ancestor_of” to check whether or not the category from the current post is a child of the parent which we fetched with the “get_category_by_slug” function.

Here is a little function that I threw into “functions.php” so that I can get the child categories easily in my theme.

/** Usage
_the_category_children("Vermont"); */
function _the_category_children($slug=""){
  if($categories       = get_the_category()):
    if($slug_category   = get_category_by_slug($slug)):
      foreach($categories as $category):
        echo (cat_is_ancestor_of($slug_category, $category)) ? $category->cat_name : '';
      endforeach;
    endif;
  endif;
}

More info/references on accomplishing this functionality are available
http://codex.wordpress.org/Function_Reference/cat_is_ancestor_of
http://wordpress.org/support/topic/284057?replies=8#post-1120489
http://wordpress.org/support/topic/297615

My Review of Moodle 1.9 Extension Development

I wrote a review for Joseph Thibault’s Moodle News on extension development for Moodle. The book is quite good and I think an essential resource for anyone wanting to develop in Moodle. The book focuses on plugin development, but it will also give you an overview of the architecture, api and best practices.

I wish I had this book about 3 years ago when I first started fooling around in the code base! At any rate, you can read the review on Moodle News at http://www.moodlenews.com/2010/moodle-1-9-extension-development-review-by-bseanvt/

There is also an interesting discussion underway about the ‘bloat’ in the Moodle code at http://www.moodlenews.com/2010/opinion-1000000-lines-of-code/

7 Dec 2009, 9:34pm
Linux mac os x:
by

1 comment

Updating Your Twitter Status with cURL and a Bash Function

I’m usually at the command line so I wrote a little a bash function so that i can type

tweet this is really neat but kind of pointless

and it will update my twitter status! some characters trip it up but in general it’s useful for most of my tweets. The tweet function just spits out the arguments passed to it for the status parameter for the API call to twitter.

Add the following to the .bash_profile file and reload the terminal (don’t forget to add your email and pwd where appropriate).

tweet() {
   curl -u your_twitter_email_addr:your_twitter_passwd -d status="$*" http://twitter.com/statuses/update.xml
 }
 

*** Twitter still uses http basic authentication for their API. However, they are moving away from it in favor of oAuth. So I’m not sure how long this fun will last :{

Ruby on Rails, jQuery and YUI API Docs Available as Mac OS X Dictionary Binaries

I came across an awesome tool this morning. Priit Haamer has chunked Ruby on Rails, jQuery, and some of YUI documentation into native Mac OS X dictionary binaries. This lets you search those API docs from Spotlight, TextMate, any application that uses the dictionary app!

I have tested the Ruby on Rails API within TextMate. Hover over any function and hit ctl+cmd+d and a little popup will give you a glimpse at the API docs. Seems like a nice alternative to ACK or the normal TextMate function lookup.

More information and installation instructions are available at Priit’s site http://www.priithaamer.com/blog

Here is a nice video of the tool