Tag Archives: plugin

Rails plugin development using WebMock

I had a few problems getting WebMock working with my rails 3 setup.

When I used webmock in my rails 3 app, the tests ran and all was nice.
But when I wanted to create a plugin using the exact same code as I just tested inside my rails app, I went into some problems.

To start, I created a new plugin using

rails generate plugin myclient

The files were generated and all seemed fine.
I then wanted to use the webmock plugin, since my client relied on some external services. (Which a test really shouldn’t do)

I had a really simple test case, just to get started:

require 'test_helper'
require 'webmock/test_unit'
require 'curb'

class MyClientTest < ActiveSupport::TestCase
  # Replace this with your real tests.
  test "the truth" do
    assert true
  end
  
  test 'webmock testing' do
    WebMock.stub_request(:any, 'www.example.com').to_return(:body => 'tester')
    data = Curl::Easy.perform("www.example.com")
    p data.body_str
    assert data.body_str == 'tester'
  end
end

When I ran “rake test” the test failed and the body that was returned, was the body of the actual page at www.example.com.
After bashing at the problem for a long time, I found out, that webmock doesn’t work if webmock is required before curb.

So… This was just a note about something that really took up too much of my time, and… yes, I should have know that webmock couldn’t “overwrite” the curb methods, if it was loaded in first.

Anyways, the final code that works!

require 'test_helper'
require 'curb'
require 'webmock/test_unit'

class MyClientTest < ActiveSupport::TestCase
  # Replace this with your real tests.
  test "the truth" do
    assert true
  end
  
  test 'webmock testing' do
    WebMock.stub_request(:any, 'www.example.com').to_return(:body => 'tester')
    data = Curl::Easy.perform("www.example.com")
    p data.body_str
    assert data.body_str == 'tester'
  end
end

WPML, what is the current language?

While building a wordpress theme that has multilingual support, I needed to know what the current language was for fixing some static parts of my template.

After searching a bit on the WPML site, I found a code example where you are shown howto create your own language selector menu:
http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/

I then wrote this function to get the current language of a site:

/** 
 * Function for getting the active language of the WPML plugin
 * @return the currently active language, defaulting to 'en'
 */
function getActiveLanguage() {
  // fetches the list of languages
  $languages = icl_get_languages('skip_missing=N&orderby=KEY&order=DIR');

  $activeLanguage = 'en';

  // runs through the languages of the system, finding the active language
  foreach($languages as $language) {
    // tests if the language is the active one
    if($language['active'] == 1) {
      $activeLanguage = $language['language_code'];
    }
  }

  return $activeLanguage;
}
?>

Just add this code to the theme’s functions.php file and you are ready to find the active language of your site!

(php) flash-message plugin

For a long time I’ve wanted a way to send messages from my controller to my view, when posting data. The message could be “Your update went well” or some error.

In the old days, I have done it like this:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."' where id = ".$_POST['id']);

if($result) {
  header("Location: /users/index.php?message='Some long message, that might bug out'");
}
else {
  header("Location: /users/edit.php?message='ERROR because....'");
}

?>

view, users/index.php and users/edit.php:

<?php
echo $_GET['message'];
?>

However, these days are over now. With my little plugin, you can easily pass messages from the controller to the view.
All you need to do, is to include the script on your pages, then call: flash(label, message) from your controller. From your view, you can call echo getFlash(label) and that is it!

Using the same example from before, we get the following:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."'  where id = ".$_POST['id']);

if($result) {
  // set the message
  flash('success', 'Some long message, that will NOT bug out');
  // do the redirect
  header("Location: /users/index.php");
}
else {
  // set the message
  flash('error', 'You had an error in your data.');
  // do the redirect
  header("Location: /users/edit.php");
}
?>

view, users/index.php:

<?php

echo getFlash('success');

?>

view, users/edit.php:

<?php

echo getFlash('error');

?>

Head on over to the github reposition at get it: http://github.com/jimmiw/php-flash-message

I hope you enjoy it

Snap Open forked!

I have been using Snap open in gEdit for quite some time and I love it. It’s fast and easy to use, plus it saves me from using my mouse. However, the “fast” part seems to loose it’s grounds, when you start using it in larger projects. I even started to have problems on java projects.

After some investigation, I found out that it also looked in my target folder. The .snap_open_ignore file helped a lot here though. But still the search was slow and some times the search window stalled for longer periods.

I decided to fork the current project on github and fix the problems myself. As you can see from the project data, the last commit date was around November 8th, 2008. So I assume the author sees it as complete. (meaning I will not try to submit patches to him).

Anyways, the problem with his version was, that everytime you typed, the search feature would do a file search on the disk. So, every time I typed a new letter, the plugin did a search… everytime. So.. I fixed this. Instead of doing the search every time I typed, it’s now doing it only once – The first time you open the window. This increased performance a LOT. It’s now a lot smoother to use the snap open plugin.

So head over to github.com/jimmiw, and check my version