Feb 14 2008

refresh_to plugin rspec matcher

etienne @ 10:00 pm

We have been using the refresh_to plugin in one of our projects. For those who do not know, the refresh_to plugin adds a refresh_to method to the ActionController::Base which allows us to avoid the IE security warnings when moving from a secure SSL page to an insecure page.

We are using rspec for our testing and when I went to write some tests to test some code that uses the refresh_to method I found out that the standard redirect_to rspec matcher does not work with refresh_to. The same problem exist for the standard test method assert_response :redirect and assert_redirected_to my_url, but the plugin does supply replacement tests for these methods. The problem is that it does not supply a replacement for the rspec matcher so I had to write my own. 

# The standard rspec redirect_to matcher does not work with the refresh_to
# plugin methods, i.e. refresh_to and refresh_back_or_default.
# This is also the case with the with the standard Rails functional
# tests - assert_response :redirect and assert_redirected_to my_url.
# The plugin has replacement test methods for these but they do not
# have an rspec replacement for redirect_to so I’ve had to write my own.
#
module Spec
  module Rails
    module Matchers
     
      class RefreshTo  #:nodoc:

        def initialize(controller, expected)
          @expected = expected
          @controller = controller
        end

        def matches?(response)
          match_data = response.body.match(/<meta http-equiv="refresh" content="0;url=([\S]+)">/)
          return false if (!match_data)
          parts = match_data[1].split("http://test.host")
          @actual = parts.last
          return match()
        end
         
        def match
          case @expected.class.to_s
            when ‘String’
              parts = @expected.split("http://test.host")                      
              @expected = parts.last
              return @actual == @expected
            when ‘Hash’
              @expected = @controller.url_for(@expected)
              return match()
          end         
        end

        def failure_message
          return %Q{expected redirect to #{@expected.inspect}, got redirect to #{@actual.inspect}}
        end

        def negative_failure_message
          return %Q{expected not to be redirected to #{@expected.inspect}, but was}
        end

        def description
          "redirect to #{@actual.inspect}"
        end
      end
     
      # :call-seq:
      #   response.should refresh_to(url)
      #   response.should refresh_to(:action => action_name)
      #   response.should refresh_to(:controller => controller_name, :action => action_name)
      #   response.should_not refresh_to(url)
      #   response.should_not refresh_to(:action => action_name)
      #   response.should_not refresh_to(:controller => controller_name, :action => action_name)
      #
      # Passes if the response is a redirect to the url, action or controller/action.
      # Useful in controller specs (integration or isolation mode).
      #
      # == Examples
      #
      #   response.should refresh_to("path/to/action")
      #   response.should refresh_to("http://test.host/path/to/action")
      #   response.should refresh_to(:action => ‘list’)
      def refresh_to(opts)
        RefreshTo.new(controller, opts)
      end
    end

  end
end 


Jan 11 2008

SMERF Homepage Finished

etienne @ 2:17 pm

I’ve now completed the SMERF web site. On the site you will find details about SMERF including documentation, download information and so on. To go to the site now click on this link.


Jan 09 2008

SMERF Released!

etienne @ 5:21 pm

 

Update 11 January 2008: SMERF web site completed.

I’m happy to announce the release of SMERF V0.0.1!

I’m still in the process of setting up the official web site but the source is now available for download. The README file contains a lot of information about the plugin including installation and configuration notes. You can also checkout the documentation in the rdoc directory which includes the API docs and the README in a more readable format.

There are several ways to download SMERF:

Direct download

Get it from RubyForge

Subversion

**Create a new smerf directory in your /vendor/plugins directory and change into the new directory

Check out the stable release

svn checkout http://smerf.rubyforge.org/svn/stable .

or Check out a particular release

svn checkout http://smerf.rubyforge.org/svn/tags/REL-0.0.1 .

or Check out the development code

svn checkout http://smerf.rubyforge.org/svn/trunk .

See the README for details of how to install and configure the plugin.

Until I get my bug tracker sorted out if you have any problems please feel free to contact me on smerf at cascadia dot com dot au.

Enjoy!

 


Jan 02 2008

Announcing SMERF - Simple MEta Rails Form

etienne @ 6:18 pm

 

Update 11 January 2008: SMERF web site completed.

Update 9 January 2008: SMERF Released! 

Update 8 January 2008: I’ve completed the documentation and testing. I’ve created a project on RubyForge which is waiting approval. If this is not ready by tomorrow I will make an alternate link available for download, so if all goes well I should have a download available tomorrow.

I am putting the final touches to a new plugin called SMERF which stands for Simple MEta Rails Form. The plugin is a result of me extracting the code I used for one of my projects which I discussed in an earlier article Ruby on Rails Survey Builder.

I’ve spend the past few weeks rewriting parts of it to make it more generic so that it can be used for all sorts of things including survey’s, questionnaires, data collection forms and so on. Some of the important changes I’ve made include

  • Allow question validations to be written using Ruby code. You can now create your own validation methods as required, there are also a couple of standard system validation methods that come with the plugin. You can specify any number of validation methods for a question by comma separating each method name, e.g. validation: validate_mandatory_question, validate_date_format
  • User responses are now stored in a DB table allowing you to perform analysis on the question responses using SQL.

ToDo V0.2

  • Currently there are no tests
  • Allow forms to be broken into smaller forms, currently you get all questions on a single form
  • Allow the save of user responses as you move from form to form

I’m currently writing a plugin generator that will make installation of the plugin easier, hopefully I’ll have this done very soon. I then need to finish the documentation which is mostly done.

I will keep you informed and let you know when the plugin will be available.