|
1 | 1 | # encoding:UTF-8 |
2 | 2 | class ContactsController < ApplicationController |
3 | | - before_filter :authenticate_edit, only: [:new, :edit, :update, :destroy] |
4 | | - before_action :set_contact, only: [:show, :edit, :update, :destroy, :mail] |
| 3 | + load_permissions_and_authorize_resource |
5 | 4 |
|
6 | 5 | def index |
7 | | - @contacts = Contact.all |
8 | 6 | end |
9 | 7 |
|
10 | 8 | def show |
11 | | - @sent = false |
12 | 9 | end |
13 | 10 |
|
14 | 11 | def new |
15 | | - @contact = Contact.new |
16 | 12 | end |
17 | 13 |
|
18 | 14 | def edit |
19 | 15 | end |
20 | 16 |
|
21 | 17 | def mail |
22 | | - if (params[:name]) && (params[:email]) && (params[:msg]) |
23 | | - @name = params[:name] |
24 | | - @email = params[:email] |
25 | | - @msg = params[:msg] |
26 | | - ContactMailer.contact_email(@name, @email, @msg, @contact).deliver_now |
27 | | - respond_to do |format| |
28 | | - format.html { redirect_to @contact, notice: 'Meddelandet skickades.' } |
29 | | - format.json { render action: 'show', status: :created, location: @contact } |
30 | | - @sent = true |
31 | | - end |
| 18 | + if @contact.mail(mail_params) |
| 19 | + redirect_to @contact, notice: 'Meddelandet skickades.' |
| 20 | + else |
| 21 | + redirect_to @contact, alert: 'Någonting blev fel, prova att skicka igen.' |
32 | 22 | end |
33 | 23 | end |
34 | 24 |
|
35 | 25 | def create |
36 | | - @contact = Contact.new(contact_params) |
37 | | - respond_to do |format| |
38 | | - if @contact.save |
39 | | - format.html { redirect_to @contact, notice: 'Kontakten skapades, success.' } |
40 | | - format.json { render action: 'show', status: :created, location: @contact } |
41 | | - else |
42 | | - format.html { render action: 'new' } |
43 | | - format.json { render json: @contact.errors, status: :unprocessable_entity } |
44 | | - end |
| 26 | + if @contact.save |
| 27 | + redirect_to @contact, notice: alert_create(Contact) |
| 28 | + else |
| 29 | + render action: :new |
45 | 30 | end |
46 | 31 | end |
47 | 32 |
|
48 | 33 | def update |
49 | | - respond_to do |format| |
50 | | - if @contact.update(contact_params) |
51 | | - format.html { render action: 'edit', notice: 'Kontakten uppdaterades!' } |
52 | | - format.json { head :no_content } |
53 | | - else |
54 | | - format.html { render action: 'edit' } |
55 | | - format.json { render json: @contact.errors, status: :unprocessable_entity } |
56 | | - end |
| 34 | + if @contact.update(contact_params) |
| 35 | + render action: :edit, notice: alert_update(Contact) |
| 36 | + else |
| 37 | + render action: :edit |
57 | 38 | end |
58 | 39 | end |
59 | 40 |
|
60 | 41 | def destroy |
61 | 42 | @contact.destroy |
62 | | - respond_to do |format| |
63 | | - format.html { redirect_to contacts_url } |
64 | | - format.json { head :no_content } |
65 | | - end |
| 43 | + redirect_to contacts_path, notice: alert_destroy(Contact) |
66 | 44 | end |
67 | 45 |
|
68 | 46 | private |
69 | | - # Use callbacks to share common setup or constraints between actions. |
70 | | - def set_contact |
71 | | - @contact = Contact.find_by_id(params[:id]) |
72 | | - if (@contact == nil) |
73 | | - redirect_to(contacts_path, notice: 'Ingen kontakt hittades.') |
74 | | - end |
| 47 | + |
| 48 | + def mail_params |
| 49 | + params.require(:contact).permit(:send_name, :send_email, :message, :copy) |
75 | 50 | end |
76 | 51 |
|
77 | | - # Never trust parameters from the scary internet, only allow the white list through. |
78 | 52 | def contact_params |
79 | 53 | params.require(:contact).permit(:name, :email, :public, :text, :council_id) |
80 | 54 | end |
|
0 commit comments