Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

LABS
Standup 10/29/08

Interesting Things

  • has_many_polymorphs preloads models

has_many_polymorphs will scan through the models directory for any files that include the plugin and preload them. This can cause problems if these models depend on other plugins (that have not yet been loaded) to operate correctly. The has_many_polymorphs source mentions this behavior in a comment and includes a fix:

Searches for models that use has_many_polymorphs or acts_as_double_polymorphic_join and makes sure that they get loaded during app initialization. This ensures that helper methods are injected into the target classes.

Note that you can override DEFAULT_OPTIONS via Rails::Configuration#has_many_polymorphs_options. For example, if you need an application extension to be required before has_many_polymorphs loads your models, add an after_initialize block in config/environment.rb that appends to the ‘requirements’ key:

  Rails::Initializer.run do |config|
    # your other configuration here

    config.after_initialize do
      config.has_many_polymorphs_options['requirements'] << 'lib/my_extension'
    end
  end

Instead of using config.has_many_polymorphs_options, we were able to achieve the same effect by simply dropping our required plugins into the after_initialize block. If we decide to remove has_many_polymorphs in the future, the config file should still run correctly. It’s worth noting that the UltraSphinx search plugin operates in a similar fashion.

  • returning JavaScript in an Ajax IFrame Method (AIM) call

If you are using AIM for file uploads, you may find that any content returned to the IFrame is escaped (as if it were regular HTML). On some browsers, this occurs regardless of what the content-type is set to. A workaround is to embed any content that you don’t want escaped into a comment tag; after the request completes, you can use JavaScript to retrieve the contents and optionally eval them.

Ask for help

  • Ajax click_and_wait for Selenium?

assert_text_present offers a simple way to test Ajax behaviors in Selenium, as it will wait 5 seconds (during which the Ajax callback can complete) while looking for the specified text (which can be the body of your Ajax response). Another strategy with which we’ve had some luck is to write a custom method that will wait a specified amount of time before continuing (particularly useful if your Ajax call takes a lengthy amount of time to complete).

Comments
  1. Erik Hanson says:

    Regarding ajax click_and_wait for Selenium: I’ve written Selenium tests for a couple ajax-heavy apps, and in both cases we had the concept of a request queue to handle the case where the user was doing things more quickly than the limited number of concurrent ajax requests would allow.

    So the “wait” part of our click_and_wait methods would just wait until the queue was empty. That was far less fragile than guessing how long the request should take. It was also faster because we only had to wait the minimum amount of time, instead of the maximum.

    If you don’t have a request queue, you might be able to set up a simple stack and push something onto it with every request and pop with every response — then your test can just wait until the stack is empty. Or create an array that acts as a log of each request and response, clear the log before performing the action from the test, and then assert that the log contains what you think it should after you perform the action.

  2. Dan Manges says:

    If you’re using the prototype javascript library, here’s a way to wait until all ajax calls are complete:

    http://davidvollbracht.com/2008/6/4/30-days-of-tech-day-3-waitforajax

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *