Converting a ruby string to a UUID

Today I was working on some embedded flash video business (ugh). One of the requirements was converting a 36 character string into a UUID (sometimes referred to as a GUID). I could have hacked up a quick regex but figured there was already a library to accomplish such a task. Enter UUIDTools. The code to convert the string "899c8ad8d54a4f8093256f78bc93d5a4" to "899c8ad8-d54a-4f80-9325-6f78bc93d5a4" is as follows:


s = "899c8ad8d54a4f8093256f78bc93d5a4"
UUIDTools::UUID.parse_hexdigest(s).to_s

Rails 3.2 Controller Testing File Uploads using Rspec

I needed to write a controller test that included a file attachment (the model uses Dragonfly). Below is how I structured the test. It took me a bit of googling to locate the exact syntax. The magic for Rails 3.2 is using Rack::Test::UploadFile.

Inserting HTML into a HAML template

This is more of a "note to self" post. I recently had a case where a Rails views helper was outputting raw HTML. I needed it to display within a HAML template. Rather than modify the helper to output HAML one can add the following code:

def my_helper_function
  Haml::Util::html_safe 'GOOGLE!'
end

The HTML in my case was significantly more complex than the example. The key component is the use of the "Haml::Util::html_safe".

Scripting Simple Firewall Rules

One of the challenges of using a VPS or cloud service like EC2 is you are on your own when it comes to securing the server. One of the first line of defenses I typically use when provisioning a new box is setting up a simple firewall. IPTABLES is the defacoto linux firewall. While IPTABLES is amazing in its flexibility it is yet another DSL to learn. Enter UFW, UFW is a simple firewall administration utility that generates a robost IPTABLES rule set following a simple syntax.

Re-ordering Reference Fields in Drupal 7

Normally node_reference fields are displayed by the order they were selected. I'm working on a project that needed them ordered by taxonomy term, then date, descending. In the days of yore (D6) this could have been easily resolved via db_rewrite_sql with a modification to the order by clause. With D7 that functionality has been replaced with hook_query_alter, which I absolutely loathe (... a blog posting for another time).

SEO Friendly Views Contextual Filters in Drupal 7

I needed a way to have views create a listing of nodes belonging to a particular taxonomy term. Contextual filters does a pretty good job of this. The problem is if you want to query by "SEO friendly" values. Ex:

Setting an Active Menu from Path in Drupal 7

Practically every Drupal site I've worked on requires setting an active menu item based on the URL of a node. URL's are typically generated via path auto. A typical example would be:

news - A view listing ALL news articles
news/research - A view listing RESEARCH news
news/research/kappo-opioid-receptors

Visually it would look kind of like:

Context Based Template Suggestions in Drupal 7

The project du jour has several "News" node types that are more or less identical. Rather than have a custom node template for each type I wanted them to share a common template. Typically i would add a template suggestion in the themes node preprocessor. Ex:

Adding Custom Rules Conditions in Drupal 7

I recently needed to create a rule that notifies users of a certain role when their comment is published. The rule generates a custom email to send them. Much to my surprise, there is not a default Rule condition for "Comment is Published". I found this to be interesting as there is one for "Content is Published". Fortunately the Rules API is flexible and fairly well documented. I had to create a custom 1-off module to define this condition but the code itself was very simple

Simple OSX Command Line Find and Replace

This is another "note to self" post. I needed to perform a bulk find and replace today. Sed is awesome for the task. Code below:


find . -name '*.module' -exec sed -i "" 's/oldprefix_/newprefix_/g' {} \;

The code above will find all files with a name of .module and replace the text oldprefix_ with the text newprefix_. This code is specific for OSX as it requires the blank extension arg "", when using sed with -i.

Pages

Subscribe to Metachunk RSS
© 2010 Nicholas G. Maloney. Drupal theme by Kiwi Themes.