Controller: Hooks - convio/watirmark GitHub Wiki

The controller has a built-in set of hooks that will fire if you define the method in your controller. These are extremely useful for cases where the behavior of an html element deviates from the general case.

KEYWORD_value

This is great for cases where you want to normalize the string so the value is always formatted properly. (Note that this can cause issues when you want to force a failure when inputs are not well-formed)

  # for the keyword :currency
  def currency_value
    "$#{@model.currency}"
  end

before_KEYWORD

Executes an action before a keyword is populated

  # for the keyword :profile_image
  def before_profile_image
    @view.image.fire_event('OnChange')
  end

after_KEYWORD

Executes an action after a keyword is populated

  # for the keyword :profile_image
  def after_profile_image
    @view.upload_button.click
  end

populate_KEYWORD

Overrides the population of a given keyword

  # for the keyword :teamdivisions
  def populate_teamdivisions
    @model.teamdivisions.each do |team|
      @view.teamdivision.set team
    end
  end

verify_KEYWORD

Overrides the verificationof a given keyword

  # for the keyword :image
  def verify_image
    if @view.uploadimage.exists?
      assert @model.image == 'nil'
    elsif @view.uploaddifferentimage.exists?
      assert @model.image != 'nil'
    end
  end

When all else fails

Overriding default population and verification

There are some cases where you want to add some additional handling or override the default behavior of the controller. The blunt instrument equivalent is to modify the method. This is almost always the very last resort and you should try to use something from above. When you do this you generally short-circuit a lot of what the MVC framework gives you unless you're judicious of when you do this.

  # Override how the controller populates.
  def populate_data
    super # to call the default populate
    # do something
  end

  # Override how the controller verifies.
  def verify_data
    super # to call the default verify
    # do something
  end

  # Override the default submit behavior which is
  # to click a Save/Next/Finish button.
  def submit
    @view.completebutton.click
  end