diff --git a/app/controllers/merge_requests_controller.rb b/app/controllers/merge_requests_controller.rb index cfb9e6dcbb7..1cf75876426 100644 --- a/app/controllers/merge_requests_controller.rb +++ b/app/controllers/merge_requests_controller.rb @@ -30,7 +30,7 @@ class MergeRequestsController < ApplicationController else @merge_requests.opened end - @merge_requests = @merge_requests.includes(:author, :project) + @merge_requests = @merge_requests.includes(:author, :project).order("created_at desc") end def show diff --git a/app/models/key.rb b/app/models/key.rb index c1bcc348f12..1d7aae359f4 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -14,8 +14,6 @@ class Key < ActiveRecord::Base before_save :set_identifier before_validation :strip_white_space - after_save :update_repository - after_destroy :repository_delete_key delegate :name, :email, :to => :user, :prefix => true validate :unique_key diff --git a/app/models/key_observer.rb b/app/models/key_observer.rb new file mode 100644 index 00000000000..fac53a67925 --- /dev/null +++ b/app/models/key_observer.rb @@ -0,0 +1,9 @@ +class KeyObserver < ActiveRecord::Observer + def after_save(key) + key.update_repository + end + + def after_destroy(key) + key.repository_delete_key + end +end diff --git a/app/models/project.rb b/app/models/project.rb index 7a99ee6f7ad..32b244597f3 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -3,19 +3,17 @@ require "grit" class Project < ActiveRecord::Base belongs_to :owner, :class_name => "User" - has_many :events, :dependent => :destroy + has_many :users, :through => :users_projects + has_many :events, :dependent => :destroy has_many :merge_requests, :dependent => :destroy - has_many :issues, :dependent => :destroy, :order => "position" + has_many :issues, :dependent => :destroy, :order => "position" has_many :users_projects, :dependent => :destroy - has_many :users, :through => :users_projects - has_many :notes, :dependent => :destroy - has_many :snippets, :dependent => :destroy - has_many :deploy_keys, :dependent => :destroy, :foreign_key => "project_id", :class_name => "Key" - has_many :web_hooks, :dependent => :destroy + has_many :notes, :dependent => :destroy + has_many :snippets, :dependent => :destroy + has_many :deploy_keys, :dependent => :destroy, :foreign_key => "project_id", :class_name => "Key" + has_many :web_hooks, :dependent => :destroy + has_many :wikis, :dependent => :destroy has_many :protected_branches, :dependent => :destroy - has_many :wikis, :dependent => :destroy - - acts_as_taggable validates :name, :uniqueness => true, @@ -39,15 +37,10 @@ class Project < ActiveRecord::Base :message => "only letters, digits & '_' '-' '.' allowed" }, :length => { :within => 3..255 } - validates :owner, - :presence => true - + validates :owner, :presence => true validate :check_limit validate :repo_name - after_destroy :destroy_repository - after_save :update_repository - attr_protected :private_flag, :owner_id scope :public_only, where(:private_flag => false) @@ -163,18 +156,6 @@ class Project < ActiveRecord::Base users_projects.find_by_user_id(user_id) end - def fresh_merge_requests(n) - merge_requests.includes(:project, :author).order("created_at desc").first(n) - end - - def fresh_issues(n) - issues.includes(:project, :author).order("created_at desc").first(n) - end - - def fresh_notes(n) - notes.inc_author_project.order("created_at desc").first(n) - end - def common_notes notes.where(:noteable_type => ["", nil]).inc_author_project end @@ -277,9 +258,7 @@ class Project < ActiveRecord::Base end def last_activity - events.last - rescue - nil + events.last || nil end def last_activity_date @@ -294,43 +273,6 @@ class Project < ActiveRecord::Base last_activity_date end - # Get project updates from cache - # or calculate. - def cached_updates(limit, expire = 2.minutes) - activities_key = "project_#{id}_activities" - cached_activities = Rails.cache.read(activities_key) - if cached_activities - activities = cached_activities - else - activities = updates(limit) - Rails.cache.write(activities_key, activities, :expires_in => expire) - end - - activities - end - - # Get 20 events for project like - # commits, issues or notes - def updates(n = 3) - [ - fresh_commits(n), - fresh_issues(n), - fresh_notes(n) - ].compact.flatten.sort do |x, y| - y.created_at <=> x.created_at - end[0...n] - end - - def activities(n=3) - [ - fresh_issues(n), - fresh_merge_requests(n), - notes.inc_author_project.where("noteable_type is not null").order("created_at desc").first(n) - ].compact.flatten.sort do |x, y| - y.created_at <=> x.created_at - end[0...n] - end - def check_limit unless owner.can_create_project? errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") diff --git a/app/models/project_observer.rb b/app/models/project_observer.rb new file mode 100644 index 00000000000..135959ab738 --- /dev/null +++ b/app/models/project_observer.rb @@ -0,0 +1,9 @@ +class ProjectObserver < ActiveRecord::Observer + def after_save(project) + project.update_repository + end + + def after_destroy(project) + project.destroy_repository + end +end diff --git a/app/views/issues/_form.html.haml b/app/views/issues/_form.html.haml index 3b379198aed..1635590bf1f 100644 --- a/app/views/issues/_form.html.haml +++ b/app/views/issues/_form.html.haml @@ -8,6 +8,10 @@ - @issue.errors.full_messages.each do |msg| %li= msg + .clearfix + = f.label :title + .input= f.text_area :title, :maxlength => 255, :class => "xxlarge" + .clearfix = f.label :assignee_id .input= f.select(:assignee_id, @project.users.all.collect {|p| [ p.name, p.id ] }, { :include_blank => "Select user" }) @@ -21,9 +25,6 @@ = f.label :closed .input= f.check_box :closed - .clearfix - = f.label :title - .input= f.text_area :title, :maxlength => 255, :class => "xxlarge" .actions = f.submit 'Save', :class => "primary btn" @@ -34,8 +35,3 @@ = link_to "Cancel", project_issues_path(@project), :class => "btn" - else = link_to "Cancel", project_issue_path(@project, @issue), :class => "btn" - - - - unless @issue.new_record? - .right - = link_to 'Remove', [@project, @issue], :confirm => 'Are you sure?', :method => :delete, :class => "danger btn" diff --git a/app/views/issues/_show.html.haml b/app/views/issues/_show.html.haml index 9d86a5ff01b..b8031e929b6 100644 --- a/app/views/issues/_show.html.haml +++ b/app/views/issues/_show.html.haml @@ -6,8 +6,6 @@ - else = link_to 'Resolve', project_issue_path(issue.project, issue, :issue => {:closed => true }, :status_only => true), :method => :put, :class => "success btn small", :remote => true = link_to 'Edit', edit_project_issue_path(issue.project, issue), :class => "btn small edit-issue-link", :remote => true - -#- if can?(current_user, :admin_issue, @project) || issue.author == current_user - = link_to 'Remove', [issue.project, issue], :confirm => 'Are you sure?', :method => :delete, :remote => true, :class => "danger btn small delete-issue", :id => "destroy_issue_#{issue.id}" = image_tag gravatar_icon(issue.assignee_email), :class => "avatar" %span.update-author assigned to diff --git a/app/views/merge_requests/_form.html.haml b/app/views/merge_requests/_form.html.haml index cab517dc3d5..c0440e07275 100644 --- a/app/views/merge_requests/_form.html.haml +++ b/app/views/merge_requests/_form.html.haml @@ -5,6 +5,9 @@ - @merge_request.errors.full_messages.each do |msg| %li= msg + .clearfix + = f.label :title + .input= f.text_area :title, :class => "xxlarge", :maxlength => 255, :rows => 5 .clearfix = f.label :source_branch, "From" .input= f.select(:source_branch, @project.heads.map(&:name), { :include_blank => "Select branch" }, :style => "width:250px") @@ -15,9 +18,6 @@ = f.label :assignee_id, "Assign to" .input= f.select(:assignee_id, @project.users.all.collect {|p| [ p.name, p.id ] }, { :include_blank => "Select user" }, :style => "width:250px") - .clearfix - = f.label :title - .input= f.text_area :title, :class => "xlarge", :maxlength => 255, :rows => 5 .actions = f.submit 'Save', :class => "primary btn" - if @merge_request.new_record? @@ -26,11 +26,6 @@ - else = link_to project_merge_request_path(@project, @merge_request), :class => "btn" do Cancel -   - - unless @merge_request.new_record? - .right - = link_to 'Remove', [@project, @merge_request], :confirm => 'Are you sure?', :method => :delete, :class => "btn danger" - diff --git a/app/views/projects/index.html.haml b/app/views/projects/index.html.haml index e70b9f0d8d7..20f4f510cdd 100644 --- a/app/views/projects/index.html.haml +++ b/app/views/projects/index.html.haml @@ -1,25 +1,21 @@ -- unless @projects.empty? - .row - .span4 - %div.leftbar.ui-box - %h5 - Projects - - if current_user.can_create_project? - %span.right - = link_to new_project_path, :class => "btn very_small info" do - New Project - .content_list - - @projects.each do |project| - = link_to project_path(project), :remote => true, :class => dom_class(project) do - %h4 - %span.ico.project - = truncate(project.name, :length => 22) - .span12.right - .show_holder.ui-box.padded - .loading - -- else - %h2 Nothing here +.row + .span4 + %div.leftbar.ui-box + %h5 + Projects + - if current_user.can_create_project? + %span.right + = link_to new_project_path, :class => "btn very_small info" do + New Project + .content_list + - @projects.each do |project| + = link_to project_path(project), :remote => true, :class => dom_class(project) do + %h4 + %span.ico.project + = truncate(project.name, :length => 22) + .span12.right + .show_holder.ui-box.padded + .loading :javascript diff --git a/config/application.rb b/config/application.rb index a3ef29c0db2..9b3dd636e55 100644 --- a/config/application.rb +++ b/config/application.rb @@ -23,7 +23,7 @@ module Gitlab # config.plugins = [ :exception_notification, :ssl_requirement, :all ] # Activate observers that should always be running. - config.active_record.observers = :mailer_observer, :activity_observer + config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. diff --git a/db/schema.rb b/db/schema.rb index 1bb5f709dcc..8838b2191fb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -24,25 +24,13 @@ ActiveRecord::Schema.define(:version => 20120301185805) do t.integer "action" end - create_table "features", :force => true do |t| - t.string "name" - t.string "branch_name" - t.integer "assignee_id" - t.integer "author_id" - t.integer "project_id" - t.datetime "created_at" - t.datetime "updated_at" - t.string "version" - t.integer "status", :default => 0, :null => false - end - create_table "issues", :force => true do |t| t.string "title" t.integer "assignee_id" t.integer "author_id" t.integer "project_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.boolean "closed", :default => false, :null => false t.integer "position", :default => 0 t.boolean "critical", :default => false, :null => false @@ -53,8 +41,8 @@ ActiveRecord::Schema.define(:version => 20120301185805) do create_table "keys", :force => true do |t| t.integer "user_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.text "key" t.string "title" t.string "identifier" @@ -69,8 +57,8 @@ ActiveRecord::Schema.define(:version => 20120301185805) do t.integer "assignee_id" t.string "title" t.boolean "closed", :default => false, :null => false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "merge_requests", ["project_id"], :name => "index_merge_requests_on_project_id" @@ -80,8 +68,8 @@ ActiveRecord::Schema.define(:version => 20120301185805) do t.string "noteable_id" t.string "noteable_type" t.integer "author_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "project_id" t.string "attachment" t.string "line_code" @@ -94,8 +82,8 @@ ActiveRecord::Schema.define(:version => 20120301185805) do t.string "name" t.string "path" t.text "description" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.boolean "private_flag", :default => true, :null => false t.string "code" t.integer "owner_id" @@ -118,8 +106,8 @@ ActiveRecord::Schema.define(:version => 20120301185805) do t.text "content" t.integer "author_id", :null => false t.integer "project_id", :null => false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "file_name" t.datetime "expires_at" end @@ -152,8 +140,8 @@ ActiveRecord::Schema.define(:version => 20120301185805) do t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "name" t.boolean "admin", :default => false, :null => false t.integer "projects_limit", :default => 10 @@ -171,16 +159,16 @@ ActiveRecord::Schema.define(:version => 20120301185805) do create_table "users_projects", :force => true do |t| t.integer "user_id", :null => false t.integer "project_id", :null => false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "project_access", :default => 0, :null => false end create_table "web_hooks", :force => true do |t| t.string "url" t.integer "project_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "wikis", :force => true do |t| diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 28a500ebed6..30aed0959e4 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2,13 +2,17 @@ require 'spec_helper' describe Project do describe "Associations" do - it { should have_many(:events) } it { should have_many(:users) } - it { should have_many(:users_projects) } - it { should have_many(:issues) } - it { should have_many(:notes) } - it { should have_many(:snippets) } + it { should have_many(:protected_branches).dependent(:destroy) } + it { should have_many(:events).dependent(:destroy) } + it { should have_many(:wikis).dependent(:destroy) } + it { should have_many(:merge_requests).dependent(:destroy) } + it { should have_many(:users_projects).dependent(:destroy) } + it { should have_many(:issues).dependent(:destroy) } + it { should have_many(:notes).dependent(:destroy) } + it { should have_many(:snippets).dependent(:destroy) } it { should have_many(:web_hooks).dependent(:destroy) } + it { should have_many(:deploy_keys).dependent(:destroy) } end describe "Validation" do @@ -70,30 +74,6 @@ describe Project do end end - describe "updates" do - let(:project) { Factory :project } - - before do - @issue = Factory :issue, - :project => project, - :author => Factory(:user), - :assignee => Factory(:user) - - @note = Factory :note, - :project => project, - :author => Factory(:user) - - @commit = project.fresh_commits(1).first - end - - describe "return commit, note & issue" do - it { project.updates(3).count.should == 3 } - it { project.updates(3).last.id.should == @commit.id } - it { project.updates(3).include?(@issue).should be_true } - it { project.updates(3).include?(@note).should be_true } - end - end - describe "last_activity" do let(:project) { Factory :project } diff --git a/spec/requests/issues_spec.rb b/spec/requests/issues_spec.rb index bff99531ee7..f3f38fd9646 100644 --- a/spec/requests/issues_spec.rb +++ b/spec/requests/issues_spec.rb @@ -46,21 +46,6 @@ describe "Issues" do page.body.should have_selector("entry summary", :text => @issue.title) end - describe "Destroy" do - before do - # admin access to remove issue - @user.users_projects.destroy_all - project.add_access(@user, :read, :write, :admin) - visit edit_project_issue_path(project, @issue) - end - - it "should remove entry" do - expect { - click_link "Remove" - }.to change { Issue.count }.by(-1) - end - end - describe "statuses" do before do @closed_issue = Factory :issue,