forked from otwcode/otwarchive
/
chapters_controller.rb
296 lines (267 loc) · 10.7 KB
/
chapters_controller.rb
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
class ChaptersController < ApplicationController
# only registered users and NOT admin should be able to create new chapters
before_filter :users_only, :except => [ :index, :show, :destroy ]
before_filter :load_work, :except => [:index, :auto_complete_for_pseud_name, :update_positions]
before_filter :set_instance_variables, :only => [ :new, :create, :edit, :update, :preview, :post ]
# only authors of a work should be able to edit its chapters
before_filter :check_ownership, :only => [ :edit, :update, :manage, :destroy ]
before_filter :check_visibility, :only => [ :show]
before_filter :check_user_status, :only => [:new, :create, :edit, :update]
cache_sweeper :chapter_sweeper
cache_sweeper :work_sweeper
# GET /work/:work_id/chapters
# GET /work/:work_id/chapters.xml
def index
# this route is never used
redirect_to work_path(params[:work_id])
end
# GET /work/:work_id/chapters/manage
def manage
@chapters = @work.chapters_in_order(false).select(&:posted)
end
# GET /work/:work_id/chapters/:id
# GET /work/:work_id/chapters/:id.xml
def show
if params[:view_adult]
session[:adult] = true
elsif @work.adult? && !see_adult?
render "works/_adult", :layout => "application" and return
end
if params[:selected_id]
redirect_to url_for(:controller => :chapters, :action => :show, :work_id => @work.id, :id => params[:selected_id]) and return
end
@chapter = @work.chapters.find(params[:id])
@chapters = @work.chapters_in_order(false)
if !logged_in? || !current_user.is_author_of?(@work)
@chapters = @chapters.select(&:posted)
end
if !@chapters.include?(@chapter)
access_denied
else
if @chapters.length > 1
chapter_position = @chapters.index(@chapter)
@previous_chapter = @chapters[chapter_position-1] unless chapter_position == 0
@next_chapter = @chapters[chapter_position+1]
end
@commentable = @work
@comments = @chapter.comments
@page_title = @work.unrevealed? ? ts("Mystery Work - Chapter %{position}", :position => @chapter.position.to_s) :
get_page_title(@work.fandoms.string,
@work.anonymous? ? ts("Anonymous") : @work.pseuds.sort.collect(&:byline).join(', '),
@work.title + " - Chapter " + @chapter.position.to_s)
if @work.unrevealed?
@tweet_text = ts("Mystery Work")
else
@tweet_text = @work.title + " by " +
(@work.anonymous? ? ts("Anonymous") : @work.pseuds.map(&:name).join(', ')) + " - " +
(@work.fandoms.size > 2 ? ts("Multifandom") : @work.fandoms.string)
@tweet_text = @tweet_text.truncate(95)
end
if current_user.respond_to?(:subscriptions)
@subscription = current_user.subscriptions.where(:subscribable_id => @work.id,
:subscribable_type => 'Work').first ||
current_user.subscriptions.build(:subscribable => @work)
end
# TEMPORARY hack-like thing to fix the fact that chaptered works weren't hit-counted or added to history at all
if chapter_position == 0
Reading.update_or_create(@work, current_user) if current_user
Rails.logger.debug "Chapter remote addr: #{request.remote_ip}"
@work.increment_hit_count(request.remote_ip)
end
respond_to do |format|
format.html
format.js
end
end
end
# GET /work/:work_id/chapters/new
# GET /work/:work_id/chapters/new.xml
def new
end
# GET /work/:work_id/chapters/1/edit
def edit
if params["remove"] == "me"
@chapter.pseuds = @chapter.pseuds - current_user.pseuds
@chapter.save
flash[:notice] = ts("You have been removed as an author from the chapter")
redirect_to @work
end
end
# POST /work/:work_id/chapters
# POST /work/:work_id/chapters.xml
def create
@work.wip_length = params[:chapter][:wip_length]
load_pseuds
if !@chapter.invalid_pseuds.blank? || !@chapter.ambiguous_pseuds.blank?
@chapter.valid? ? (render :partial => 'choose_coauthor', :layout => 'application') : (render :new)
elsif params[:edit_button]
render :new
elsif params[:cancel_button]
redirect_back_or_default('/')
else # :preview or :cancel_coauthor_button
@work.major_version = @work.major_version + 1
if @chapter.save
# @work.update_major_version
if @chapter.published_at > @work.revised_at.to_date || @chapter.published_at == Date.today
@work.set_revised_at(@chapter.published_at)
end
if params[:post_without_preview_button]
@chapter.posted = true
if @chapter.save && @work.save
post_chapter
redirect_to [@work, @chapter]
end
elsif @work.save
flash[:notice] = ts("This is a preview of what this chapter will look like when it's posted to the Archive. You should probably read the whole thing to check for problems before posting.")
redirect_to [:preview, @work, @chapter]
else
render :new
end
else
render :new
end
end
end
# PUT /work/:work_id/chapters/1
# PUT /work/:work_id/chapters/1.xml
def update
@chapter.attributes = params[:chapter]
@work.wip_length = params[:chapter][:wip_length]
load_pseuds
if !@chapter.invalid_pseuds.blank? || !@chapter.ambiguous_pseuds.blank?
@chapter.valid? ? (render :partial => 'choose_coauthor', :layout => 'application') : (render :new)
elsif params[:preview_button] || params[:cancel_coauthor_button]
@preview_mode = true
render :preview_edit
elsif params[:cancel_button]
# Not quite working yet - should send the user back to wherever they were before they hit edit
redirect_back_or_default('/')
elsif params[:edit_button]
render :edit
else
params[:chapter][:posted] = true if params[:post_button]
@work.minor_version = @work.minor_version + 1
if @chapter.save
# @work.update_minor_version
if defined?(@previous_published_at) && @previous_published_at != @chapter.published_at #if published_at has changed
if @chapter.published_at == Date.today # if today, set revised_at to this date
@work.set_revised_at(@chapter.published_at)
else # if p_at date not today, tell model to find most recent chapter date
@work.set_revised_at
end
end
if @work.save
flash[:notice] = ts('Chapter was successfully updated.')
redirect_to [@work, @chapter]
else
render :edit
end
else
render :edit
end
end
end
def update_positions
if params[:chapters]
@work = Work.find(params[:work_id])
@work.reorder(params[:chapters])
flash[:notice] = ts("Chapter order has been successfully updated.")
elsif params[:chapter]
params[:chapter].each_with_index do |id, position|
Chapter.update(id, :position => position + 1)
(@chapters ||= []) << Chapter.find(id)
end
end
respond_to do |format|
format.html { redirect_to(@work) and return }
format.js { render :nothing => true }
end
end
# GET /chapters/1/preview
def preview
@preview_mode = true
end
# POST /chapters/1/post
def post
if params[:cancel_button]
redirect_to @work
elsif params[:edit_button]
redirect_to [:edit, @work, @chapter]
else
@chapter.posted = true
if @chapter.save && @work.save
post_chapter
redirect_to(@work)
else
render :preview
end
end
end
# DELETE /work/:work_id/chapters/1
# DELETE /work/:work_id/chapters/1.xml
def destroy
@chapter = @work.chapters.find(params[:id])
if @chapter.is_only_chapter?
flash[:error] = ts("You can't delete the only chapter in your story. If you want to delete the story, choose 'Delete work'.")
redirect_to(edit_work_url(@work))
else
if @chapter.destroy
@work.minor_version = @work.minor_version + 1
@work.set_revised_at
@work.save
flash[:notice] = ts("The chapter was successfully deleted.")
else
flash[:error] = ts("Something went wrong. Please try again.")
end
redirect_to :controller => 'works', :action => 'show', :id => @work
end
end
private
def load_pseuds
@allpseuds = (current_user.pseuds + (@work.authors ||= []) + @work.pseuds + (@chapter.authors ||= []) + (@chapter.pseuds ||= [])).uniq
@pseuds = current_user.pseuds
@coauthors = @allpseuds.select{ |p| p.user.id != current_user.id}
to_select = @chapter.authors.blank? ? @chapter.pseuds.blank? ? @work.pseuds : @chapter.pseuds : @chapter.authors
@selected_pseuds = to_select.collect {|pseud| pseud.id.to_i }
end
# fetch work these chapters belong to from db
def load_work
@work = params[:work_id] ? Work.find(params[:work_id]) : Chapter.find(params[:id]).work
@check_ownership_of = @work
@check_visibility_of = @work
end
# Sets values for @chapter, @coauthor_results, @pseuds, and @selected_pseuds
def set_instance_variables
# stuff new bylines into author attributes to be parsed by the chapter model
if params[:chapter] && params[:pseud] && params[:pseud][:byline] && params[:chapter][:author_attributes]
params[:chapter][:author_attributes][:byline] = params[:pseud][:byline]
params[:pseud][:byline] = ""
end
# stuff co-authors into author attributes too so we won't lose them
if params[:chapter] && params[:chapter][:author_attributes] && params[:chapter][:author_attributes][:coauthors]
params[:chapter][:author_attributes][:ids].concat(params[:chapter][:author_attributes][:coauthors]).uniq!
end
if params[:id] # edit, update, preview, post
@chapter = @work.chapters.find(params[:id])
@previous_published_at = @chapter.published_at
if params[:chapter] # editing, save our changes
@chapter.attributes = params[:chapter]
end
elsif params[:chapter] # create
@chapter = @work.chapters.build(params[:chapter])
else # new
@chapter = @work.chapters.build(:position => @work.number_of_chapters + 1)
end
@allpseuds = (current_user.pseuds + (@work.authors ||= []) + @work.pseuds + (@chapter.authors ||= []) + (@chapter.pseuds ||= [])).uniq
@pseuds = current_user.pseuds
@coauthors = @allpseuds.select{ |p| p.user.id != current_user.id}
to_select = @chapter.authors.blank? ? @chapter.pseuds.blank? ? @work.pseuds : @chapter.pseuds : @chapter.authors
@selected_pseuds = to_select.collect {|pseud| pseud.id.to_i }
end
def post_chapter
if !@work.posted
@work.update_attribute(:posted, true)
end
flash[:notice] = ts('Chapter has been posted!')
end
end