Class: PostsController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#req, #res

Instance Method Summary collapse

Methods inherited from ApplicationController

#csrf_token, #initialize, #log_activity, #params, #redirect_to, #render, #render_json, #session, #validate!

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/posts_controller.rb', line 14

def create
  data = params['post']
  data['is_active'] = boolean_param(data['is_active'])
  @post = Post.new(data)
  if @post.save
    log_activity("Created post: #{@post.title}", @post, "Category: #{@post.category}")
    redirect_to '/dashboard/posts'
  else
    render 'cms/posts_form'
  end
end

#destroyObject



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

def destroy
  @post = Post[params['id']]
  title = @post.title
  delete_from_storage(@post.image_url) if @post.respond_to?(:image_url)
  delete_all_images_from_content(@post.content) if @post.respond_to?(:content)
  @post.destroy
  log_activity("Deleted post: #{title}")
  redirect_to '/dashboard/posts'
end

#editObject



26
27
28
29
# File 'app/controllers/posts_controller.rb', line 26

def edit
  @post = Post[params['id']]
  render 'cms/posts_form'
end

#indexObject



4
5
6
7
# File 'app/controllers/posts_controller.rb', line 4

def index
  @posts = Post.order(Sequel.desc(:created_at)).all
  render 'cms/posts_index'
end

#newObject



9
10
11
12
# File 'app/controllers/posts_controller.rb', line 9

def new
  @post = Post.new
  render 'cms/posts_form'
end

#updateObject



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

def update
  @post = Post[params['id']]
  data = params['post']
  data['is_active'] = boolean_param(data['is_active'])
  if @post.update(data)
    log_activity("Updated post: #{@post.title}", @post, "New Data: #{data.reject{|k| k == 'content'}.to_json}")
    redirect_to '/dashboard/posts'
  else
    render 'cms/posts_form'
  end
end