forked from AdaGold/media-ranker-revisited
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathworks_controller.rb
95 lines (81 loc) · 2.46 KB
/
works_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
class WorksController < ApplicationController
before_action :category_from_work, except: [:root, :index, :new, :create]
before_action :require_login, except:[:root]
def root
@albums = Work.best_albums
@books = Work.best_books
@movies = Work.best_movies
@best_work = Work.order(vote_count: :desc).first
end
def index
@works_by_category = Work.to_category_hash
end
def new
@work = Work.new
end
def create
@work = Work.new(media_params)
@media_category = @work.category
if @work.save
flash[:status] = :success
flash[:result_text] = "Successfully created #{@media_category.singularize} #{@work.id}"
redirect_to work_path(@work)
else
flash[:status] = :failure
flash[:result_text] = "Could not create #{@media_category.singularize}"
flash[:messages] = @work.errors.messages
render :new, status: :bad_request
end
end
def show
@votes = @work.votes.order(created_at: :desc)
end
def edit
end
def update
@work.update_attributes(media_params)
if @work.save
flash[:status] = :success
flash[:result_text] = "Successfully updated #{@media_category.singularize} #{@work.id}"
redirect_to work_path(@work)
else
flash.now[:status] = :failure
flash.now[:result_text] = "Could not update #{@media_category.singularize}"
flash.now[:messages] = @work.errors.messages
render :edit, status: :bad_request
end
end
def destroy
@work.destroy
flash[:status] = :success
flash[:result_text] = "Successfully destroyed #{@media_category.singularize} #{@work.id}"
redirect_to root_path
end
def upvote
flash[:status] = :failure
if @login_user
vote = Vote.new(user: @login_user, work: @work)
if vote.save
flash[:status] = :success
flash[:result_text] = "Successfully upvoted!"
else
flash[:result_text] = "Could not upvote"
flash[:messages] = vote.errors.messages
end
else
flash[:result_text] = "You must log in to do that"
end
# Refresh the page to show either the updated vote count
# or the error message
redirect_back fallback_location: work_path(@work)
end
private
def media_params
params.require(:work).permit(:title, :category, :creator, :description, :publication_year)
end
def category_from_work
@work = Work.find_by(id: params[:id])
render_404 unless @work
@media_category = @work.category.downcase.pluralize
end
end