Class: Cms9::PostsController

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

Overview

posts controller

Instance Method Summary collapse

Methods inherited from ApplicationController

#authorize, #current_user, #user_logout

Instance Method Details

#createObject



21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/cms9/posts_controller.rb', line 21

def create
  @post = Post.new(post_params)
  @post.user_id = current_user.id

  if @post.save
    create_post_event(@post.id, nil)
    redirect_to posts_path(post_definition_id: @post.post_definition.id)
  else
    render :new
  end
end

#destroyObject



55
56
57
58
59
60
61
# File 'app/controllers/cms9/posts_controller.rb', line 55

def destroy
  @post = Post.find(params[:id])
  post_definition_id = @post.post_definition.id

  create_post_event(@post.id, post_name) && @post.destroy
  redirect_to posts_path(post_definition_id: post_definition_id)
end

#editObject



33
34
35
36
37
38
39
40
41
# File 'app/controllers/cms9/posts_controller.rb', line 33

def edit
  @post = Post.find(params[:id])

  @post.post_definition.post_fields.each do |post_field|
    if @post.fields.where(post_field_id: post_field[:id]).blank?
      @post.fields.build(post_field: post_field)
    end
  end
end

#indexObject



4
5
6
7
8
9
10
# File 'app/controllers/cms9/posts_controller.rb', line 4

def index
  @post_definition = Cms9::PostDefinition.find(params[:post_definition_id])
  @posts = Post.where(post_definition: @post_definition)
               .order('created_at desc')
               .page(params[:page])
               .per(20)
end

#newObject



12
13
14
15
16
17
18
19
# File 'app/controllers/cms9/posts_controller.rb', line 12

def new
  @post = Post.new
  @post.post_definition_id = params[:post_definition_id]

  @post.post_definition.post_fields.each do |post_field|
    @post.fields.build(post_field: post_field)
  end
end

#updateObject



43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/cms9/posts_controller.rb', line 43

def update
  @post = Post.find(params[:id])
  @post.user_id = current_user.id

  if @post.update(post_params)
    create_post_event(@post.id, nil)
    redirect_to posts_path(post_definition_id: @post.post_definition.id)
  else
    render :edit
  end
end