Commit 4b2585504136c21a81ffce6690abb1d4ce2fafd5

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

Improve Repository#latest to return only public repositories

app/models/repository.rb
@@ -17,7 +17,7 @@ class Repository < KalibroClient::Entities::Processor::Repository @@ -17,7 +17,7 @@ class Repository < KalibroClient::Entities::Processor::Repository
17 end 17 end
18 18
19 def self.latest(count=1) 19 def self.latest(count=1)
20 - all.sort { |one, another| another.id <=> one.id }.first(count) 20 + all.sort { |one, another| another.id <=> one.id }.select { |repository| repository.attributes.public }.first(count)
21 end 21 end
22 22
23 def attributes 23 def attributes
spec/models/repository_spec.rb
@@ -31,20 +31,43 @@ describe Repository do @@ -31,20 +31,43 @@ describe Repository do
31 31
32 describe 'class method' do 32 describe 'class method' do
33 describe 'latest' do 33 describe 'latest' do
34 - let!(:repository) { FactoryGirl.build(:repository, id: 1) }  
35 - let!(:another_repository) { FactoryGirl.build(:another_repository, id: 2) } 34 + let!(:repository) { FactoryGirl.build(:repository) }
  35 + let!(:another_repository) { FactoryGirl.build(:repository, id: 2) }
  36 + let!(:repository_attributes) { FactoryGirl.build(:repository_attributes) }
36 37
37 - before do  
38 - Repository.expects(:all).returns([repository, another_repository]) 38 + before :each do
  39 + repository.expects(:attributes).returns(repository_attributes)
  40 + another_repository.expects(:attributes).returns(repository_attributes)
39 end 41 end
40 42
41 - it 'should return the two repositorys ordered' do  
42 - expect(Repository.latest(2)).to eq([another_repository, repository]) 43 + context 'without private repositories' do
  44 + before :each do
  45 + Repository.expects(:all).returns([repository, another_repository])
  46 + end
  47 +
  48 + it 'is expected to return the two repositories ordered' do
  49 + expect(Repository.latest(2)).to eq([another_repository, repository])
  50 + end
  51 +
  52 + context 'when no parameter is passed' do
  53 + it 'is expected to return just the most recent repository' do
  54 + expect(Repository.latest).to eq([another_repository])
  55 + end
  56 + end
43 end 57 end
44 58
45 - context 'when no parameter is passed' do  
46 - it 'should return just the most recent repository' do  
47 - expect(Repository.latest).to eq([another_repository]) 59 + context 'with private repositories' do
  60 + let(:private_repository) { FactoryGirl.build(:repository, id: 3) }
  61 + let(:private_attributes) { FactoryGirl.build(:repository_attributes, :private) }
  62 +
  63 + before :each do
  64 + private_repository.expects(:attributes).returns(private_attributes)
  65 +
  66 + Repository.expects(:all).returns([repository, another_repository, private_repository])
  67 + end
  68 +
  69 + it 'is expected to return only the public ones' do
  70 + expect(Repository.latest(2)).to eq([another_repository, repository])
48 end 71 end
49 end 72 end
50 end 73 end