Class: Proclaim::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/proclaim/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /posts



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/proclaim/posts_controller.rb', line 45

def create
  @post = Post.new(post_params)
  @post.author = current_author

  if params[:publish] == "true"
    @post.publish
  end

  authorize @post

  if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
  else
    render :new
  end
end

#destroyObject

DELETE /posts/1



81
82
83
84
85
86
# File 'app/controllers/proclaim/posts_controller.rb', line 81

def destroy
  authorize @post

  @post.destroy
  redirect_to posts_url, notice: 'Post was successfully destroyed.'
end

#editObject

GET /posts/1/edit



40
41
42
# File 'app/controllers/proclaim/posts_controller.rb', line 40

def edit
  authorize @post
end

#indexObject

GET /posts



11
12
13
14
# File 'app/controllers/proclaim/posts_controller.rb', line 11

def index
  @posts = policy_scope(Post).order(published_at: :desc, updated_at: :desc)
  authorize Post
end

#newObject

GET /posts/new



34
35
36
37
# File 'app/controllers/proclaim/posts_controller.rb', line 34

def new
  @post = Post.new(author: current_author)
  authorize @post
end

#showObject

GET /posts/1



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/proclaim/posts_controller.rb', line 17

def show
  authorize @post

  # Don't track traffic if it's a logged-in user
  unless current_author
    @post.punch(request)
  end

  # If an old id or a numeric id was used to find the record, then
  # the request path will not match the post_path, and we should do
  # a 301 redirect that uses the current friendly id.
  if request.path != post_path(@post)
    return redirect_to @post, status: :moved_permanently
  end
end

#updateObject

PATCH/PUT /posts/1



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/proclaim/posts_controller.rb', line 63

def update
  @post.assign_attributes(post_params)

  if (params[:publish] == "true") and not @post.published?
    @post.publish
    @post.author = current_author # Reassign author when it's published
  end

  authorize @post

  if @post.save
    redirect_to @post, notice: 'Post was successfully updated.'
  else
    render :edit
  end
end