Skip to content

Commit 51fffe8

Browse files
committed
Programs now upload from Hackety.
Yeah!
1 parent 61d70aa commit 51fffe8

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

controllers/programs.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,28 @@
2121
haml :"programs/show"
2222
end
2323

24+
put "/programs/:username/:slug.json" do
25+
require_login_or_api! :username => params[:username], :password => params[:password]
26+
if current_user.username != params[:username]
27+
redirect "/"
28+
end
29+
program = Program.first(:creator_username => params[:username], :slug => params[:slug])
30+
if program.nil?
31+
program = Program.create(params)
32+
else
33+
program.update_attributes(params)
34+
program.save
35+
end
36+
37+
flash[:notice] = "Program updated!"
38+
redirect "/programs/#{program.creator_username}/#{program.slug}"
39+
end
40+
2441
put "/programs/:username/:slug" do
2542
require_login_or_api! :username => params[:username], :password => params[:password]
2643
if current_user.username != params[:username]
2744
flash[:notice] = "Sorry, buddy"
28-
redirect_to "/"
45+
redirect "/"
2946
end
3047
program = Program.first(:creator_username => params[:username], :slug => params[:slug])
3148
program.update_attributes(params[:program])

models/program.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Program
33
key :creator_username, String
44
key :title, String
55
key :slug, String
6+
key :code, String
67

78
validate_on_create :slug_check
89
before_save :make_slug

utility.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ def require_directory dirname
1414

1515
class String
1616
def to_slug
17-
self.gsub(/\s/, "_").downcase
17+
self.gsub(/\W/, "").gsub(/\s/, "_").downcase
1818
end
1919
end

views/programs/show.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
%h1= @program.title
22
%h2 By #{@program.creator_username}
33

4-
%p= @program.body
4+
%p= @program.code

0 commit comments

Comments
 (0)