Removed issues,mr delete buttons. Refactored models

This commit is contained in:
Dmitriy Zaporozhets 2012-03-04 15:35:15 +02:00
parent 3fe9d2972f
commit 0727edd8a0
13 changed files with 82 additions and 186 deletions

View file

@ -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

View file

@ -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

View file

@ -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")

View file

@ -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