Skip to content

Commit

Permalink
feat(prod pacts in index): allow all tags to be shown on index
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Nov 11, 2017
1 parent af5c919 commit 0a1f0ee
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 7 deletions.
12 changes: 11 additions & 1 deletion lib/pact_broker/index/service.rb
Expand Up @@ -14,10 +14,20 @@ class Service
def self.find_index_items options = {}
pact_repository
.find_latest_pacts
.collect { | pact| build_index_item_rows(pact, options[:tags] || []) }
.collect { | pact| build_index_item_rows(pact, tags_for(pact, options)) }
.flatten
end

def self.tags_for(pact, options)
if options[:tags] == true
tag_service.find_all_tag_names_for_pacticipant(pact.consumer_name)
elsif options[:tags].is_a?(Array)
options[:tags]
else
[]
end
end

def self.build_index_item_rows(pact, tags)
index_items = [build_latest_pact_index_item(pact, tags)]
tags.each do | tag |
Expand Down
12 changes: 11 additions & 1 deletion lib/pact_broker/tags/repository.rb
Expand Up @@ -15,7 +15,7 @@ def create args
def find args
PactBroker::Domain::Tag
.select_all_qualified
.join(:versions, {id: :version_id})
.join(:versions, { id: :version_id })
.join(:pacticipants, {Sequel.qualify("pacticipants", "id") => Sequel.qualify("versions", "pacticipant_id")})
.where(name_like(Sequel.qualify("tags", "name"), args.fetch(:tag_name)))
.where(name_like(Sequel.qualify("versions", "number"), args.fetch(:pacticipant_version_number)))
Expand All @@ -26,6 +26,16 @@ def find args
def delete_by_version_id version_id
Sequel::Model.db[:tags].where(version_id: version_id).delete
end

def find_all_tag_names_for_pacticipant pacticipant_name
PactBroker::Domain::Tag
.select(Sequel[:tags][:name])
.join(:versions, { Sequel[:versions][:id] => Sequel[:tags][:version_id] })
.join(:pacticipants, { Sequel[:pacticipants][:id] => Sequel[:versions][:pacticipant_id] })
.where(Sequel[:pacticipants][:name] => pacticipant_name)
.distinct
.collect{ |tag| tag[:name] }.sort
end
end
end
end
3 changes: 3 additions & 0 deletions lib/pact_broker/tags/service.rb
Expand Up @@ -25,6 +25,9 @@ def delete args
connection.run("delete from tags where name = '#{args.fetch(:tag_name)}' and version_id = '#{version.id}'")
end

def find_all_tag_names_for_pacticipant pacticipant_name
tag_repository.find_all_tag_names_for_pacticipant pacticipant_name
end
end
end
end
4 changes: 2 additions & 2 deletions lib/pact_broker/ui/controllers/index.rb
Expand Up @@ -10,9 +10,9 @@ class Index < Base
include PactBroker::Services

get "/" do
tags = [*params[:tags]].compact
tags = params[:tags] == 'true' ? true : [*params[:tags]].compact
view_model = ViewDomain::IndexItems.new(index_service.find_index_items(tags: tags))
page = tags.any? ? :'index/show-with-tags' : :'index/show'
page = tags == true || tags.any? ? :'index/show-with-tags' : :'index/show'
haml page, {locals: {index_items: view_model, title: "Pacts"}, layout: :'layouts/main'}
end

Expand Down
24 changes: 23 additions & 1 deletion spec/lib/pact_broker/tags/repository_spec.rb
Expand Up @@ -5,6 +5,8 @@ module PactBroker
module Tags
describe Repository do

let(:td) { TestDataBuilder.new }

describe ".find" do

let(:pacticipant_name) { "test_pacticipant" }
Expand Down Expand Up @@ -80,9 +82,29 @@ module Tags
it "deletes the tag" do
expect{ subject }.to change { PactBroker::Domain::Tag.count }.by(-2)
end

end


describe "find_all_tag_names_for_pacticipant" do
before do
td.create_consumer("Foo")
.create_consumer_version("1")
.create_consumer_version_tag("prod")
.create_consumer_version_tag("master")
.create_consumer_version("2")
.create_consumer_version_tag("prod")
.create_consumer_version_tag("dev")
.create_consumer("Bar")
.create_consumer_version("1")
.create_consumer_version_tag("ignore")
end

subject { Repository.new.find_all_tag_names_for_pacticipant("Foo") }

it "returns all the tag names for the pacticipant" do
expect(subject).to eq ["dev", "master", "prod"]
end
end
end
end
end
1 change: 0 additions & 1 deletion spec/lib/pact_broker/tags/service_spec.rb
Expand Up @@ -45,7 +45,6 @@ module Tags
}.by(-1)
end
end

end
end
end
34 changes: 33 additions & 1 deletion spec/lib/pact_broker/ui/controllers/index_spec.rb
Expand Up @@ -31,9 +31,41 @@ module Controllers
expect(last_response.status).to eq(200)
end

context "with tags=true" do
before do
allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([])
end

it "passes tags: true to the IndexService" do
expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: true)
get "/", { tags: 'true' }
end
end

context "with tags[]=prod" do
before do
allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([])
end

it "passes tags: ['prod'] to the IndexService" do
expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: ["prod"])
get "/", { tags: ["prod"] }
end
end

context "with tags=prod" do
before do
allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([])
end

it "passes tags: ['prod'] to the IndexService" do
expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: ["prod"])
get "/", { tags: "prod" }
end
end
end
end
end
end
end
end
end

0 comments on commit 0a1f0ee

Please sign in to comment.