Delegate all repo abilities to platform. Remove repository owner and relations. Fix templates and specs. Refactor and code cleanup. Refs #205

This commit is contained in:
Pavel Chipiga 2012-02-21 01:13:05 +02:00
parent f6f7d1fb87
commit ed60751dfa
25 changed files with 77 additions and 131 deletions

View File

@ -12,7 +12,7 @@ class PersonalRepositoriesController < ApplicationController
else
@projects = @repository.projects.recent.paginate :page => params[:project_page], :per_page => 30
end
@user = @repository.owner
@user = @repository.platform.owner
@urpmi_commands = @repository.platform.urpmi_list(request.host)
end

View File

@ -42,7 +42,6 @@ class RepositoriesController < ApplicationController
def create
@repository = Repository.new(params[:repository])
@repository.platform_id = params[:platform_id]
@repository.owner = get_owner
if @repository.save
flash[:notice] = t('flash.repository.saved')
redirect_to @repositories_path

View File

@ -66,18 +66,16 @@ class Ability
can :read, Platform, :owner_type => 'User', :owner_id => user.id
can :read, Platform, :owner_type => 'Group', :owner_id => user.group_ids
can(:read, Platform, read_relations_for('platforms')) {|platform| local_reader? platform}
can(:update, Platform) {|platform| local_admin? platform}
can([:update, :build_all], Platform) {|platform| local_admin? platform}
can([:freeze, :unfreeze, :destroy], Platform) {|platform| owner? platform}
can :autocomplete_user_uname, Platform
# TODO delegate to platform?
can :read, Repository, :platform => {:visibility => 'open'}
can :read, Repository, :owner_type => 'User', :owner_id => user.id
can :read, Repository, :owner_type => 'Group', :owner_id => user.group_ids
can(:read, Repository, read_relations_for('repositories')) {|repository| local_reader? repository}
can(:create, Repository) {|repository| local_admin? repository.platform}
can([:update, :add_project, :remove_project], Repository) {|repository| local_admin? repository}
can([:change_visibility, :settings, :destroy], Repository) {|repository| owner? repository}
can :read, Repository, :platform => {:owner_type => 'User', :owner_id => user.id}
can :read, Repository, :platform => {:owner_type => 'Group', :owner_id => user.group_ids}
can(:read, Repository, read_relations_for('repositories', 'platforms')) {|repository| local_reader? repository.platform}
can([:create, :update, :projects_list, :add_project, :remove_project], Repository) {|repository| local_admin? repository.platform}
can([:change_visibility, :settings, :destroy], Repository) {|repository| owner? repository.platform}
can :read, Product, :platform => {:owner_type => 'User', :owner_id => user.id}
can :read, Product, :platform => {:owner_type => 'Group', :owner_id => user.group_ids}

View File

@ -2,16 +2,16 @@
class Group < ActiveRecord::Base
belongs_to :owner, :class_name => 'User'
has_many :own_projects, :as => :owner, :class_name => 'Project'
has_many :relations, :as => :object, :dependent => :destroy
has_many :objects, :as => :target, :class_name => 'Relation'
has_many :targets, :as => :object, :class_name => 'Relation'
has_many :members, :through => :objects, :source => :object, :source_type => 'User', :autosave => true
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true, :dependent => :destroy
has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true
has_many :relations, :as => :object, :dependent => :destroy
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true
has_many :own_projects, :as => :owner, :class_name => 'Project', :dependent => :destroy
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
validates :name, :owner, :presence => true
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ }
@ -26,11 +26,12 @@ class Group < ActiveRecord::Base
after_initialize lambda {|r| r.name ||= r.uname } # default
include Modules::Models::PersonalRepository
# include Modules::Models::Owner
# include Modules::Models::Owner
protected
def add_owner_to_members
Relation.create_with_role(self.owner, self, 'admin')
# members << self.owner if !members.exists?(:id => self.owner.id)
end
def add_owner_to_members
Relation.create_with_role(self.owner, self, 'admin')
# members << self.owner if !members.exists?(:id => self.owner.id)
end
end

View File

@ -22,7 +22,8 @@ class Relation < ActiveRecord::Base
end
protected
def add_default_role
self.role = ROLES.first if role.nil? || role.empty?
end
def add_default_role
self.role = ROLES.first if role.nil? || role.empty?
end
end

View File

@ -1,27 +1,19 @@
# -*- encoding : utf-8 -*-
class Repository < ActiveRecord::Base
belongs_to :platform
belongs_to :owner, :polymorphic => true
has_many :projects, :through => :project_to_repositories #, :dependent => :destroy
has_many :project_to_repositories, :validate => true, :dependent => :destroy
has_many :relations, :as => :target, :dependent => :destroy
has_many :objects, :as => :target, :class_name => 'Relation', :dependent => :destroy
has_many :members, :through => :objects, :source => :object, :source_type => 'User'
has_many :groups, :through => :objects, :source => :object, :source_type => 'Group'
validates :description, :uniqueness => {:scope => :platform_id}, :presence => true
validates :name, :uniqueness => {:scope => :platform_id, :case_sensitive => false}, :presence => true, :format => { :with => /^[a-z0-9_\-]+$/ }
# validates :platform_id, :presence => true # if you uncomment this platform clone will not work
scope :recent, order("name ASC")
before_create :xml_rpc_create, :unless => lambda {Thread.current[:skip]}
before_destroy :xml_rpc_destroy
after_create :add_admin_relations
attr_accessible :description, :name #, :platform_id
attr_accessible :description, :name
def full_clone(attrs) # owner
clone.tap do |c| # dup
@ -31,8 +23,6 @@ class Repository < ActiveRecord::Base
end
end
include Modules::Models::Owner
class << self
def build_stub(platform)
rep = Repository.new
@ -43,31 +33,21 @@ class Repository < ActiveRecord::Base
protected
def xml_rpc_create
result = BuildServer.create_repo name, platform.name
if result == BuildServer::SUCCESS
return true
else
raise "Failed to create repository #{name} inside platform #{platform.name} with code #{result}."
end
def xml_rpc_create
result = BuildServer.create_repo name, platform.name
if result == BuildServer::SUCCESS
return true
else
raise "Failed to create repository #{name} inside platform #{platform.name} with code #{result}."
end
end
def xml_rpc_destroy
result = BuildServer.delete_repo name, platform.name
if result == BuildServer::SUCCESS
return true
else
raise "Failed to delete repository #{name} inside platform #{platform.name} with code #{result}."
end
end
def add_admin_relations
platform.relations.where(:role => 'admin').each do |rel|
if !relations.exists?(:role => 'admin', :object_type => rel.object_type, :object_id => rel.object_id) && rel.object != owner
r = relations.build(:role => 'admin', :object_type => rel.object_type)
r.object_id = rel.object_id
r.save
end
end
def xml_rpc_destroy
result = BuildServer.delete_repo name, platform.name
if result == BuildServer::SUCCESS
return true
else
raise "Failed to delete repository #{name} inside platform #{platform.name} with code #{result}."
end
end
end

View File

@ -11,22 +11,19 @@ class User < ActiveRecord::Base
has_many :authentications, :dependent => :destroy
has_many :build_lists, :dependent => :destroy
has_many :subscribes, :foreign_key => :user_id, :dependent => :destroy
has_many :comments, :dependent => :destroy
has_many :relations, :as => :object, :dependent => :destroy
has_many :targets, :as => :object, :class_name => 'Relation'
has_many :own_projects, :as => :owner, :class_name => 'Project', :dependent => :destroy
has_many :own_groups, :foreign_key => :owner_id, :class_name => 'Group'
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
has_many :own_repositories, :as => :owner, :class_name => 'Repository', :dependent => :destroy
has_many :groups, :through => :targets, :source => :target, :source_type => 'Group', :autosave => true
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
has_many :groups, :through => :targets, :source => :target, :source_type => 'Group', :autosave => true
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true
has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true
has_many :subscribes, :foreign_key => :user_id, :dependent => :destroy
has_many :comments, :dependent => :destroy
has_many :own_projects, :as => :owner, :class_name => 'Project', :dependent => :destroy
has_many :own_groups, :foreign_key => :owner_id, :class_name => 'Group', :dependent => :destroy
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
include Modules::Models::PersonalRepository

View File

@ -20,21 +20,11 @@
= t("activerecord.attributes.repository.platform")
\:
= link_to @repository.platform.name, url_for(@repository.platform)
%p
%b
= t("activerecord.attributes.repository.owner")
\:
= link_to @repository.owner.name, url_for(@repository.owner)
%p
%b
= t("activerecord.attributes.platform.visibility")
\:
= @repository.platform.visibility
%p
%b
= t("activerecord.attributes.repository.platform")
\:
= link_to @repository.platform.name, platform_path(@platform)
.wat-cf
= link_to image_tag("web-app-theme/icons/cross.png", :alt => t("layout.delete")) + " " + t("layout.delete"), @repository_path, :method => "delete", :class => "button", :confirm => t("layout.repositories.confirm_delete")

View File

@ -6,12 +6,6 @@
%li= link_to t("layout.personal_repositories.private_users"), platform_private_users_path(@repository.platform) if @repository.platform.hidden?
.content
.inner
%p
%b
= t("activerecord.attributes.repository.owner")
\:
= link_to @repository.owner.name, url_for(@repository.owner)
= render 'shared/urpmi_list', :urpmi_commands => @urpmi_commands
.wat-cf
=# link_to image_tag("web-app-theme/icons/cross.png", :alt => t("layout.delete")) + " " + t("layout.delete"), @repository_path, :method => "delete", :class => "button", :confirm => t("layout.repositories.confirm_delete")

View File

@ -21,11 +21,6 @@
= t("activerecord.attributes.repository.platform")
\:
= link_to @repository.platform.description, url_for(@repository.platform)
%p
%b
= t("activerecord.attributes.repository.owner")
\:
= link_to @repository.owner.uname, url_for(@repository.owner)
.wat-cf
= link_to image_tag("web-app-theme/icons/cross.png", :alt => t("layout.delete")) + " " + t("layout.delete"), @repository_path, :method => "delete", :class => "button", :confirm => t("layout.repositories.confirm_delete")

View File

@ -21,11 +21,6 @@
= t("activerecord.attributes.repository.platform")
\:
= link_to @repository.platform.description, url_for(@repository.platform)
%p
%b
= t("activerecord.attributes.repository.owner")
\:
= link_to @repository.owner.try(:name), url_for(@repository.owner)
.wat-cf
= link_to image_tag("web-app-theme/icons/cross.png", :alt => t("layout.delete")) + " " + t("layout.delete"), @repository_path, :method => "delete", :class => "button", :confirm => t("layout.repositories.confirm_delete") if can? :destroy, @repository

View File

@ -0,0 +1,12 @@
class RemoveRepositoriesOwner < ActiveRecord::Migration
def self.up
remove_column :repositories, :owner_id
remove_column :repositories, :owner_type
Relation.delete_all(:target_type => 'Repository')
end
def self.down
add_column :repositories, :owner_id, :integer
add_column :repositories, :owner_type, :string
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120210141153) do
ActiveRecord::Schema.define(:version => 20120220185458) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
@ -294,8 +294,6 @@ ActiveRecord::Schema.define(:version => 20120210141153) do
t.datetime "created_at"
t.datetime "updated_at"
t.string "name", :null => false
t.integer "owner_id"
t.string "owner_type"
end
create_table "rpms", :force => true do |t|

View File

@ -20,7 +20,6 @@ module Modules
pl.save!
rep = pl.repositories.build
rep.owner = pl.owner
rep.name = 'main'
rep.description = 'main'
rep.save!

View File

@ -37,6 +37,7 @@ end
describe CollaboratorsController do
before(:each) do
stub_rsync_methods
@project = Factory(:project)
@another_user = Factory(:user)
@update_params = {:user => {:read => {@another_user.id => '1'}}}

View File

@ -3,6 +3,7 @@ require 'spec_helper'
describe GroupsController do
before(:each) do
stub_rsync_methods
@group = Factory(:group)
@another_user = Factory(:user)
@create_params = {:group => {:name => 'grp1', :uname => 'un_grp1'}}

View File

@ -3,6 +3,7 @@ require 'spec_helper'
describe MembersController do
before(:each) do
stub_rsync_methods
@group = Factory(:group)
@user = @group.owner
set_session_for @user

View File

@ -92,9 +92,6 @@ describe PersonalRepositoriesController do
@project.update_attribute(:owner, @user)
@repository.update_attribute(:owner, @user)
@repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin')
@repository.platform.update_attribute(:owner, @user)
@repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin')
end
@ -108,7 +105,7 @@ describe PersonalRepositoriesController do
before(:each) do
@user = Factory(:user)
set_session_for(@user)
@repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader')
@repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader')
end
it_should_behave_like 'personal repository viewer'

View File

@ -79,8 +79,8 @@ describe RepositoriesController do
before(:each) do
@user = Factory(:user)
set_session_for(@user)
@repository.update_attribute(:owner, @user)
@repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin')
@repository.platform.update_attribute(:owner, @user)
@repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin')
end
it_should_behave_like 'repository user with owner rights'
@ -90,7 +90,7 @@ describe RepositoriesController do
before(:each) do
@user = Factory(:user)
set_session_for(@user)
@repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader')
@repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader')
end
it_should_behave_like 'repository user with reader rights'

View File

@ -3,18 +3,12 @@ Factory.define(:repository) do |p|
p.description { Factory.next(:string) }
p.name { Factory.next(:unixname) }
p.association :platform, :factory => :platform
p.association :owner, :factory => :user
end
Factory.define(:personal_repository, :class => Repository) do |p|
p.description { Factory.next(:string) }
p.name { Factory.next(:unixname) }
p.association :platform, :factory => :platform
p.association :owner, :factory => :user
p.after_create { |rep|
rep.platform.platform_type = 'personal'
rep.platform.visibility = 'hidden'
rep.platform.save!
Factory.define(:personal_repository, :parent => :repository) do |p|
p.after_create {|r|
r.platform.platform_type = 'personal'
r.platform.visibility = 'hidden'
r.platform.save!
}
end

View File

@ -278,24 +278,19 @@ describe CanCan do
context 'with owner rights' do
before(:each) do
@repository.update_attribute(:owner, @user)
@repository.platform.update_attribute(:owner, @user)
end
[:read, :update, :destroy, :add_project, :remove_project, :change_visibility, :settings].each do |action|
[:read, :create, :update, :destroy, :add_project, :remove_project, :change_visibility, :settings].each do |action|
it "should be able to #{action} repository" do
@ability.should be_able_to(action, @repository)
end
end
it do
@repository.platform.update_attribute(:owner, @user)
@ability.should be_able_to(:create, @repository)
end
end
context 'with read rights' do
before(:each) do
@repository.relations.create!(:object_id => @user.id, :object_type => 'User', :role => 'reader')
@repository.platform.relations.create!(:object_id => @user.id, :object_type => 'User', :role => 'reader')
end
it "should be able to read repository" do

View File

@ -21,6 +21,7 @@ def set_comments_data_for_commit
end
describe Comment do
before { stub_rsync_methods }
context 'for global admin user' do
before(:each) do
@user = Factory(:admin)

View File

@ -15,6 +15,7 @@ def set_commentable_data
end
describe Comment do
before { stub_rsync_methods }
context 'for global admin user' do
before(:each) do
@user = Factory(:admin)

View File

@ -10,14 +10,9 @@ describe Repository do
@params = {:name => 'tst_platform', :description => 'test platform'}
end
it 'it should increase Relations.count by 1' do
rep = Repository.new(@params)
rep.platform = @platform
rep.owner = @platform.owner
rep.save!
Relation.by_object(rep.owner).by_target(rep).count.should eql(1)
# (@platform.owner.repositories.where(:platform_id => @platform.id).count == 1).should be_true
it 'it should increase Repository.count by 1' do
rep = Repository.create(@params) {|r| r.platform = @platform}
@platform.repositories.count.should eql(1)
end
end
#pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -12,6 +12,7 @@ def set_testable_data
end
describe Subscribe do
before { stub_rsync_methods }
context 'for global admin user' do
before(:each) do
@user = Factory(:admin)