Skip to content

Commit

Permalink
refactor: separate version selection code into Version domain class
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 29, 2020
1 parent 69dc8ed commit 02ba3b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
9 changes: 9 additions & 0 deletions lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def delete
PactBroker::Domain::Tag.where(version: self).delete
super
end

def find_for_selector(selector)
query = select_all_qualified
query = query.where_pacticipant_name(selector.pacticipant_name) if selector.pacticipant_name
query = query.where_tag(selector.tag) if selector.tag
query = query.where_number(selector.pacticipant_version_number) if selector.pacticipant_version_number
query = query.reverse_order(:order).limit(1) if selector.latest
versions = query.all
end
end

def after_create
Expand Down
16 changes: 15 additions & 1 deletion lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,25 @@ def base_model(options)
def resolve_versions_and_add_ids(selectors, selector_type)
selectors.collect do | selector |
pacticipant = PactBroker::Domain::Pacticipant.find(name: selector.pacticipant_name)
versions = version_repository.find_versions_for_selector(selector)
versions = find_versions_for_selector(selector)
build_resolved_selectors(pacticipant, versions, selector, selector_type)
end.flatten
end

def find_versions_for_selector(selector)
# For selectors that just set the pacticipant name, there's no need to resolve the version -
# only the pacticipant ID will be used in the query
return nil unless (selector.tag || selector.latest || selector.pacticipant_version_number)

versions = version_repository.find_versions_for_selector(selector)

if selector.latest
[versions.first]
else
versions.empty? ? [nil] : versions
end
end

# When a single selector specifies multiple versions (eg. "all prod pacts"), this expands
# the single selector into one selector for each version.
def build_resolved_selectors(pacticipant, versions, original_selector, selector_type)
Expand Down
16 changes: 1 addition & 15 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,7 @@ def delete_orphan_versions consumer, provider
end

def find_versions_for_selector(selector)
if selector.tag && selector.latest
version = find_by_pacticipant_name_and_latest_tag(selector.pacticipant_name, selector.tag)
[version]
elsif selector.latest
version = find_latest_by_pacticpant_name(selector.pacticipant_name)
[version]
elsif selector.tag
versions = find_by_pacticipant_name_and_tag(selector.pacticipant_name, selector.tag)
versions.any? ? versions : [nil]
elsif selector.pacticipant_version_number
version = find_by_pacticipant_name_and_number(selector.pacticipant_name, selector.pacticipant_version_number)
[version]
else
nil
end
PactBroker::Domain::Version.find_for_selector(selector)
end
end
end
Expand Down

0 comments on commit 02ba3b7

Please sign in to comment.