Class: Boss::PostsController

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

Instance Method Summary collapse

Instance Method Details

#contentObject



63
64
65
66
67
68
69
70
# File 'app/controllers/boss/posts_controller.rb', line 63

def content
  @post = Boss::Post.find params[:id]
  @content = @post.body
  
  respond_to do |format|
    format.json { render json: @content.to_json }
  end
end

#editObject



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

def edit
  @post = Boss::Post.find params[:id]
  @categories = Boss::Category.all
  session[:post_id] = params[:id]
end

#indexObject



16
17
18
# File 'app/controllers/boss/posts_controller.rb', line 16

def index
  @posts = Boss::Post.posts_for_index
end

#loadObject



72
73
74
75
76
77
78
79
80
# File 'app/controllers/boss/posts_controller.rb', line 72

def load
  @posts = Boss::Post.posts_for_index({ starts_at: params[:starts_at] })
  
  html_str = render_to_string( partial: "boss/posts/post", collection: @posts)
      
  respond_to do |format|
    format.json { render json: { html: html_str, has_more: !@posts.empty?, new_start: (@posts.last) ? @posts.last.id : nil } }
  end
end

#newObject



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

def new
  @post = Boss::Post.new
  @categories = Boss::Category.all
  session.delete :post_id
end

#publishObject



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

def publish
  if params[:id]
    @post = Boss::Post.find params[:id]
    saved = @post.update_attributes draft: false
  else
    if session[:post_id]
      @post = Boss::Post.find session[:post_id]
      saved = @post.update_attributes body: params[:data], title: params[:title], category_id: params[:category_id], draft: false
      session.delete :post_id
    else
      @post = Boss::Post.create body: params[:data], title: params[:title], category_id: params[:category_id], draft: false
      saved = (@post.id) ? true : false
    end
  end

  if saved
    flash[:notice] = t('posts.flash.published_post', title: @post.title)
  else
    flash[:error] = t('posts.flash.failed_to_published_post')
  end

  redirect_to (params[:id]) ? boss.admin_posts_path : boss.posts_path
end

#saveObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/boss/posts_controller.rb', line 20

def save
  if session[:post_id]
    @post = Boss::Post.find session[:post_id]
    saved = @post.update_attributes body: params[:data], category_id: params[:category_id], title: params[:title]
  else
    @post = Boss::Post.create body: params[:data], title: params[:title], category_id: params[:category_id], draft: true
    saved = (@post.id) ? true : false
    session[:post_id] = @post.id if saved
  end

  @response = (saved) ? 
    { flash_message: t('posts.flash.saved_draft'), flash: "notice", redirect: posts_path } : 
    { flash_message: t('posts.flash.failed_to_save'), flash: "error" }

  respond_to do |format|
    format.json { render json: @response.to_json }
  end
end

#showObject



82
83
84
# File 'app/controllers/boss/posts_controller.rb', line 82

def show
  @post = Boss::Post.find params[:id]
end