From da03a5c7e25601c2bce8375dbbe1cffc58db7bbf Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 3 Jan 2013 09:06:07 +0200 Subject: [PATCH] more refactoring using models/concerns --- .../models/concerns/issuable.rb | 35 ++++++++++++++++- app/models/issue.rb | 3 +- app/models/merge_request.rb | 3 +- app/models/project.rb | 10 ++--- app/models/protected_branch.rb | 4 +- app/models/users_project.rb | 4 +- app/observers/key_observer.rb | 6 +-- lib/git_host.rb | 11 ------ lib/gitolited.rb | 11 ++++++ lib/votes.rb | 39 ------------------- 10 files changed, 58 insertions(+), 68 deletions(-) rename lib/issue_commonality.rb => app/models/concerns/issuable.rb (72%) delete mode 100644 lib/git_host.rb create mode 100644 lib/gitolited.rb delete mode 100644 lib/votes.rb diff --git a/lib/issue_commonality.rb b/app/models/concerns/issuable.rb similarity index 72% rename from lib/issue_commonality.rb rename to app/models/concerns/issuable.rb index b755936c..f9dd74f9 100644 --- a/lib/issue_commonality.rb +++ b/app/models/concerns/issuable.rb @@ -1,10 +1,10 @@ -# == IssueCommonality role +# == Issuable concern # # Contains common functionality shared between Issues and MergeRequests # # Used by Issue, MergeRequest # -module IssueCommonality +module Issuable extend ActiveSupport::Concern included do @@ -68,4 +68,35 @@ module IssueCommonality def is_being_reopened? closed_changed? && !closed end + + # Return the number of +1 comments (upvotes) + def upvotes + notes.select(&:upvote?).size + end + + def upvotes_in_percent + if votes_count.zero? + 0 + else + 100.0 / votes_count * upvotes + end + end + + # Return the number of -1 comments (downvotes) + def downvotes + notes.select(&:downvote?).size + end + + def downvotes_in_percent + if votes_count.zero? + 0 + else + 100.0 - upvotes_in_percent + end + end + + # Return the total number of votes + def votes_count + upvotes + downvotes + end end diff --git a/app/models/issue.rb b/app/models/issue.rb index 1de9d0f9..7381136c 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -17,8 +17,7 @@ # class Issue < ActiveRecord::Base - include IssueCommonality - include Votes + include Issuable attr_accessible :title, :assignee_id, :closed, :position, :description, :milestone_id, :label_list, :author_id_of_changes diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 68211acc..b6ea85f6 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -23,8 +23,7 @@ require Rails.root.join("app/models/commit") require Rails.root.join("lib/static_model") class MergeRequest < ActiveRecord::Base - include IssueCommonality - include Votes + include Issuable attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch, :milestone_id, :author_id_of_changes diff --git a/app/models/project.rb b/app/models/project.rb index a5ef65cd..f60c2442 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -21,7 +21,7 @@ require "grit" class Project < ActiveRecord::Base - include GitHost + include Gitolited class TransferError < StandardError; end @@ -408,7 +408,7 @@ class Project < ActiveRecord::Base Gitlab::ProjectMover.new(self, old_dir, new_dir).execute - git_host.move_repository(old_repo, self) + gitolite.move_repository(old_repo, self) save! end @@ -670,7 +670,7 @@ class Project < ActiveRecord::Base end def url_to_repo - git_host.url_to_repo(path_with_namespace) + gitolite.url_to_repo(path_with_namespace) end def path_to_repo @@ -682,11 +682,11 @@ class Project < ActiveRecord::Base end def update_repository - git_host.update_repository(self) + gitolite.update_repository(self) end def destroy_repository - git_host.remove_repository(self) + gitolite.remove_repository(self) end def repo_exists? diff --git a/app/models/protected_branch.rb b/app/models/protected_branch.rb index c54aa3ce..f405a7bf 100644 --- a/app/models/protected_branch.rb +++ b/app/models/protected_branch.rb @@ -10,7 +10,7 @@ # class ProtectedBranch < ActiveRecord::Base - include GitHost + include Gitolited attr_accessible :name @@ -22,7 +22,7 @@ class ProtectedBranch < ActiveRecord::Base after_destroy :update_repository def update_repository - git_host.update_repository(project) + gitolite.update_repository(project) end def commit diff --git a/app/models/users_project.rb b/app/models/users_project.rb index b8aacb32..ce7ac8ac 100644 --- a/app/models/users_project.rb +++ b/app/models/users_project.rb @@ -11,7 +11,7 @@ # class UsersProject < ActiveRecord::Base - include GitHost + include Gitolited GUEST = 10 REPORTER = 20 @@ -152,7 +152,7 @@ class UsersProject < ActiveRecord::Base end def update_repository - git_host.update_repository(project) + gitolite.update_repository(project) end def project_access_human diff --git a/app/observers/key_observer.rb b/app/observers/key_observer.rb index a3f17bde..bf5fa647 100644 --- a/app/observers/key_observer.rb +++ b/app/observers/key_observer.rb @@ -1,12 +1,12 @@ class KeyObserver < ActiveRecord::Observer - include GitHost + include Gitolited def after_save(key) - git_host.set_key(key.identifier, key.key, key.projects) + gitolite.set_key(key.identifier, key.key, key.projects) end def after_destroy(key) return if key.is_deploy_key && !key.last_deploy? - git_host.remove_key(key.identifier, key.projects) + gitolite.remove_key(key.identifier, key.projects) end end diff --git a/lib/git_host.rb b/lib/git_host.rb deleted file mode 100644 index 2410e0fe..00000000 --- a/lib/git_host.rb +++ /dev/null @@ -1,11 +0,0 @@ -# == GitHost role -# -# Provide a shortcut to Gitlab::Gitolite instance -# -# Used by Project, UsersProject -# -module GitHost - def git_host - Gitlab::Gitolite.new - end -end diff --git a/lib/gitolited.rb b/lib/gitolited.rb new file mode 100644 index 00000000..68b9b625 --- /dev/null +++ b/lib/gitolited.rb @@ -0,0 +1,11 @@ +# == Gitolited mixin +# +# Provide a shortcut to Gitlab::Gitolite instance by gitolite +# +# Used by Project, UsersProject, etc +# +module Gitolited + def gitolite + Gitlab::Gitolite.new + end +end diff --git a/lib/votes.rb b/lib/votes.rb deleted file mode 100644 index dfd751b1..00000000 --- a/lib/votes.rb +++ /dev/null @@ -1,39 +0,0 @@ -# == Votes role -# -# Provides functionality to upvote/downvote entity -# based on +1 and -1 notes -# -# Used for Issue and Merge Request -# -module Votes - # Return the number of +1 comments (upvotes) - def upvotes - notes.select(&:upvote?).size - end - - def upvotes_in_percent - if votes_count.zero? - 0 - else - 100.0 / votes_count * upvotes - end - end - - # Return the number of -1 comments (downvotes) - def downvotes - notes.select(&:downvote?).size - end - - def downvotes_in_percent - if votes_count.zero? - 0 - else - 100.0 - upvotes_in_percent - end - end - - # Return the total number of votes - def votes_count - upvotes + downvotes - end -end