Class: Dust::ContactsController

Inherits:
AuthenticationController show all
Defined in:
app/controllers/dust/contacts_controller.rb

Instance Method Summary collapse

Methods inherited from AuthenticationController

#not_authenticated, #permission_denied, #try_return_to_previous_page

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/dust/contacts_controller.rb', line 18

def create
  @page = Dust::Page.find(1)
  @contact = Dust::Contact.new(params[:dust_contact])
  if @contact.save
    @contact.deliver_messages
    flash[:notice] = "Successfully Sent Message."
    redirect_to root_url
  else
    render :action => 'new', :layout => 'application'
  end
end

#csvObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/dust/contacts_controller.rb', line 51

def csv
  @contacts = Dust::Contact.order("created_at DESC")
  if @contacts.empty?
    flash[:error] = "There are no contact requests available."
    redirect_to dust_contacts_path
  else
    csv_string = CSV.generate do |csv|
      csv << ["Name", "Email", "message"]
      @contacts.each do |contact|
        csv << [contact['name'], contact['email'], contact['message']]
      end
    end

    send_data(csv_string,
              :type => 'text/csv; charset=utf-8; header=present',
              :filename => "AllDust::Contacts#{Date.today}.csv"
             )
  end
end

#csv_importObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/dust/contacts_controller.rb', line 71

def csv_import
  csv = CSV.new(params[:csv_import][:file])
  csv.each do |row|
    Dust::Contact.create(:name => row[0], 
                         :email => row[1],
                         :message => row[2]
                        )
  end
  csv.close
  flash[:notice] = "Successfully added some Dust::Contact(s)."
  redirect_to contacts_path
rescue => exception
  # If an exception is thrown, the transaction rolls back and we end up in this rescue block
  error = ERB::Util.h(exception.to_s) # get the error and HTML escape it
  flash[:error] = "Error adding logs. (some #{error}). Please try again."
  redirect_to contacts_path
end

#destroyObject



44
45
46
47
48
49
# File 'app/controllers/dust/contacts_controller.rb', line 44

def destroy
  @contact = Dust::Contact.find(params[:id])
  @contact.destroy
  flash[:notice] = "Successfully destroyed contact."
  redirect_to dust_contacts_url
end

#editObject



30
31
32
# File 'app/controllers/dust/contacts_controller.rb', line 30

def edit
  @contact = Dust::Contact.find(params[:id])
end

#indexObject



10
11
12
# File 'app/controllers/dust/contacts_controller.rb', line 10

def index
  @contacts = Dust::Contact.page(params[:search], params[:page], params[:date])
end

#newObject



14
15
16
# File 'app/controllers/dust/contacts_controller.rb', line 14

def new
  @contact = Dust::Contact.new
end

#updateObject



34
35
36
37
38
39
40
41
42
# File 'app/controllers/dust/contacts_controller.rb', line 34

def update
  @contact = Dust::Contact.find(params[:id])
  if @contact.update_attributes(params[:dust_contact])
    flash[:notice] = "Successfully updated contact."
    redirect_to @contact
  else
    render :action => 'edit'
  end
end