Class: Back::PostsController

Inherits:
BackController
  • Object
show all
Defined in:
app/controllers/lato_blog/back/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

This function creates a new post.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 49

def create
  @post = LatoBlog::Post.new(new_post_params)

  unless @post.save
    flash[:danger] = @post.errors.full_messages.to_sentence
    redirect_to lato_blog.new_post_path
    return
  end

  flash[:success] = LANGUAGES[:lato_blog][:flashes][:post_create_success]
  redirect_to lato_blog.post_path(@post.id)
end

#destroyObject

This function destroyes a post.



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 175

def destroy
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  unless @post.destroy
    flash[:danger] = @post.post_parent.errors.full_messages.to_sentence
    redirect_to lato_blog.edit_post_path(@post.id)
    return
  end

  flash[:success] = LANGUAGES[:lato_blog][:flashes][:post_destroy_success]
  redirect_to lato_blog.posts_path(status: 'deleted')
end

#destroy_all_deletedObject

Tis function destroyes all posts with status deleted.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 190

def destroy_all_deleted
  @posts = LatoBlog::Post.deleted

  if !@posts || @posts.empty?
    flash[:warning] = LANGUAGES[:lato_blog][:flashes][:deleted_posts_not_found]
    redirect_to lato_blog.posts_path(status: 'deleted')
    return
  end

  @posts.each do |post|
    unless post.destroy
      flash[:danger] = post.errors.full_messages.to_sentence
      redirect_to lato_blog.edit_post_path(post.id)
      return
    end
  end

  flash[:success] = LANGUAGES[:lato_blog][:flashes][:deleted_posts_destroy_success]
  redirect_to lato_blog.posts_path(status: 'deleted')
end

#editObject

This function show the view to edit a post.



63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 63

def edit
  core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:posts_edit])
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  if @post.meta_language != cookies[:lato_blog__current_language]
    set_current_language @post.meta_language
  end

  fetch_external_objects
end

#indexObject

This function shows the list of published posts.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 9

def index
  core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:posts])
  # find correct status to show
  @posts_status = 'published'
  @posts_status = 'drafted' if params[:status] && params[:status] === 'drafted'
  @posts_status = 'deleted' if params[:status] && params[:status] === 'deleted'
  # find informations data
  @posts_informations = {
    published_length: LatoBlog::Post.published.where(meta_language: cookies[:lato_blog__current_language]).length,
    drafted_length: LatoBlog::Post.drafted.where(meta_language: cookies[:lato_blog__current_language]).length,
    deleted_length: LatoBlog::Post.deleted.where(meta_language: cookies[:lato_blog__current_language]).length
  }
  # find posts to show
  @posts = LatoBlog::Post.where(meta_status: @posts_status,
  meta_language: cookies[:lato_blog__current_language]).joins(:post_parent).order('lato_blog_post_parents.publication_datetime DESC')

  @widget_index_posts = core__widgets_index(@posts, search: 'title', pagination: 10)
end

#newObject

This function shows the view to create a new post.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 35

def new
  core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:posts_new])
  @post = LatoBlog::Post.new

  set_current_language params[:language] if params[:language]

  if params[:parent]
    @post_parent = LatoBlog::PostParent.find_by(id: params[:parent])
  end

  fetch_external_objects
end

#showObject

This function shows a single post. It create a redirect to the edit path.



29
30
31
32
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 29

def show
  # use edit as default post show page
  redirect_to lato_blog.edit_post_path(params[:id])
end

#updateObject

This function updates a post.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 76

def update
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  # update for autosaving
  autosaving = params[:autosave] && params[:autosave] == 'true'
  if autosaving
    @post.update(edit_post_params)
    update_fields
    render status: 200, json: {} # render something positive :)
    return
  end

  # check post data update
  unless @post.update(edit_post_params)
    flash[:danger] = @post.errors.full_messages.to_sentence
    redirect_to lato_blog.edit_post_path(@post.id)
    return
  end

  # update single fields
  unless update_fields
    flash[:warning] = LANGUAGES[:lato_blog][:flashes][:post_update_fields_warning]
    redirect_to lato_blog.edit_post_path(@post.id)
    return
  end

  # render positive response
  flash[:success] = LANGUAGES[:lato_blog][:flashes][:post_update_success]
  redirect_to lato_blog.post_path(@post.id)
end

#update_categoriesObject

This function updates the categories of a post.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 125

def update_categories
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  params[:categories].each do |category_id, value|
    category = LatoBlog::Category.find_by(id: category_id)
    next if !category || category.meta_language != @post.meta_language

    category_post = LatoBlog::CategoryPost.find_by(lato_blog_post_id: @post.id, lato_blog_category_id: category.id)
    if value == 'true'
      LatoBlog::CategoryPost.create(lato_blog_post_id: @post.id, lato_blog_category_id: category.id) unless category_post
    else
      category_post.destroy if category_post
    end
  end
end

#update_publication_datetimeObject

This function updates the publication datetime of a post (update the post parent).



117
118
119
120
121
122
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 117

def update_publication_datetime
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  @post.post_parent.update(publication_datetime: params[:publication_datetime])
end

#update_seo_descriptionObject

This function updates the seo description of a post.



167
168
169
170
171
172
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 167

def update_seo_description
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  @post.update(seo_description: params[:seo_description])
end

#update_statusObject

This function updates the status of a post.



109
110
111
112
113
114
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 109

def update_status
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  @post.update(meta_status: params[:status])
end

#update_tagsObject

This function updates the tags of a post.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/controllers/lato_blog/back/posts_controller.rb', line 143

def update_tags
  @post = LatoBlog::Post.find_by(id: params[:id])
  return unless check_post_presence

  params_tags = params[:tags].map(&:to_i)
  tag_posts = LatoBlog::TagPost.where(lato_blog_post_id: @post.id)

  params_tags.each do |tag_id|
    tag = LatoBlog::Tag.find_by(id: tag_id)
    next if !tag || tag.meta_language != @post.meta_language

    tag_post = tag_posts.find_by(lato_blog_tag_id: tag.id)
    LatoBlog::TagPost.create(lato_blog_post_id: @post.id, lato_blog_tag_id: tag.id) unless tag_post
  end

  tag_ids = tag_posts.pluck(:lato_blog_tag_id)
  tag_ids.each do |tag_id|
    next if params_tags.include?(tag_id)
    tag_post = tag_posts.find_by(lato_blog_tag_id: tag_id)
    tag_post.destroy if tag_post
  end
end