module Stepper::ActiveRecordAdditions::ClassMethods

Public Instance Methods

has_steps(options = {}) click to toggle source

Sets up methods and define steps. For example, you have model Company and you want to fill it fields in few steps description, kind and address:

class Company < ActiveRecord::Base
  has_steps :steps => %w{ description kind address }
end

Model should have current step column, by default it name is current_step. It should be added by migration:

add_column :companies, :current_step, :string
add_index  :companies, :current_step

The column name can be set up with option current_step_column.

Options:

:steps

It is required option. Define steps for multistep form.

:current_step_column

Define what field use for save current step of form. Default current_step

# File lib/stepper/models/active_record_additions.rb, line 32
def has_steps(options = {})
  #check options
  raise Stepper::StepperException.new("Options for has_steps must be in a hash.") unless options.is_a? Hash
  options.each do |key, value|
    unless [:current_step_column, :steps].include? key
      raise Stepper::StepperException.new("Unknown option for has_steps: #{key.inspect} => #{value.inspect}.")
    end
  end

  raise Stepper::StepperException.new(":steps condition can't be blank") if options[:steps].blank?

  #set current step column
  class_attribute :stepper_current_step_column, :instance_writer => false
  self.stepper_current_step_column = options[:current_step_column] || :current_step

  class_attribute :stepper_options, :instance_writer => false
  self.stepper_options = options

  self.validate :current_step_validation

  include InstanceMethods
end