From f17c4950fa5a951102d5592dc962f20e302b44aa Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 5 Nov 2014 15:13:34 +0200 Subject: Refactor commits/builds logic Signed-off-by: Dmitriy Zaporozhets --- app/models/project_status.rb | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 app/models/project_status.rb (limited to 'app/models/project_status.rb') diff --git a/app/models/project_status.rb b/app/models/project_status.rb new file mode 100644 index 0000000..b14cb4f --- /dev/null +++ b/app/models/project_status.rb @@ -0,0 +1,45 @@ +module ProjectStatus + def status + last_commit.status if last_commit + end + + def broken? + last_commit.failed? if last_commit + end + + def success? + last_commit.success? if last_commit + end + + def broken_or_success? + broken? || success? + end + + def last_commit + @last_commit ||= commits.last if commits.any? + end + + def last_commit_date + last_commit.try(:created_at) + end + + def human_status + status + end + + # only check for toggling build status within same ref. + def last_commit_changed_status? + ref = last_commit.ref + last_commits = commits.where(ref: ref).order('id DESC').limit(2) + + if last_commits.size < 2 + false + else + last_commits[0].status != last_commits[1].status + end + end + + def last_commit_for_ref(ref) + commits.where(ref: ref).order('id DESC').first + end +end -- cgit v1.2.1