Skip to content

Commit

Permalink
feat(matrix ui): add form
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Dec 14, 2017
1 parent 15c7d93 commit cbf0837
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 57 deletions.
5 changes: 5 additions & 0 deletions lib/pact_broker/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module PactBroker

class Error < StandardError; end

end
6 changes: 3 additions & 3 deletions lib/pact_broker/matrix/parse_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ def self.call query
params = Rack::Utils.parse_nested_query(query)
selectors = (params['q'] || []).collect do |i|
p = {}
p[:pacticipant_name] = i['pacticipant'] if i['pacticipant']
p[:pacticipant_version_number] = i['version'] if i['version']
p[:pacticipant_name] = i['pacticipant'] if i['pacticipant'] && i['pacticipant'] != ''
p[:pacticipant_version_number] = i['version'] if i['version'] && i['version'] != ''
p[:latest] = true if i['latest'] == 'true'
p[:tag] = i['tag'] if i['tag']
p[:tag] = i['tag'] if i['tag'] && i['tag'] != ''
p
end
options = {}
Expand Down
37 changes: 5 additions & 32 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
require 'pact_broker/repositories/helpers'
require 'pact_broker/matrix/row'
require 'pact_broker/matrix/latest_row'
require 'pact_broker/error'

module PactBroker
module Matrix

class Error < PactBroker::Error; end

class Repository
include PactBroker::Repositories::Helpers
include PactBroker::Repositories
Expand Down Expand Up @@ -78,7 +82,7 @@ def look_up_versions_for_latest_and_tag(selectors, options)
# resource validation currently stops tag being specified without latest=true
if selector[:tag] && selector[:latest]
version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:tag])
raise "Could not find version with tag #{selector[:tag].inspect} for #{selector[:pacticipant_name]}" unless version
raise Error.new("Could not find version with tag #{selector[:tag].inspect} for #{selector[:pacticipant_name]}") unless version
# validation in resource should ensure we always have a version
{
pacticipant_name: selector[:pacticipant_name],
Expand Down Expand Up @@ -122,37 +126,6 @@ def all_pacticipant_names_in_specified_matrix(selectors, options)
.flatten
.uniq
end

# def where_row_matches_selectors selectors, query
# if selectors.size == 1
# where_consumer_or_provider_is(selectors.first, query)
# else
# where_consumer_and_provider_in(selectors, query)
# end
# end

# def where_consumer_and_provider_in selectors, query
# query.where{
# Sequel.&(
# Sequel.|(
# *selectors.collect{ |s| s[:pacticipant_version_number] ? Sequel.&(consumer_name: s[:pacticipant_name], consumer_version_number: s[:pacticipant_version_number]) : Sequel.&(consumer_name: s[:pacticipant_name]) }
# ),
# Sequel.|(
# *(selectors.collect{ |s| s[:pacticipant_version_number] ? Sequel.&(provider_name: s[:pacticipant_name], provider_version_number: s[:pacticipant_version_number]) : Sequel.&(provider_name: s[:pacticipant_name]) } +
# selectors.collect{ |s| Sequel.&(provider_name: s[:pacticipant_name], provider_version_number: nil) })
# )
# )
# }
# end

# def where_consumer_or_provider_is s, query
# query.where{
# Sequel.|(
# s[:pacticipant_version_number] ? Sequel.&(consumer_name: s[:pacticipant_name], consumer_version_number: s[:pacticipant_version_number]) : Sequel.&(consumer_name: s[:pacticipant_name]),
# s[:pacticipant_version_number] ? Sequel.&(provider_name: s[:pacticipant_name], provider_version_number: s[:pacticipant_version_number]) : Sequel.&(provider_name: s[:pacticipant_name])
# )
# }
# end
end
end
end
4 changes: 1 addition & 3 deletions lib/pact_broker/matrix/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def validate_selectors selectors
error_messages = []

selectors.each do | s |
if s[:pacticipant_name].nil? && s[:pacticipant_version_number].nil?
error_messages << "Please specify the pacticipant name and version"
elsif s[:pacticipant_name].nil?
if s[:pacticipant_name].nil?
error_messages << "Please specify the pacticipant name"
else
if s.key?(:pacticipant_version_number) && s.key?(:latest)
Expand Down
15 changes: 15 additions & 0 deletions lib/pact_broker/ui/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@
require 'pact_broker/ui/controllers/matrix'
require 'pact_broker/doc/controllers/app'


module PactBroker
module UI
class PathInfoFixer
PATH_INFO = 'PATH_INFO'.freeze

def initialize app
@app = app
end

def call env
env[PATH_INFO] = '/' if env[PATH_INFO] == ''
@app.call(env)
end
end

class App

def initialize
Expand All @@ -23,6 +37,7 @@ def initialize
end

map "/matrix" do
use PathInfoFixer
run PactBroker::UI::Controllers::Matrix
end

Expand Down
42 changes: 41 additions & 1 deletion lib/pact_broker/ui/controllers/matrix.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'pact_broker/ui/controllers/base_controller'
require 'pact_broker/ui/view_models/matrix_line'
require 'pact_broker/matrix/parse_query'
require 'pact_broker/logging'
require 'haml'

module PactBroker
Expand All @@ -8,11 +10,38 @@ module Controllers
class Matrix < Base

include PactBroker::Services
include PactBroker::Logging

get "/" do
selectors = [OpenStruct.new, OpenStruct.new]
locals = {
lines: [],
title: "The Matrix",
selectors: create_selector_objects(selectors)
}
begin
if params[:q]
selectors, options = PactBroker::Matrix::ParseQuery.call(request.env['QUERY_STRING'])
locals[:selectors] = create_selector_objects(selectors)
errors = matrix_service.validate_selectors(selectors)
if errors.empty?
lines = matrix_service.find(selectors, options)
locals[:lines] = lines.collect{ |line| PactBroker::UI::ViewDomain::MatrixLine.new(line) }
else
locals[:errors] = errors
end
end
rescue StandardError => e
log_error e
locals[:errors] = [e.message]
end
haml :'matrix/show', {locals: locals, layout: :'layouts/main'}
end

get "/provider/:provider_name/consumer/:consumer_name" do
selectors = [{ pacticipant_name: params[:consumer_name] }, { pacticipant_name: params[:provider_name] } ]
lines = matrix_service.find(selectors, {latestby: 'cvpv', limit: 1000})
lines = lines.collect{|line| PactBroker::UI::ViewDomain::MatrixLine.new(line) }.sort
lines = lines.collect{ |line| PactBroker::UI::ViewDomain::MatrixLine.new(line) }.sort
locals = {
lines: lines,
title: "The Matrix",
Expand All @@ -22,6 +51,17 @@ class Matrix < Base
haml :'matrix/show', {locals: locals, layout: :'layouts/main'}
end

def create_selector_objects(selector_hashes)
selector_hashes.collect do | selector_hash |
o = OpenStruct.new(selector_hash)
o.tag_disabled = o.tag ? nil : 'disabled'
o.version_disabled = o.pacticipant_version_number ? nil : 'disabled'
o.specify_latest_tag_checked = o.tag ? 'checked' : nil
o.specify_version_checked = o.pacticipant_version_number ? 'checked' : nil
o.specify_all_versions_checked = !(o.tag || o.pacticipant_version_number) ? 'checked' : nil
o
end
end
end
end
end
Expand Down
48 changes: 38 additions & 10 deletions lib/pact_broker/ui/views/matrix/show.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
%body
%link{rel: 'stylesheet', href: '/css/bootstrap.min.css'}
%link{rel: 'stylesheet', href: '/stylesheets/index.css'}
%link{rel: 'stylesheet', href: '/stylesheets/matrix.css'}
%script{type: 'text/javascript', src:'/javascripts/jquery-2.1.1.min.js'}
%script{type: 'text/javascript', src:'/javascripts/jquery.tablesorter.min.js'}
%script{type: 'text/javascript', src:'/javascripts/matrix.js'}
%script{type: 'text/javascript', src:'/js/bootstrap.min.js'}

.container
%h1.page-header
= title

- if defined?(errors) && errors.any?
- errors.each do | error |
%div.alert.alert-danger
= error

%form{action: '/matrix', onsubmit:'return onSubmit()'}
- selectors.each_with_index do | selector, index |
.selector
%label{for: "pacticipant#{index}"}
Pacticipant name
%input{name: 'q[]pacticipant', id: "pacticipant1#{index}", value: selector.pacticipant_name}

.input-group
%input{type: 'radio', name: "ignorethis#{index}", class: 'specify-all-versions version-selectorizor', value: 'all_versions', id: "pacticipant#{index}_all_versions", checked: selector.specify_all_versions_checked}
%label{for: "pacticipant#{index}_all_versions"}
All versions

.input-group
%input{type: 'radio', name: "ignorethis#{index}", class: 'specify-version version-selectorizor', value: 'version', id: "pacticipant#{index}_by_version", checked: selector.specify_version_checked}
%label{for: "pacticipant#{index}_by_version"}
Version
%input{name: 'q[]version', type: 'text', id: "pacticipant#{index}_version", class: 'by-version', value: selector.pacticipant_version_number}

.input-group
%input{type: 'radio', name: "ignorethis#{index}", class: 'specify-latest-tag version-selectorizor', value: 'tag', id: "pacticipant#{index}_by_tag", checked: selector.specify_latest_tag_checked}
%label{for: "pacticipant#{index}_by_tag"}
Latest version with tag
%input{name: 'q[]tag', type: 'text', id: "pacticipant#{index}_tag", class: "by-latest-tag", value: selector.tag}
%input{name: 'q[]latest', value: 'true', hidden: true, class: 'latest-flag'}

%div
%input{type: 'submit'}



%table.table.table-bordered.table-striped{id: 'matrix'}
%thead
%th.consumer
Expand Down Expand Up @@ -45,13 +83,3 @@
= line.provider_version_number
%td.verification-result{class: line.verification_status_class}
= line.verification_status

:javascript
$(function(){
$("#matrix").tablesorter({
textExtraction : function(node, table, cellIndex){
n = $(node);
return n.attr('data-sort-value') || n.text();
}
});
});
59 changes: 59 additions & 0 deletions public/javascripts/matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function handleRadioButtonClicked() {
selectApplicableTextBox($(this));
}

function selectApplicableTextBox(selectedRadioButton) {
selectedRadioButton.closest('.input-group').find('input[type="text"]').first().focus();
}

function handleTextBoxClicked() {
selectApplicableRadioButton($(this));
clearOtherTextBoxes($(this));
}

function selectApplicableRadioButton(selectedTextBox) {
selectedTextBox.closest('.input-group').find('.version-selectorizor').prop('checked', 'checked');
}

function clearOtherTextBoxes(selectedTextBox) {
selectedTextBox.closest('.selector').find('input[type="text"]').each(function(){
if(!selectedTextBox.is($(this))) {
$(this).prop('value', '');
}
});
}

function onSubmit() {
disableFieldsThatShouldNotBeSubmitted();
return true;
}

function disableFieldsThatShouldNotBeSubmitted() {
disableInputsForUncheckedRadioButtons();
disableRadioButtons();
}

function disableInputsForUncheckedRadioButtons() {
$('.version-selectorizor').each(function(){
if($(this).prop('checked') === false) {
$(this).closest('.input-group').find('input').prop('disabled', 'disabled');
}
});
}

function disableRadioButtons() {
$('.version-selectorizor').prop('disabled', 'disabled');
}

$(document).ready(function(){
$('.by-version').click(handleTextBoxClicked);
$('.by-latest-tag').click(handleTextBoxClicked);
$('.version-selectorizor').click(handleRadioButtonClicked);

$("#matrix").tablesorter({
textExtraction : function(node, table, cellIndex){
n = $(node);
return n.attr('data-sort-value') || n.text();
}
});
});
4 changes: 4 additions & 0 deletions public/stylesheets/matrix.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.input-group {
display: inline-block;
padding-left: 20px
}
2 changes: 2 additions & 0 deletions script/seed-matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@

TestDataBuilder.new
.create_pact_with_hierarchy("A", "1", "B")
.create_consumer_version_tag("master")
.create_verification(provider_version: '1', success: false)
.create_verification(provider_version: '1', number: 2, success: true)
.create_verification(provider_version: '2', number: 3)
.create_verification(provider_version: '4', number: 4)
.create_provider_version("5")
.use_consumer("B")
.use_consumer_version("1")
.create_consumer_version_tag("master")
.create_provider("C")
.create_pact
.create_verification(provider_version: '1', success: false)
Expand Down
8 changes: 0 additions & 8 deletions spec/lib/pact_broker/matrix/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ module Matrix
end
end

context "when the pacticipant name and version are not specified" do
let(:selectors) { [{ pacticipant_name: nil, pacticipant_version_number: nil }] }

it "returns error messages" do
expect(subject.first).to eq "Please specify the pacticipant name and version"
end
end

context "when the latest_tag is used instead of a version" do
before do
td.create_pacticipant("Foo")
Expand Down

0 comments on commit cbf0837

Please sign in to comment.