-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathadmin_posts_controller.rb
More file actions
153 lines (128 loc) · 4.49 KB
/
admin_posts_controller.rb
File metadata and controls
153 lines (128 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
class AdminPostsController < Admin::BaseController
before_action :admin_only, except: [:index, :show]
before_action :load_languages, except: [:show, :destroy]
before_action :load_admin_posts, only: [:index, :drafts]
# GET /admin_posts
def index
@page_subtitle = t(".page_title")
@pagy, @admin_posts = pagy(@admin_posts.posted.order(published_at: :desc))
end
# GET /admin_posts/drafts
def drafts
authorize AdminPost
@page_subtitle = t(".page_title")
@pagy, @admin_posts = pagy(@admin_posts.unposted.order(created_at: :desc))
end
# GET /admin_posts/1
def show
@admin_post = AdminPost.find(params[:id])
authorize(@admin_post) unless @admin_post.posted?
admin_posts = AdminPost.non_translated
if @admin_post.posted?
@admin_posts = admin_posts.posted.order(published_at: :desc).limit(8)
@previous_admin_post = admin_posts.posted.order(published_at: :desc).where("published_at < ?", @admin_post.published_at).first
@next_admin_post = admin_posts.posted.order(published_at: :asc).where("published_at > ?", @admin_post.published_at).first
else
@admin_posts = admin_posts.unposted.order(created_at: :desc).limit(8)
@previous_admin_post = admin_posts.unposted.order(created_at: :desc).where("created_at < ?", @admin_post.created_at).first
@next_admin_post = admin_posts.unposted.order(created_at: :asc).where("created_at > ?", @admin_post.created_at).first
end
@page_subtitle = @admin_post.title.html_safe
respond_to do |format|
format.html # show.html.erb
format.js
end
end
# GET /admin_posts/1/preview
def preview
@preview_mode = true
@admin_post = AdminPost.find(params[:id])
authorize(@admin_post)
end
# GET /admin_posts/new
# GET /admin_posts/new.xml
def new
@admin_post = AdminPost.new
authorize @admin_post
end
# GET /admin_posts/1/edit
def edit
@admin_post = AdminPost.find(params[:id])
authorize @admin_post
end
# POST /admin_posts
def create
@admin_post = AdminPost.new(admin_post_params)
@admin_post.posted = true if params[:post_button] && !@admin_post&.translated_post&.draft?
authorize @admin_post
if !params[:edit_button] && @admin_post.valid?
if params[:preview_button]
@preview_mode = true
render action: "preview" and return
elsif !params[:edit_button] && @admin_post.save
flash[:notice] = t(".success")
redirect_to(admin_post_path(@admin_post)) and return
end
end
render action: "new"
end
# PUT /admin_posts/1
def update
@admin_post = AdminPost.find(params[:id])
@admin_post.attributes = admin_post_params
@admin_post.posted = true if params[:post_button] && !@admin_post&.translated_post&.draft?
authorize @admin_post
if !params[:edit_button] && @admin_post.valid?
if params[:preview_button]
@preview_mode = true
render :preview and return
elsif @admin_post.save
flash[:notice] = t(".success")
redirect_to(@admin_post) and return
end
end
render action: "edit"
end
# PUT /admin_posts/1/post
def post
@admin_post = AdminPost.find(params[:id])
authorize @admin_post
@admin_post.posted = true
if @admin_post.save
flash[:notice] = t(".success")
redirect_to @admin_post
else
flash[:error] = t(".error")
redirect_to(edit_admin_post_path(@admin_post))
end
end
# DELETE /admin_posts/1
def destroy
@admin_post = AdminPost.find(params[:id])
authorize @admin_post
@admin_post.destroy
redirect_to(@admin_post.posted? ? admin_posts_path : drafts_admin_posts_path)
end
protected
def load_languages
@news_languages = Language.where(id: Locale.all.map(&:language_id)).default_order
end
def load_admin_posts
@tag = AdminPostTag.find_by(id: params[:tag]) if params[:tag]
@admin_posts = @tag&.admin_posts || AdminPost
if params[:language_id].present? && (@language = Language.find_by(short: params[:language_id]))
@admin_posts = @admin_posts.where(language_id: @language.id)
@tags = AdminPostTag.distinct.joins(:admin_posts).where(admin_posts: { language_id: @language.id }).order(:name)
else
@admin_posts = @admin_posts.non_translated
@tags = AdminPostTag.order(:name)
end
end
private
def admin_post_params
params.require(:admin_post).permit(
:admin_id, :title, :content, :translated_post_id, :language_id, :tag_list,
:comment_permissions, :moderated_commenting_enabled
)
end
end