Issue #4 fixed duplication of Relations when creating personal

repository
This commit is contained in:
George Vinogradov 2011-12-08 18:03:56 +04:00
parent 26689f3bd2
commit 3e41e6fb42
4 changed files with 24 additions and 5 deletions

View File

@ -23,7 +23,7 @@ class Group < ActiveRecord::Base
after_create :add_owner_to_members
include Modules::Models::PersonalRepository
include Modules::Models::Owner
# include Modules::Models::Owner
protected
def add_owner_to_members

View File

@ -5,6 +5,7 @@ class Relation < ActiveRecord::Base
ROLES = %w[reader writer admin]
validates :role, :inclusion => {:in => ROLES}
# validate { errors.add(:object, :taken) if Relation.where(:object_type => self.object_type, :object_id => self.object_id).present? }
before_validation :add_default_role
scope :by_object, lambda {|obj| {:conditions => ['object_id = ? AND object_type = ?', obj.id, obj.class.to_s]}}

View File

@ -62,9 +62,11 @@ class Repository < ActiveRecord::Base
def add_admin_relations
platform.relations.where(:role => 'admin').each do |rel|
r = relations.build(:role => 'admin', :object_type => rel.object_type)
r.object_id = rel.object_id
r.save
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
end
end

View File

@ -1,5 +1,21 @@
require 'spec_helper'
describe Repository do
pending "add some examples to (or delete) #{__FILE__}"
context 'when create with same owner that platform' do
before (:each) do
@platform = Factory(:platform)
@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
end
end
#pending "add some examples to (or delete) #{__FILE__}"
end