aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/new_controller.rb
blob: 1da6f1398446ab40ddc4130bdbedd63553b70b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
# app/controllers/new_controller.rb:
# Interface for building a new FOI request.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: new_controller.rb,v 1.4 2007-10-10 16:06:17 francis Exp $

class NewController < ApplicationController
    def index
        # render index.rhtml template
    end

    def create
        # Create both FOI request and the first request message
        @info_request = InfoRequest.new(params[:info_request])
        @outgoing_message = OutgoingMessage.new(params[:outgoing_message].merge({ 
            :status => 'ready', 
            :message_type => 'initial_request'
        }))
        @info_request.outgoing_messages << @outgoing_message
        @outgoing_message.info_request = @info_request

        # This automatically saves dependent objects, such as @info_request, in the same transaction
        if not @info_request.valid?
            render :action => 'index'
        elsif authenticated?
            @info_request.user = authenticated_user
            @info_request.save
            flash[:notice] = "Your Freedom of Information request has been created."
            redirect_to :controller => 'request', :id => @info_request
        end

        # Save both models
#        valid = @info_request.valid? 
#        valid &&= @outgoing_message.valid? # XXX maybe there is a nicer way of preventing lazy boolean evaluation than this
#        if valid
#            if authenticated?
#                @info_request.save!
#                @outgoing_message.save!
#            end
#        else
#            render :action => 'index'
#        end
    end

    private

end