Commit d7012537dc1b518beab24128e2cf32f8587ed9bb

add a project_id to comments and the beginnings of a dashboard for logged in users

Commit diff

app/controllers/comments_controller.rb

 
2525 def create
2626 @comment = @repository.comments.new(params[:comment])
2727 @comment.user = current_user
28 @comment.project = @project
2829 respond_to do |format|
2930 if @comment.save
3031 format.html do
toggle raw diff

app/controllers/site_controller.rb

 
11class SiteController < ApplicationController
2 before_filter :login_required, :only => [:dashboard]
23
34 def index
45 @tags = Project.tag_counts
56 @projects = Project.find(:all, :limit => 5, :order => "id desc")
67 end
78
9 def dashboard
10 @projects = current_user.projects
11 project_ids = @projects.map(&:id)
12 @recent_comments = Comment.find(:all, :limit => 10,
13 :conditions => ["project_id in (?)", project_ids], :order => "created_at desc")
14 @repository_clones = @projects.map(&:repository_clones).flatten
15 # @repository_clones = Repository.find(:all,
16 # :conditions => ["project_id in (?) and mainline = ?", project_ids, false])
17 end
18
819 def about
920 end
1021
toggle raw diff

app/models/comment.rb

 
11class Comment < ActiveRecord::Base
22 belongs_to :user
33 belongs_to :repository
4 belongs_to :project
45
56 attr_protected :user_id
67
7 validates_presence_of :user_id, :repository_id, :body
8 validates_presence_of :user_id, :repository_id, :body, :project_id
89
910
1011end
toggle raw diff

app/models/project.rb

 
22 acts_as_taggable
33
44 belongs_to :user
5 has_many :comments
56 has_many :repositories, :order => "mainline desc, created_at asc",
67 :dependent => :destroy
78 has_one :mainline_repository, :conditions => ["mainline = ?", true],
89 :class_name => "Repository"
9 has_many :branch_repositories, :conditions => ["mainline = ?", false],
10 has_many :repository_clones, :conditions => ["mainline = ?", false],
1011 :class_name => "Repository"
1112
1213 URL_FORMAT_RE = /^(http|https|nntp):\/\//.freeze
toggle raw diff

app/views/layouts/application.html.erb

 
1313 <%= GitoriousConfig["extra_html_head_data"] -%>
1414</head>
1515
16<body>
16<body id="<%= controller.controller_name -%>">
1717 <div id="container">
1818 <div id="header">
1919 <div class="login-logout">
3434 <div id="menu">
3535 <ul>
3636 <li><%= link_to "Home", root_path -%></li>
37 <% if logged_in? -%>
38 <li><%= link_to "Dashboard", dashboard_path -%></li>
39 <% end -%>
3740 <li><%= link_to "Projects", projects_path -%></li>
3841 <li><%= link_to "About", about_path -%></li>
3942 </ul>
toggle raw diff

app/views/site/dashboard.html.erb

 
1<% @page_title = "#{current_user.login}'s dashboard" -%>
2
3<div id="recent_comments">
4 <h2>Recent comments on your commits</h2>
5 <% @recent_comments.each do |comment| -%>
6 <div class="comment">
7 <p class="body"><%= sanitize(comment.body) -%></p>
8 <p class="byline hint">
9 on <%= link_to h(comment.repository.name), project_repository_comments_path(comment.project, comment.repository, :anchor => dom_id(comment)) -%>
10 <% unless comment.sha1.blank? -%>
11 (<%= link_to h(comment.sha1[0..6]), project_repository_commit_path(comment.project, comment.repository, comment.sha1) -%>)
12 <% end -%>
13 in <%= link_to h(comment.project.title), comment.project -%> by
14 <%= link_to(h(comment.user.login), comment.user) -%>
15 <%= time_ago_in_words(comment.created_at) -%> ago
16 </p>
17 </div>
18 <% end -%>
19 <% if @recent_comments.blank? -%>
20 <p class="hint">Sorry, no comments yet</p>
21 <% end -%>
22</div>
23
24<div id="clone_list">
25 <h2>Clones of your project repositories</h2>
26 <ul>
27 <% @repository_clones.each do |repos| -%>
28 <li>
29 <%= link_to h(repos.name), project_repository_path(@project, repos) -%>
30 <small>of <%= h(repos.project.slug) -%></small>
31 </li>
32 <% end -%>
33 <% if @repository_clones.blank? -%>
34 <li class="hint">No clones so far</li>
35 <% end -%>
36 </ul>
37</div>
38
39<div class="clear"></div>
40
41<% content_for :sidebar do -%>
42 <h4>Your projects:</h4>
43 <ul class="links">
44 <% @projects.each do |project| -%>
45 <li><%= link_to project.slug, project -%></li>
46 <% end -%>
47 <li><%= link_to "Create new &#x2192;", new_project_path -%></li>
48 </ul>
49<% end -%>
toggle raw diff

config/routes.rb

 
6262 session.login '/login', :action => 'new'
6363 session.logout '/logout', :action => 'destroy'
6464 end
65
65
66 map.dashboard "dashboard", :controller => "site", :action => "dashboard"
6667 map.about "about", :controller => "site", :action => "about"
6768
6869 # Install the default route as the lowest priority.
toggle raw diff

db/migrate/020_add_project_id_to_comments.rb

 
1class AddProjectIdToComments < ActiveRecord::Migration
2 def self.up
3 add_column :comments, :project_id, :integer
4 add_index :comments, :project_id
5 ActiveRecord::Base::reset_column_information
6
7 Comment.find(:all).each do |comment|
8 comment.update_attributes(:project_id => comment.repository.project_id)
9 end
10 end
11
12 def self.down
13 remove_column :comments, :project_id
14 end
15end
toggle raw diff

db/schema.rb

 
99#
1010# It's strongly recommended to check this file into your version control system.
1111
12ActiveRecord::Schema.define(:version => 19) do
12ActiveRecord::Schema.define(:version => 20) do
1313
1414 create_table "comments", :force => true do |t|
1515 t.integer "user_id", :null => false
1818 t.text "body"
1919 t.datetime "created_at"
2020 t.datetime "updated_at"
21 t.integer "project_id"
2122 end
2223
2324 add_index "comments", ["user_id"], :name => "index_comments_on_user_id"
2425 add_index "comments", ["repository_id"], :name => "index_comments_on_repository_id"
2526 add_index "comments", ["sha1"], :name => "index_comments_on_sha1"
27 add_index "comments", ["project_id"], :name => "index_comments_on_project_id"
2628
2729 create_table "committerships", :force => true do |t|
2830 t.integer "user_id"
toggle raw diff

public/stylesheets/base.css

 
730730#site_overview p.create_account_hint {
731731 margin-top: 1.5em;
732732}
733
734
735#site #recent_comments {
736 width: 48%;
737 float: left;
738 padding-right: 10px;
739 border-right: 1px dotted #dedede;
740}
741
742#site #recent_comments .comment .body {
743 font-size: 90%;
744}
745
746#site #clone_list {
747 width: 49%;
748 float: right;
749}
toggle raw diff

spec/controllers/site_controller_spec.rb

 
22
33describe SiteController do
44
5 describe "#index" do
6
5 describe "#index" do
76 it "GETs sucessfully" do
87 get :index
98 response.should be_success
1818 assigns[:projects].should == Project.find(:all, :limit => 5, :order => "id desc")
1919 end
2020 end
21
22 describe "#dashboard" do
23 before(:each) do
24 login_as :johan
25 end
26 it "GETs successfully" do
27 get :dashboard
28 response.should be_success
29 response.should render_template("site/dashboard")
30 end
31
32 it "requires login" do
33 login_as nil
34 get :dashboard
35 response.should redirect_to(new_sessions_path)
36 end
37
38 it "get a list of the current_users projects" do
39 get :dashboard
40 assigns[:projects].should == [*projects(:johans)]
41 end
42
43 it "gets a list of recent comments from users projects" do
44 get :dashboard
45 assigns[:recent_comments].should == comments(:johans_repos, :johans_repos2)
46 end
47
48 it "gets a list of all the clones made of current_users repositories" do
49 get :dashboard
50 assigns[:repository_clones].should == [*repositories(:johans2)]
51 end
52 end
2153
2254end
toggle raw diff

spec/fixtures/comments.yml

 
66 repository_id: 1
77 sha1: 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 # foo
88 body: a comment
9 project_id: 1
910
1011johans_repos2:
1112 id: 2
1414 repository_id: 1
1515 sha1: 62cdb7020ff920e5aa642c3d4066950dd1f01f4d # bar
1616 body: another comment
17 project_id: 1
1718
1819moes_repos:
1920 id: 3
2222 repository_id: 2
2323 sha1: 62cdb7020ff920e5aa642c3d4066950dd1f01f4d # bar
2424 body: a comment in moes repos
25 project_id: 2
toggle raw diff

spec/fixtures/repositories.yml

 
1111 id: 2
1212 name: Johansprojectrepos-clone
1313 project_id: 1
14 user_id: 1
14 user_id: 2
1515 parent_id: 1
1616 created_at: 2007-11-18 01:21:21
1717 updated_at: 2007-11-18 01:21:21
toggle raw diff

spec/models/comment_spec.rb

 
99 c = Comment.new({
1010 :repository => repositories(:johans),
1111 :sha1 => Digest::SHA1.hexdigest("baz"),
12 :body => "blabla"
12 :body => "blabla",
13 :project => projects(:johans)
1314 }.merge(opts))
1415 c.user = opts[:user] || users(:johan)
1516 c
3838 @comment.should have(1).error_on(:body)
3939 end
4040
41 it "should belong to a project to be valid" do
42 @comment.project_id = nil
43 @comment.should_not be_valid
44 @comment.should have(1).error_on(:project_id)
45 end
46
4147 # it "should have a sha1 to be valid" do
4248 # @comment.sha1 = nil
4349 # @comment.should_not be_valid
toggle raw diff