-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore.rb
More file actions
158 lines (142 loc) · 4.14 KB
/
core.rb
File metadata and controls
158 lines (142 loc) · 4.14 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
154
155
156
157
158
Bundler.require
require_relative './models/user'
require_relative './models/work'
require_relative './models/tag'
class Profistory
class Core < Sinatra::Base
register Sinatra::RespondWith
register Config
Mongoid.load!("config/mongoid.yml")
def current_user
@current_user ||= User.where(uid: session[:uid]).first
end
def title
title = Settings.title.dup
title << " > #{params[:user_name]}" if params[:user_name]
title << " > #{CGI.unescape(params[:title])}" if params[:title]
title
end
def allowed_to_edit?(work, user)
work.users.find(user) rescue nil
end
def gravatar_icon(user, size=nil)
url = "//gravatar.com/avatar/#{Digest::MD5.hexdigest(user.uid)}"
url += "?size=#{size}" if size
url
end
def create_work
attributes = {
title: CGI.unescape(params[:title]),
tag_list: params[:tags],
description: params[:description],
links_text: params[:links_text],
date: params[:date]
}
if params[:old_title] && (@work = current_user.works.where(:title => CGI.unescape(params[:old_title])).first)
if !allowed_to_edit?(@work, current_user)
halt 403
end
@work.update_attributes(attributes)
else
@work = current_user.works.create(attributes)
end
save = @work.save
respond_to do |f|
f.html do
if save
redirect to("works/#{@work.title_escaped}")
else
haml :edit_work
end
end
f.json do
if save
jbuilder :show_work
else
{errors: @work.errors.to_json}
end
end
end
end
def show_work
@work = Work.where(:title => CGI.unescape(params[:title])).first
respond_to do |f|
f.html { haml :show_work }
f.json { jbuilder :show_work }
end
end
def join_work
@work = Work.where(:title => CGI.unescape(params[:title])).first
@work.users.push(current_user)
respond_to do |f|
f.html { redirect to("works/#{@work.title_escaped}") }
f.json { jbuilder :show_work }
end
end
def leave_work
@work = Work.where(:title => CGI.unescape(params[:title])).first
@work.users.delete(current_user)
respond_to do |f|
f.html { redirect to("works/#{@work.title_escaped}") }
f.json { jbuilder :show_work }
end
end
def list_works
@works = Work.desc(:date)
@years = @works.map {|work| work.date.year }.uniq.sort.reverse
respond_to do |f|
f.html { haml :list_works }
f.json { jbuilder :list_works }
end
end
def list_users
@users = User.order_by(:uid.asc)
@atoz = @users.map {|user| user.name[0].upcase }.uniq.sort
haml :list_users
respond_to do |f|
f.html { haml :list_users }
f.json { jbuilder :list_users }
end
end
def update_user
@user = User.where(:name => params[:user_name]).first
@user.update_attributes!(
tag_list: params[:tags]
)
respond_to do |f|
f.html { redirect to("users/#{params[:user_name]}") }
f.json { jbuilder :show_user }
end
end
def show_user
@user = User.where(:name => params[:user_name]).first
@works = @user.works.desc(:date)
@years = @user.works.map {|work| work.date.year }.uniq.sort.reverse
respond_to do |f|
f.html { haml :show_user }
f.json { jbuilder :show_user }
end
end
def list_tags
@tags = Tag.all
@max_count = @tags.map {|tag| tag[:count] }.max
@min_count = @tags.map {|tag| tag[:count] }.min
@tags.each do |tag|
weight = tag[:count].to_f / (@max_count - @min_count)
tag[:size] = (weight * 5).round
end
respond_to do |f|
f.html { haml :list_tags }
f.json { jbuilder :list_tags }
end
end
def show_tag
@users = User.tagged_with(params[:name])
@works = Work.tagged_with(params[:name])
respond_to do |f|
f.html { haml :show_tag }
f.json { jbuilder :show_tag }
end
end
end
end