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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/proclaim/posts_controller.rb', line 39

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

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

  authorize @post

  if @post.valid?
    # Save and rewrite each image in Carrierwave's cache
    @post.body = saved_and_rewrite_cached_images(@post.body)

    @post.save

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

#destroyObject

DELETE /posts/1



85
86
87
88
89
90
# File 'app/controllers/proclaim/posts_controller.rb', line 85

def destroy
  authorize @post

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

#editObject

GET /posts/1/edit



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

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



28
29
30
31
# File 'app/controllers/proclaim/posts_controller.rb', line 28

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

#showObject

GET /posts/1



17
18
19
20
21
22
23
24
25
# File 'app/controllers/proclaim/posts_controller.rb', line 17

def show
  begin
    authorize @post
  rescue Pundit::NotAuthorizedError
    # Don't leak that this resource actually exists. Turn the
    # "permission denied" into a "not found"
    raise ActiveRecord::RecordNotFound
  end
end

#updateObject

PATCH/PUT /posts/1



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

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.valid?
    # Save and rewrite each image in Carrierwave's cache
    @post.body = saved_and_rewrite_cached_images(@post.body)

    @post.save

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