Skip to content

Commit 315fc46

Browse files
committed
user scoped programs
1 parent 7fefcbc commit 315fc46

6 files changed

Lines changed: 16 additions & 5 deletions

File tree

app/controllers/programs_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class ProgramsController < InheritedController
22
load_and_authorize_resource
3+
34
belongs_to :user, :optional => true
45

56
def show
6-
@program = Program.find_by_slug(params[:slug])
7+
# @program = Program.find_by_slug(params[:slug])
78
show!
89
end
910

app/models/program.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Program
66
key :title, String
77
key :source_code, String
88

9+
scope :by_username, lambda { |username| where(:author_username => username) }
10+
911
def to_param
1012
slug
1113
end

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class User
1212
many :questions
1313
many :answers
1414

15+
def programs
16+
Program.by_username self.username
17+
end
18+
1519
#the list of hackers this hacker is following
1620
key :following_ids, Array
1721
many :following, :in => :following_ids, :class_name => 'User'

app/views/programs/index.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ Here it is: every single program we've got. Check them out!
1919

2020
%ul
2121
- @programs.each do |program|
22-
%li= link_to program.title, program_path(program.author_username, program)
22+
%li= link_to program.title, user_program_path(User.find_by_username(program.author_username).id, program.id)

app/views/users/show.html.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
%h2= "About #{resource.username}"
1717
%h4= resource.about
1818
%hr
19+
%p
20+
= link_to "Programs", user_programs_path(resource)
1921
%p
2022
Following:
2123
=link_to resource.following.count, resource_path(resource) + "/following"

config/routes.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
resources :answers
55
end
66

7-
resources :programs, :only => [:index]
7+
resources :programs, :only => [:index, :show]
88

99
constraints(ApiConstraint) do
1010
match '/' => 'static#api_root'
@@ -25,10 +25,12 @@
2525
get "followers" => "users#followers", :as => :followers
2626
post "follow" => "users#follow", :as => :follow
2727
post "unfollow" => "users#unfollow", :as => :unfollow
28+
29+
resources :programs
2830
end
2931

30-
root :to => "static#root"
32+
# match ':user_id/:slug', :to => "programs#show", :as => :program
3133

32-
match ':user_id/:slug', :to => "programs#show", :as => :program
34+
root :to => "static#root"
3335

3436
end

0 commit comments

Comments
 (0)