Commit 801561148a761399be4a9bea6ee0d828109cdb6b

Authored by Diego Araújo
1 parent 6f209c3e
Exists in colab and in 2 other branches master, stable

Implement Repository#public_or_owned_by_user

This method is a copy and paste from other methods (of Project, for
example) and should be refactored to avoid code duplications.
app/models/repository.rb
1 1 class Repository < KalibroClient::Entities::Processor::Repository
2 2 include KalibroRecord
3 3  
  4 + def self.public_or_owned_by_user(user = nil)
  5 + repository_attributes = RepositoryAttributes.where(public: true)
  6 + repository_attributes += RepositoryAttributes.where(user_id: user.id, public: false) if user
  7 +
  8 + repository_attributes.map do |attribute|
  9 + begin
  10 + self.find(attribute.repository_id)
  11 + rescue Likeno::Errors::RecordNotFound
  12 + nil
  13 + end
  14 + end.compact
  15 + end
  16 +
4 17 def self.latest(count=1)
5 18 all.sort { |one, another| another.id <=> one.id }.first(count)
6 19 end
... ...
spec/factories/repository_attributes.rb
1 1 FactoryGirl.define do
2 2 factory :repository_attributes do
  3 + self.public true
3 4 association :user, strategy: :build
4 5 association :repository, strategy: :build
  6 +
  7 + trait :private do
  8 + self.public false
  9 + end
5 10 end
6 11 end
... ...
spec/models/repository_spec.rb
... ... @@ -20,5 +20,62 @@ describe Repository do
20 20 end
21 21 end
22 22 end
  23 +
  24 + describe 'public_or_owned_by_user' do
  25 + let!(:user) { FactoryGirl.build(:user, :with_id) }
  26 +
  27 + let!(:owned_private_attrs) { FactoryGirl.build(:repository_attributes, :private, user_id: user.id) }
  28 + let!(:owned_public_attrs) { FactoryGirl.build(:repository_attributes, user_id: user.id) }
  29 + let!(:not_owned_private_attrs) { FactoryGirl.build(:repository_attributes, :private, user_id: user.id + 1) }
  30 + let!(:not_owned_public_attrs) { FactoryGirl.build(:repository_attributes, user_id: user.id + 1) }
  31 +
  32 + let!(:public_attrs) { [owned_public_attrs, not_owned_public_attrs] }
  33 + let(:public_repositories) { public_attrs.map(&:repository) }
  34 +
  35 + let!(:owned_or_public_attrs) { public_attrs + [owned_private_attrs] }
  36 + let!(:owned_or_public_repositories) { owned_or_public_attrs.map(&:repository) }
  37 +
  38 + let(:all_repositories) { owned_or_public_repositories + [not_owned_private_attrs.repository] }
  39 +
  40 + context 'when repositories exist' do
  41 + before :each do
  42 + all_repositories.each do |repository|
  43 + described_class.stubs(:find).with(repository.id).returns(repository)
  44 + end
  45 +
  46 + RepositoryAttributes.expects(:where).with(public: true).returns(public_attrs)
  47 + end
  48 +
  49 + context 'when user is not provided' do
  50 + it 'is expected to find all public repositories' do
  51 + expect(described_class.public_or_owned_by_user).to eq(public_repositories)
  52 + end
  53 + end
  54 +
  55 + context 'when user is provided' do
  56 + before do
  57 + RepositoryAttributes.expects(:where).with(user_id: user.id, public: false).returns([owned_private_attrs])
  58 + end
  59 +
  60 + it 'is expected to find all public and owned repositories' do
  61 + expect(described_class.public_or_owned_by_user(user)).to eq(owned_or_public_repositories)
  62 + end
  63 + end
  64 + end
  65 +
  66 + context 'when no repositories exist' do
  67 + before :each do
  68 + all_repositories.each do |repository|
  69 + described_class.stubs(:find).with(repository.id).raises(Likeno::Errors::RecordNotFound)
  70 + end
  71 +
  72 + RepositoryAttributes.expects(:where).with(public: true).returns(public_attrs)
  73 + end
  74 +
  75 + it 'is expected to be empty' do
  76 + expect(described_class.public_or_owned_by_user).to be_empty
  77 + end
  78 + end
  79 + end
23 80 end
24   -end
25 81 \ No newline at end of file
  82 +end
... ...