Cleaning and refactoring

This commit is contained in:
Dmitriy Zaporozhets 2012-03-06 00:26:40 +02:00
parent 74f8bc7298
commit cb59aade4e
12 changed files with 135 additions and 232 deletions

View file

@ -31,8 +31,7 @@ class Admin::ProjectsController < ApplicationController
UsersProject.bulk_import(
@admin_project,
params[:user_ids],
params[:project_access],
params[:repo_access]
params[:project_access]
)
redirect_to [:admin, @admin_project], notice: 'Project was successfully updated.'

View file

@ -23,8 +23,7 @@ class Admin::UsersController < ApplicationController
UsersProject.user_bulk_import(
@admin_user,
params[:project_ids],
params[:project_access],
params[:repo_access]
params[:project_access]
)
redirect_to [:admin, @admin_user], notice: 'Teams were successfully updated.'

View file

@ -54,27 +54,6 @@ class Project < ActiveRecord::Base
UsersProject.access_roles
end
def repository
@repository ||= Repository.new(self)
end
delegate :repo,
:url_to_repo,
:path_to_repo,
:update_repository,
:destroy_repository,
:tags,
:repo_exists?,
:commit,
:commits,
:commits_with_refs,
:tree,
:heads,
:commits_since,
:fresh_commits,
:commits_between,
:to => :repository, :prefix => nil
def to_param
code
end
@ -213,18 +192,6 @@ class Project < ActiveRecord::Base
keys.map(&:identifier)
end
def readers
@readers ||= users_projects.includes(:user).map(&:user)
end
def writers
@writers ||= users_projects.includes(:user).map(&:user)
end
def admins
@admins ||= users_projects.includes(:user).where(:project_access => UsersProject::MASTER).map(&:user)
end
def allow_read_for?(user)
!users_projects.where(:user_id => user.id).empty?
end
@ -269,10 +236,6 @@ class Project < ActiveRecord::Base
end
end
def last_activity_date_cached(expire = 1.hour)
last_activity_date
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")
@ -293,7 +256,127 @@ class Project < ActiveRecord::Base
errors.add(:path, "Invalid repository path")
false
end
def commit(commit_id = nil)
commit = if commit_id
repo.commits(commit_id).first
else
repo.commits.first
end
Commit.new(commit) if commit
end
def fresh_commits(n = 10)
commits = heads.map do |h|
repo.commits(h.name, n).map { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0...n]
end
def commits_with_refs(n = 20)
commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0..n]
end
def commits_since(date)
commits = heads.map do |h|
repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits
end
def commits(ref, path = nil, limit = nil, offset = nil)
if path
repo.log(ref, path, :max_count => limit, :skip => offset)
elsif limit && offset
repo.commits(ref, limit, offset)
else
repo.commits(ref)
end.map{ |c| Commit.new(c) }
end
def commits_between(from, to)
repo.commits_between(from, to).map { |c| Commit.new(c) }
end
def project_id
self.id
end
def write_hooks
%w(post-receive).each do |hook|
write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook")))
end
end
def write_hook(name, content)
hook_file = File.join(path_to_repo, 'hooks', name)
File.open(hook_file, 'w') do |f|
f.write(content)
end
File.chmod(0775, hook_file)
end
def repo
@repo ||= Grit::Repo.new(path_to_repo)
end
def url_to_repo
Gitlabhq::GitHost.url_to_repo(path)
end
def path_to_repo
File.join(GIT_HOST["base_path"], "#{path}.git")
end
def update_repository
Gitlabhq::GitHost.system.update_project(path, self)
write_hooks if File.exists?(path_to_repo)
end
def destroy_repository
Gitlabhq::GitHost.system.destroy_project(self)
end
def repo_exists?
@repo_exists ||= (repo && !repo.branches.empty?)
rescue
@repo_exists = false
end
def tags
repo.tags.map(&:name).sort.reverse
end
def heads
@heads ||= repo.heads
end
def tree(fcommit, path = nil)
fcommit = commit if fcommit == :head
tree = fcommit.tree
path ? (tree / path) : tree
end
end
# == Schema Information
#
# Table name: projects

View file

@ -7,9 +7,7 @@ class ProtectedBranch < ActiveRecord::Base
after_destroy :update_repository
def update_repository
Gitlabhq::GitHost.system.new.configure do |c|
c.update_project(project.path, project)
end
Gitlabhq::GitHost.system.update_project(project.path, project)
end
def commit

View file

@ -10,135 +10,4 @@ class Repository
def self.access_options
{}
end
def initialize(project)
@project = project
end
def path
@path ||= project.path
end
def project_id
project.id
end
def write_hooks
%w(post-receive).each do |hook|
write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook")))
end
end
def write_hook(name, content)
hook_file = File.join(project.path_to_repo, 'hooks', name)
File.open(hook_file, 'w') do |f|
f.write(content)
end
File.chmod(0775, hook_file)
end
def repo
@repo ||= Grit::Repo.new(project.path_to_repo)
end
def url_to_repo
Gitlabhq::GitHost.url_to_repo(path)
end
def path_to_repo
File.join(GIT_HOST["base_path"], "#{path}.git")
end
def update_repository
Gitlabhq::GitHost.system.new.configure do |c|
c.update_project(path, project)
end
write_hooks if File.exists?(project.path_to_repo)
end
def destroy_repository
Gitlabhq::GitHost.system.new.configure do |c|
c.destroy_project(@project)
end
end
def repo_exists?
@repo_exists ||= (repo && !repo.branches.empty?)
rescue
@repo_exists = false
end
def tags
repo.tags.map(&:name).sort.reverse
end
def heads
@heads ||= repo.heads
end
def tree(fcommit, path = nil)
fcommit = commit if fcommit == :head
tree = fcommit.tree
path ? (tree / path) : tree
end
def commit(commit_id = nil)
commit = if commit_id
repo.commits(commit_id).first
else
repo.commits.first
end
Commit.new(commit) if commit
end
def fresh_commits(n = 10)
commits = heads.map do |h|
repo.commits(h.name, n).map { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0...n]
end
def commits_with_refs(n = 20)
commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0..n]
end
def commits_since(date)
commits = heads.map do |h|
repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits
end
def commits(ref, path = nil, limit = nil, offset = nil)
if path
repo.log(ref, path, :max_count => limit, :skip => offset)
elsif limit && offset
repo.commits(ref, limit, offset)
else
repo.commits(ref)
end.map{ |c| Commit.new(c) }
end
def commits_between(from, to)
repo.commits_between(from, to).map { |c| Commit.new(c) }
end
end

View file

@ -18,7 +18,7 @@ class UsersProject < ActiveRecord::Base
delegate :name, :email, :to => :user, :prefix => true
def self.bulk_import(project, user_ids, project_access, repo_access)
def self.bulk_import(project, user_ids, project_access)
UsersProject.transaction do
user_ids.each do |user_id|
users_project = UsersProject.new(
@ -31,7 +31,7 @@ class UsersProject < ActiveRecord::Base
end
end
def self.user_bulk_import(user, project_ids, project_access, repo_access)
def self.user_bulk_import(user, project_ids, project_access)
UsersProject.transaction do
project_ids.each do |project_id|
users_project = UsersProject.new(

View file

@ -6,4 +6,4 @@
= project.name
%small
last activity at
= project.last_activity_date_cached.stamp("Aug 25, 2011")
= project.last_activity_date.stamp("Aug 25, 2011")

View file

@ -1,23 +0,0 @@
- @activities.each do |update|
.wll
= link_to dashboard_feed_path(@project, update) do
- if update.kind_of? Note
%p
%strong
- if update.target
= update.target.class.name.titleize
= truncate update.target.id.to_s, :length => 10
commented
- else
Project wall
&ndash;
= image_tag gravatar_icon(update.author_email), :class => "", :width => 16
= truncate dashboard_feed_title(update), :length => 50
- else
%p
%strong
= update.class.name.titleize
= truncate update.id.to_s
&ndash;
= image_tag gravatar_icon(update.author_email), :class => "", :width => 16
= truncate dashboard_feed_title(update), :length => 50

View file

@ -1,26 +0,0 @@
%table.round-borders#projects-list
%tr
%th Name
%th Path
%th Code
%th Web
%th Git
%th Admin
%th Actions
- @projects.each do |project|
%tr{ :class => "project", :url => project_path(project) }
%td
= project.name
.small-tags= tag_list project
%td= truncate project.url_to_repo
%td= project.code
%td= check_box_tag "read", 1, project.readers.include?(current_user), :disabled => :disabled
%td= check_box_tag "commit", 1, project.writers.include?(current_user), :disabled => :disabled
%td= check_box_tag "admin", 1, project.admins.include?(current_user), :disabled => :disabled
%td
-if can? current_user, :admin_project, project
= link_to 'Edit', edit_project_path(project), :class => "lbutton positive"
%br

View file

@ -6,6 +6,14 @@ module Gitlabhq
class Gitolite
class AccessDenied < StandardError; end
def self.update_project(path, project)
self.new.configure { |git| git.update_project(path, project) }
end
def self.destroy_project(project)
self.new.configure { |git| git.destroy_project(project) }
end
def pull
# create tmp dir
@local_dir = File.join(Dir.tmpdir,"gitlabhq-gitolite-#{Time.now.to_i}")

View file

@ -90,8 +90,7 @@ def create_repo_project(project_name, user_email)
# Add user as admin for project
project.users_projects.create!(
:repo_access => Repository::REPO_RW,
:project_access => Project::PROJECT_RWA,
:project_access => UsersProject::MASTER,
:user => user
)

View file

@ -22,10 +22,7 @@ describe Project do
end
describe "Respond to" do
it { should respond_to(:readers) }
it { should respond_to(:writers) }
it { should respond_to(:repository_writers) }
it { should respond_to(:admins) }
it { should respond_to(:add_access) }
it { should respond_to(:reset_access) }
it { should respond_to(:update_repository) }