Commit 04b76c3a57793640818af72eb621ab7213e25002
Exists in
colab
and in
2 other branches
Merge pull request #348 from mezuro/hide_repositories
Hide repositories
Showing
9 changed files
with
163 additions
and
23 deletions
Show diff stats
app/controllers/repositories_controller.rb
| ... | ... | @@ -14,7 +14,7 @@ class RepositoriesController < ApplicationController |
| 14 | 14 | skip_before_action :verify_authenticity_token, :only => [:notify_push] |
| 15 | 15 | |
| 16 | 16 | def index |
| 17 | - @repositories = Repository.all | |
| 17 | + @repositories = Repository.public_or_owned_by_user(current_user) | |
| 18 | 18 | end |
| 19 | 19 | |
| 20 | 20 | # GET /projects/1/repositories/1 | ... | ... |
app/models/repository.rb
| 1 | 1 | class Repository < KalibroClient::Entities::Processor::Repository |
| 2 | 2 | include KalibroRecord |
| 3 | 3 | |
| 4 | + attr_writer :attributes | |
| 5 | + | |
| 6 | + def self.public_or_owned_by_user(user = nil) | |
| 7 | + repository_attributes = RepositoryAttributes.where(public: true) | |
| 8 | + repository_attributes += RepositoryAttributes.where(user_id: user.id, public: false) if user | |
| 9 | + | |
| 10 | + repository_attributes.map do |attribute| | |
| 11 | + begin | |
| 12 | + self.find(attribute.repository_id) | |
| 13 | + rescue Likeno::Errors::RecordNotFound | |
| 14 | + nil | |
| 15 | + end | |
| 16 | + end.compact | |
| 17 | + end | |
| 18 | + | |
| 4 | 19 | def self.latest(count=1) |
| 5 | - 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 | |
| 22 | + | |
| 23 | + def attributes | |
| 24 | + @attributes ||= RepositoryAttributes.find_by_repository_id(@id) | |
| 6 | 25 | end |
| 7 | 26 | end | ... | ... |
db/migrate/20160418192431_add_public_to_repository_attributes.rb
0 → 100644
db/schema.rb
| ... | ... | @@ -11,10 +11,7 @@ |
| 11 | 11 | # |
| 12 | 12 | # It's strongly recommended that you check this file into your version control system. |
| 13 | 13 | |
| 14 | -ActiveRecord::Schema.define(version: 20151106182639) do | |
| 15 | - | |
| 16 | - # These are extensions that must be enabled in order to support this database | |
| 17 | - enable_extension "plpgsql" | |
| 14 | +ActiveRecord::Schema.define(version: 20160418192431) do | |
| 18 | 15 | |
| 19 | 16 | create_table "kalibro_configuration_attributes", force: :cascade do |t| |
| 20 | 17 | t.integer "user_id" |
| ... | ... | @@ -44,11 +41,12 @@ ActiveRecord::Schema.define(version: 20151106182639) do |
| 44 | 41 | create_table "repository_attributes", force: :cascade do |t| |
| 45 | 42 | t.integer "repository_id" |
| 46 | 43 | t.integer "user_id" |
| 47 | - t.datetime "created_at", null: false | |
| 48 | - t.datetime "updated_at", null: false | |
| 44 | + t.datetime "created_at", null: false | |
| 45 | + t.datetime "updated_at", null: false | |
| 46 | + t.boolean "public", default: true | |
| 49 | 47 | end |
| 50 | 48 | |
| 51 | - add_index "repository_attributes", ["user_id"], name: "index_repository_attributes_on_user_id", using: :btree | |
| 49 | + add_index "repository_attributes", ["user_id"], name: "index_repository_attributes_on_user_id" | |
| 52 | 50 | |
| 53 | 51 | create_table "users", force: :cascade do |t| |
| 54 | 52 | t.string "name", limit: 255, default: "", null: false |
| ... | ... | @@ -66,7 +64,7 @@ ActiveRecord::Schema.define(version: 20151106182639) do |
| 66 | 64 | t.string "last_sign_in_ip", limit: 255 |
| 67 | 65 | end |
| 68 | 66 | |
| 69 | - add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree | |
| 70 | - add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree | |
| 67 | + add_index "users", ["email"], name: "index_users_on_email", unique: true | |
| 68 | + add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true | |
| 71 | 69 | |
| 72 | 70 | end | ... | ... |
features/repository/index.feature
| ... | ... | @@ -19,8 +19,9 @@ Feature: Repository listing |
| 19 | 19 | And I have a sample repository |
| 20 | 20 | And I have a sample project |
| 21 | 21 | And I have a sample repository within the sample project |
| 22 | + And I own that repository | |
| 22 | 23 | And I am at the All Repositories page |
| 23 | - Then the sample repository should be there | |
| 24 | + Then the sample repository should not be there | |
| 24 | 25 | And the project repository should be there |
| 25 | 26 | And I should not see "You must be logged in to create new Repositories." |
| 26 | 27 | ... | ... |
features/step_definitions/repository_steps.rb
| ... | ... | @@ -249,6 +249,10 @@ Then(/^the sample repository should be there$/) do |
| 249 | 249 | expect(page).to have_content(@independent_repository.description) |
| 250 | 250 | end |
| 251 | 251 | |
| 252 | +Then(/^the sample repository should not be there$/) do | |
| 253 | + expect(page).to_not have_content(@independent_repository.name) | |
| 254 | +end | |
| 255 | + | |
| 252 | 256 | Then(/^the project repository should be there$/) do |
| 253 | 257 | expect(page).to have_content(@repository.name) |
| 254 | 258 | expect(page).to have_content(@repository.description) | ... | ... |
spec/controllers/repositories_controller_spec.rb
| ... | ... | @@ -5,7 +5,7 @@ describe RepositoriesController, :type => :controller do |
| 5 | 5 | let!(:repository) { FactoryGirl.build(:repository) } |
| 6 | 6 | |
| 7 | 7 | before :each do |
| 8 | - Repository.expects(:all).returns([repository]) | |
| 8 | + Repository.expects(:public_or_owned_by_user).returns([repository]) | |
| 9 | 9 | get :index |
| 10 | 10 | end |
| 11 | 11 | ... | ... |
spec/factories/repository_attributes.rb
spec/models/repository_spec.rb
| 1 | 1 | require 'rails_helper' |
| 2 | 2 | |
| 3 | 3 | describe Repository do |
| 4 | + describe 'methods' do | |
| 5 | + describe 'attributes' do | |
| 6 | + subject { FactoryGirl.build(:repository) } | |
| 7 | + | |
| 8 | + context 'when there are attributes' do | |
| 9 | + let!(:repository_attributes) { FactoryGirl.build(:repository_attributes) } | |
| 10 | + | |
| 11 | + before :each do | |
| 12 | + RepositoryAttributes.expects(:find_by_repository_id).returns(repository_attributes) | |
| 13 | + end | |
| 14 | + | |
| 15 | + it 'is expected to return the repository attributes' do | |
| 16 | + expect(subject.attributes).to eq(repository_attributes) | |
| 17 | + end | |
| 18 | + end | |
| 19 | + | |
| 20 | + context 'when there are no attributes' do | |
| 21 | + before :each do | |
| 22 | + RepositoryAttributes.expects(:find_by_repository_id).returns(nil) | |
| 23 | + end | |
| 24 | + | |
| 25 | + it 'is expected to return the repository attributes' do | |
| 26 | + expect(subject.attributes).to be_nil | |
| 27 | + end | |
| 28 | + end | |
| 29 | + end | |
| 30 | + end | |
| 31 | + | |
| 4 | 32 | describe 'class method' do |
| 5 | 33 | describe 'latest' do |
| 6 | - let!(:repository) { FactoryGirl.build(:repository, id: 1) } | |
| 7 | - 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) } | |
| 37 | + | |
| 38 | + before :each do | |
| 39 | + repository.expects(:attributes).returns(repository_attributes) | |
| 40 | + another_repository.expects(:attributes).returns(repository_attributes) | |
| 41 | + end | |
| 42 | + | |
| 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 | |
| 57 | + end | |
| 58 | + | |
| 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) | |
| 8 | 65 | |
| 9 | - before do | |
| 10 | - Repository.expects(:all).returns([repository, another_repository]) | |
| 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]) | |
| 71 | + end | |
| 11 | 72 | end |
| 73 | + end | |
| 74 | + | |
| 75 | + describe 'public_or_owned_by_user' do | |
| 76 | + let!(:user) { FactoryGirl.build(:user, :with_id) } | |
| 77 | + | |
| 78 | + let!(:owned_private_attrs) { FactoryGirl.build(:repository_attributes, :private, user_id: user.id) } | |
| 79 | + let!(:owned_public_attrs) { FactoryGirl.build(:repository_attributes, user_id: user.id) } | |
| 80 | + let!(:not_owned_private_attrs) { FactoryGirl.build(:repository_attributes, :private, user_id: user.id + 1) } | |
| 81 | + let!(:not_owned_public_attrs) { FactoryGirl.build(:repository_attributes, user_id: user.id + 1) } | |
| 82 | + | |
| 83 | + let!(:public_attrs) { [owned_public_attrs, not_owned_public_attrs] } | |
| 84 | + let(:public_repositories) { public_attrs.map(&:repository) } | |
| 85 | + | |
| 86 | + let!(:owned_or_public_attrs) { public_attrs + [owned_private_attrs] } | |
| 87 | + let!(:owned_or_public_repositories) { owned_or_public_attrs.map(&:repository) } | |
| 12 | 88 | |
| 13 | - it 'should return the two repositorys ordered' do | |
| 14 | - expect(Repository.latest(2)).to eq([another_repository, repository]) | |
| 89 | + let(:all_repositories) { owned_or_public_repositories + [not_owned_private_attrs.repository] } | |
| 90 | + | |
| 91 | + context 'when repositories exist' do | |
| 92 | + before :each do | |
| 93 | + all_repositories.each do |repository| | |
| 94 | + described_class.stubs(:find).with(repository.id).returns(repository) | |
| 95 | + end | |
| 96 | + | |
| 97 | + RepositoryAttributes.expects(:where).with(public: true).returns(public_attrs) | |
| 98 | + end | |
| 99 | + | |
| 100 | + context 'when user is not provided' do | |
| 101 | + it 'is expected to find all public repositories' do | |
| 102 | + expect(described_class.public_or_owned_by_user).to eq(public_repositories) | |
| 103 | + end | |
| 104 | + end | |
| 105 | + | |
| 106 | + context 'when user is provided' do | |
| 107 | + before do | |
| 108 | + RepositoryAttributes.expects(:where).with(user_id: user.id, public: false).returns([owned_private_attrs]) | |
| 109 | + end | |
| 110 | + | |
| 111 | + it 'is expected to find all public and owned repositories' do | |
| 112 | + expect(described_class.public_or_owned_by_user(user)).to eq(owned_or_public_repositories) | |
| 113 | + end | |
| 114 | + end | |
| 15 | 115 | end |
| 16 | 116 | |
| 17 | - context 'when no parameter is passed' do | |
| 18 | - it 'should return just the most recent repository' do | |
| 19 | - expect(Repository.latest).to eq([another_repository]) | |
| 117 | + context 'when no repositories exist' do | |
| 118 | + before :each do | |
| 119 | + all_repositories.each do |repository| | |
| 120 | + described_class.stubs(:find).with(repository.id).raises(Likeno::Errors::RecordNotFound) | |
| 121 | + end | |
| 122 | + | |
| 123 | + RepositoryAttributes.expects(:where).with(public: true).returns(public_attrs) | |
| 124 | + end | |
| 125 | + | |
| 126 | + it 'is expected to be empty' do | |
| 127 | + expect(described_class.public_or_owned_by_user).to be_empty | |
| 20 | 128 | end |
| 21 | 129 | end |
| 22 | 130 | end |
| 23 | 131 | end |
| 24 | -end | |
| 25 | 132 | \ No newline at end of file |
| 133 | +end | ... | ... |