Class: ContactsController

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

Instance Method Summary collapse

Instance Method Details

#inquireObject



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/contacts_controller.rb', line 16

def inquire
	if !params[:other_email].blank?
		flash[:error] = "You must be a robot! No robots allowed here!"
		redirect_to request.referer || "/" and return
	end
	unless params[:spud_inquiry]
		flash[:error] = "Inquiry Not Found!"
		redirect_to request.referer || "/" and return
	end
	@inquiry_form = SpudInquiryForm.where(:id => params[:spud_inquiry][:spud_inquiry_form_id]).first
	if @inquiry_form.blank?
		flash[:error] = "Form Not Found!"
		redirect_to request.referer || "/" and return
	end
	@spud_inquiry = SpudInquiry.new(:spud_inquiry_form_id => params[:spud_inquiry][:spud_inquiry_form_id])

	@spud_inquiry.recipients = @inquiry_form.recipients
	@spud_inquiry.subject = @inquiry_form.subject

	@inquiry_form.spud_inquiry_form_fields.order(:field_order).each do |field|
		val = params[:spud_inquiry][field.field_name]
		if field.required && val.blank?
			flash[:error] = "Not all required fields were entered"
			@spud_inquiry.errors.add field.field_name,"is a required field"
		end

		if field.validation_rule
			if Regexp.new(field.validation_rule).match(val) == nil
				flash[:error] = "Not all fields were valid"
				@spud_inquiry.errors.add field.field_name, field.validation_error_message
			end
		end
		@spud_inquiry.spud_inquiry_fields.new(:name => field.name, :field_name => field.field_name,:value => val)
	end

	if !@spud_inquiry.errors.empty?
		respond_to do |format|
			format.html {render :action => "show"}
			format.js { render "show.js.erb"}
		end
		return
	end
	if @spud_inquiry.save
		flash[:notice] = "Your inquiry was received!"
		if !@spud_inquiry.recipients.blank?
			Spud::InquiryMailer.inquiry_notification(@spud_inquiry).deliver
		end
	else
		flash[:error] = "Whoops! Something went wrong. Please try again!"
		respond_to do |format|
			format.html {render :action => "show"}
			format.js { render "show.js.erb"}
		end
		return
	end
	respond_to do |format|
		format.html { redirect_to contact_thankyou_url }
		format.js { render "thankyou.js.erb"}
	end

end

#showObject



5
6
7
8
9
10
11
12
13
14
# File 'app/controllers/contacts_controller.rb', line 5

def show
	url_name      = !params[:id].blank? ? params[:id] : Spud::Inquiries.default_contact_form
	@inquiry_form = SpudInquiryForm.where(:url_name => url_name).includes(:spud_inquiry_form_fields).first

	if @inquiry_form.blank?
		flash[:error] = "Contact Inquiry Form not found!"
		redirect_to "/" and return
	end
	@spud_inquiry = SpudInquiry.new(:spud_inquiry_form_id => @inquiry_form.id)
end

#thankyouObject



80
81
82
# File 'app/controllers/contacts_controller.rb', line 80

def thankyou
	render
end