Skip to content

Commit

Permalink
Use a concern to share code between checkables
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Nov 14, 2018
1 parent 7bc6d69 commit 39d0135
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
10 changes: 3 additions & 7 deletions src/api/app/models/obs_factory/staging_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,9 @@ def fetch_status
end

def add_status(checkable)
status = checkable.status_reports.latest
if status
@missing_checks += status.missing_checks
@checks += status.checks
else
@missing_checks += checkable.required_checks
end
status = checkable.current_status_report
@missing_checks += status.missing_checks
@checks += status.checks
end

# Used internally to calculate #broken_packages and #building_repositories
Expand Down
12 changes: 2 additions & 10 deletions src/api/app/models/repository.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Repository < ApplicationRecord
serialize :required_checks, Array
include Status::Checkable

belongs_to :project, foreign_key: :db_project_id, inverse_of: :repositories

before_destroy :cleanup_before_destroy
Expand All @@ -16,15 +17,6 @@ class Repository < ApplicationRecord
has_many :product_medium, dependent: :delete_all
has_many :repository_architectures, -> { order('position') }, dependent: :destroy, inverse_of: :repository
has_many :architectures, -> { order('position') }, through: :repository_architectures
has_many :status_reports, as: :checkable, class_name: 'Status::Report', dependent: :destroy do
def for_uuid(uuid)
where(status_reports: { uuid: uuid })
end

def latest
for_uuid(proxy_association.owner.build_id).first
end
end

scope :not_remote, -> { where(remote_project_name: '') }
scope :remote, -> { where.not(remote_project_name: '') }
Expand Down
13 changes: 2 additions & 11 deletions src/api/app/models/repository_architecture.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class RepositoryArchitecture < ApplicationRecord
serialize :required_checks, Array
include Status::Checkable

belongs_to :repository, inverse_of: :repository_architectures
belongs_to :architecture, inverse_of: :repository_architectures

Expand All @@ -8,16 +9,6 @@ class RepositoryArchitecture < ApplicationRecord
validates :repository, :architecture, :position, presence: true
validates :repository, uniqueness: { scope: :architecture }

has_many :status_reports, as: :checkable, class_name: 'Status::Report', dependent: :destroy do
def for_uuid(uuid)
where(status_reports: { uuid: uuid })
end

def latest
for_uuid(proxy_association.owner.build_id).first
end
end

def build_id
Backend::Api::Build::Repository.build_id(repository.project.name, repository.name, architecture.name)
end
Expand Down
12 changes: 12 additions & 0 deletions src/api/app/models/status/checkable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Status::Checkable
extend ActiveSupport::Concern

included do
serialize :required_checks, Array
has_many :status_reports, as: :checkable, class_name: 'Status::Report', dependent: :destroy
end

def current_status_report
status_reports.find_or_initialize_by(uuid: build_id)
end
end

0 comments on commit 39d0135

Please sign in to comment.