Commit 4b2585504136c21a81ffce6690abb1d4ce2fafd5
1 parent
f6b35fcc
Exists in
colab
and in
2 other branches
Improve Repository#latest to return only public repositories
Showing
2 changed files
with
33 additions
and
10 deletions
Show diff stats
app/models/repository.rb
... | ... | @@ -17,7 +17,7 @@ class Repository < KalibroClient::Entities::Processor::Repository |
17 | 17 | end |
18 | 18 | |
19 | 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 | 21 | end |
22 | 22 | |
23 | 23 | def attributes | ... | ... |
spec/models/repository_spec.rb
... | ... | @@ -31,20 +31,43 @@ describe Repository do |
31 | 31 | |
32 | 32 | describe 'class method' do |
33 | 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 | 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 | 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 | 71 | end |
49 | 72 | end |
50 | 73 | end | ... | ... |