Activating Apache/PHP on Mac alongside Pow server

Instead of rewriting a step-by-step guide on how to do this, I can recommend two great articles that will get you setup in no time.

First read this article on how to setup Apache and PHP on your Mac:
http://superfancy.net/coding/php-mysql-apache-in-mac-osx-leopard/

Next, follow the instructions from 37signals on using Pow alongside Apache. Make sure to pay attention to the troubleshooting section if you had any trouble.
https://github.com/37signals/pow/wiki/Running-Pow-with-Apache

Stub RSpec cookies to set them in your tests

I have recently been in the situation where I was testing a controller that looked up the value of a cookie in a before_filter to validate the user.

I had trouble setting the cookie from within the controller spec so that it was available to the before_filter. The solution that I want to share is

controller.stubs(:cookies).returns({:user_session => 'xxxx'})

The reason I could not get this working earlier was that I was mistakenly trying to stub the cookies method directly. Instead I have to stub the controller cookies, even if it is a private method.

Detect if page is within Facebook iframe or not: Javascript

Make sure to check the update at the end of the article. 

There is a handy piece of Javascript which you can use to tell if the current page is within the Facebook canvas iframe, or if it is being viewed normally in the browser.

The Facebook canvas gives its window a special "name" attribute. In most cases unless you generated the browsing window, it will have no name so we can use the piece of code below.

  if(window.name != "") {
    //We are on Facebook
  } 
  else
  {
    //We are just in the normal browser window
  }

However, if you changed your window name through Javascript, change the IF statement to match.

With this information, we can hide, add or manipulate our HTML in any way that is required to highlight the difference between your Facebook app and the regular web app.

 

Update Feb 2016:

As noted by Kilian in the comment section, an imporved way to detect the Facebook iframe is:

if(window.name.indexOf('iframe_canvas_fb') > -1)

Rails 3 check if the user has specified request.format

The request.format object in Rails is always set. If the user has not specified a value in their URL, e.g. example.com/projects.json, then it will default to :html.

To check if the user has chosen the request format, use this piece of code:

    params[:format].nil?

Fix Fast Debugger (ruby-debug-ide) on Windows 7

I've finally found a solution that will make the Fast Debugger (a.k.a ruby-debug-ide) work with Netbeans 6.9.1+ on Windows 7. This solution should also work for anyone using Vista, which I know from experience has similar issues.


First step: make sure the Ruby DevKit is installed. Full instructions for installing it are here on Github: https://github.com/oneclick/rubyinstaller/wiki/Development-Kit.

The DevKit basically temporarily enhances Windows and allows tricky Gems to be compiled.

Second step: Next you must install the correct debug Gems. If your Ruby version is 1.9.2+ then you will require the gems ending with a "19".

The way to make the gems install successfully is through Netbeans. Go to Tools > Ruby Gems > New Gems.

Do a search for "ruby-debug" and you shall see a list similar to this:

Install the "latest" version of all of the Gems which I have highlighted. Do not try and install "ruby-debug".

Third step: Once the Gems have installed, open up and new Command Prompt, run the command "gem list" and check that all of the Gems are listed, if not try to install them manually.

Next, the "rdebug-ide" file must be edited. Find this by browsing to (or similar):
C:\Ruby192\lib\ruby\gems\1.9.1\gems\ruby-debug-ide19-0.4.12\bin

 

Find the line (around 79):

Debugger::PROG_SCRIPT = ARGV.shift

Replace it with:

script = ARGV.shift
Debugger::PROG_SCRIPT = (script =~ /script([\\\/])rails/ ? Dir.pwd + $1 : '') + script
puts Debugger::PROG_SCRIPT
#Debugger::PROG_SCRIPT = ARGV.shift

Final step: Save the file and restart your computer. When Netbeans is reopened, click the "Debug main project" button to start the server in debug mode.

Thanks must be given to the following for help in figuring this one out:

Fix Rails error: The specified module could not be found: mysql-2.8.1-x86-mingw32 mysql_api.so (LoadError )

After updating my Rails version to 3.0.7 I have been experiencing the error:

The specified module could not be found. - C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so (LoadError )

This occurs every time I try to load the Webrick server.

The solution is to find the "libmysql.dll" file and copy it to your Ruby bin.

The location of these folders on Windows 7 is:
C:\Program Files\MySQL\MySQL Server 5.5\lib
and
C:\Ruby192\bin

As soon as that DLL is copied you can start the server without any problems.

Sort string array values as integer with PHP

Sometimes you'll want to sort an array by its values. Normally you can use one of the PHP sort functions such as sort() or usort(). This will sort your array into ascending order for either integer, starting with zero or string, from A - Z.

However, you may have the following array:

 
$scores = array(
	'32-Andy',
	'43-Steve',
	'28-Joe'
);

If you try to sort these by the ID (28, 32, 48) the sort function will not take just the numbers into account. It will also then sort on the string, from A to Z.

There's a simple way to fix this. We need to upgrade the usort() function by adding an extra argument. We'll add the SORT_NUMERIC predefined constant as the second argument and magically PHP will ignore sorting on the string part of the array values and instead successfully sort the array by the ID.

Using the following code will output the array in the order: 2, 0, 1 as expected.

usort($scores, SORT_NUMERIC);
print_r($scores);
//0 = 28-Joe
//1 = 32-Andy
//2 = 43-Steve

© Blake Simpson, 2012 – 2017