Skip to content

inspire9/spanish_inquisition

Repository files navigation

Spanish Inquisition

Nobody expects the Spanish Inquisition!

This is a simple and opinionated survey library for Rails. It generates the form using Formtastic, and it does not deal with persistence – the surveys are plain Ruby objects, not ActiveRecord objects. It fulfills our needs at Inspire9 nicely – and perhaps it’s what you’re looking for too.

Installation

It’s a gem – so, like any other gem, add it to your app’s Gemfile:

gem 'spanish_inquisition'

And then execute:

$ bundle

Or install it yourself as:

$ gem install spanish_inquisition

Usage

Firstly, you’ll want to define your surveys in files within app/surveys. Here’s a quick example:

SpanishInquisition::Survey.new :inquisition do |survey|
  survey.page do |page|
    page.question :weapon do |question|
      question.text    = 'What is your chief weapon?''
      question.style   = :one
      question.answers = ['fear', 'surprise', 'ruthless efficency']
    end

    page.question :torture do |question|
      question.text    = 'What do you torture with?'
      question.style   = :one
      question.answers = ['The Rack', 'The Cushions', 'The Comfy Chair']
    end
  end

  survey.page do |page|
    page.question :comments do |question|
      question.text     = 'Do you have any suggestions for better torture devices?'
      question.style    = :text
      question.required = false
    end

    page.question :cardinal do |question|
      question.text =  'Who is your favourite Cardinal: Ximinex, Biggles or Fang?'
      question.style = :string
    end
  end
end

Questions are required by default, and currently only three question styles are supported: :string (a standard text field), :text (a multi-line text field, aka the textarea HTML element), and :one (many options, single choice, aka radio inputs). The answers value can be anything that Formtastic accepts as a collection (so both arrays and hashes are valid settings).

Rendering the form is done via a presenter:

survey = SpanishInquisition::Presenter.new :inquisition, params[:survey]

And then, this presenter can be used within a Formtastic-built form in your views:

<%= semantic_form_for survey do |form| %>
  <%= survey.render_to form %>
  <%= form.actions do %>
    <%= form.action :submit, :label => 'Save my answers' %>
  <%- end %>
<%- end %>

When the form is posted back, you will have to persist the results somewhere – they can be obtained via the answers method on the presenter. It will return a hash with each question’s identifier as the keys, and the supplied answers as the values.

There’s a few more optoins available with questions, but the source is simple and uncomplicated – I’d recommend reading it.

Contributing

This gem is built using git-flow – so keep in mind that the master branch is the release branch, develop is work-in-progress, and any in-progress features get their own feature branches.

  1. Fork it
  2. Create your feature branch (`git checkout -b feature/name`)
  3. Commit your changes (`git commit -am ‘Add some feature’`)
  4. Push to the branch (`git push origin feature/name`)
  5. Create new Pull Request

Licence

Copyright © 2013, Spanish Inquisition is developed and maintained by Pat Allan and Inspire9, and is released under the open MIT Licence.

About

Simple Ruby survey DSL

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages