Class: FormsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/forms_controller.rb

Instance Method Summary collapse

Instance Method Details

#newObject



7
8
9
10
11
12
# File 'app/controllers/forms_controller.rb', line 7

def new
  @response = find_or_create_response
  @response.update_attribute(:result, nil)
  
  redirect_to :back rescue redirect_to '/'
end

#updateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/forms_controller.rb', line 14

def update
  @page = Page.find(params[:page_id]) rescue Page.first
  @page.data    = params 
  @page.request = OpenStruct.new({
    :referrer => request.referer,
    :session => session # Creating a pretend response object
  })
  
  @form = Form.find(params[:id])
  @form.page = @page
  
  # We need a response object
  @response = find_or_create_response
  # Put the submitted data into the response object
  @response.result = Forms::Config.deep_symbolize_keys(params)
  
  begin
    # Grab the form configuration data
    @form[:extensions] = Forms::Config.convert(@form.config)
  rescue
    raise "Form '#{@form.title}' has not been configured"
  end
  
  @results =  {}
  # Iterate through each configured extension
  @form[:extensions].each do |name, config|
    result = @form.call_extension(name,config)
    
    @results.merge!({ name.to_sym => result })
    session.merge!(result[:session]) if result[:session].present?
  end
  # Those results are merged into the response object
  @response.result = @response.result.merge!({ :results => @results})
  
  begin
    @response.save!
    redirect_to (@form.redirect_to.present? ? @form.redirect_to : (params[:redirect_to] || :back))
  rescue
    "Form '#{@form.title}' could not be submitted."
  end
end