blob: 1ff310d86e7942ace974b5d8c907b42846fd298b (
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
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
77
78
|
class BlogpostsController < ApplicationController
before_filter :set_post, except: [:index, :new, :create]
before_filter :auth, except: [:index, :show]
def index
@posts = Blogpost.order(id: :desc).page(params[:page]).per(10)
end
def show
@comment = Comment.new(blogpost: @post)
@comments = @post.comments.page(params[:page])
end
def new
@post = Blogpost.new
end
def edit
end
def create
@post = Blogpost.new(post_params)
@post.user_author = current_user
if @post.save
@post.send_new_mention_mail
redirect_to @post, notice: 'Post has been created.'
else
flash[:alert] = "Error creating blogpost"
render action: "new"
end
end
def update
@post.user_editor = current_user
@post.attributes = post_params([:user_editor])
old_content = @post.content_was
if @post.save
@post.send_new_mention_mail(old_content)
redirect_to @post, notice: 'Post has been updated.'
else
flash[:alert] = "There was a problem while updating the post"
render action: "edit"
end
end
def destroy
if @post.destroy
flash[:notice] = "Post deleted!"
else
flash[:alert] = "There was a problem while deleting this Post"
end
redirect_to blogposts_path
end
private
def post_params(add = [])
a = [:title, :content]
a += add
params.require(:blogpost).permit(a)
end
def set_post
if params[:id]
@post = Blogpost.find(params[:id])
end
end
def auth
unless mod?
flash[:alert] = "You are not allowed to edit posts!"
redirect_to @post ? @post : blogposts_path
end
end
end
|