Commit da03a5c7e25601c2bce8375dbbe1cffc58db7bbf

Authored by Dmitriy Zaporozhets
1 parent 40a956eb

more refactoring using models/concerns

app/models/concerns/issuable.rb 0 → 100644
... ... @@ -0,0 +1,102 @@
  1 +# == Issuable concern
  2 +#
  3 +# Contains common functionality shared between Issues and MergeRequests
  4 +#
  5 +# Used by Issue, MergeRequest
  6 +#
  7 +module Issuable
  8 + extend ActiveSupport::Concern
  9 +
  10 + included do
  11 + belongs_to :project
  12 + belongs_to :author, class_name: "User"
  13 + belongs_to :assignee, class_name: "User"
  14 + belongs_to :milestone
  15 + has_many :notes, as: :noteable, dependent: :destroy
  16 +
  17 + validates :project, presence: true
  18 + validates :author, presence: true
  19 + validates :title, presence: true, length: { within: 0..255 }
  20 + validates :closed, inclusion: { in: [true, false] }
  21 +
  22 + scope :opened, where(closed: false)
  23 + scope :closed, where(closed: true)
  24 + scope :of_group, ->(group) { where(project_id: group.project_ids) }
  25 + scope :assigned, ->(u) { where(assignee_id: u.id)}
  26 + scope :recent, order("created_at DESC")
  27 +
  28 + delegate :name,
  29 + :email,
  30 + to: :author,
  31 + prefix: true
  32 +
  33 + delegate :name,
  34 + :email,
  35 + to: :assignee,
  36 + allow_nil: true,
  37 + prefix: true
  38 +
  39 + attr_accessor :author_id_of_changes
  40 + end
  41 +
  42 + module ClassMethods
  43 + def search(query)
  44 + where("title like :query", query: "%#{query}%")
  45 + end
  46 + end
  47 +
  48 + def today?
  49 + Date.today == created_at.to_date
  50 + end
  51 +
  52 + def new?
  53 + today? && created_at == updated_at
  54 + end
  55 +
  56 + def is_assigned?
  57 + !!assignee_id
  58 + end
  59 +
  60 + def is_being_reassigned?
  61 + assignee_id_changed?
  62 + end
  63 +
  64 + def is_being_closed?
  65 + closed_changed? && closed
  66 + end
  67 +
  68 + def is_being_reopened?
  69 + closed_changed? && !closed
  70 + end
  71 +
  72 + # Return the number of +1 comments (upvotes)
  73 + def upvotes
  74 + notes.select(&:upvote?).size
  75 + end
  76 +
  77 + def upvotes_in_percent
  78 + if votes_count.zero?
  79 + 0
  80 + else
  81 + 100.0 / votes_count * upvotes
  82 + end
  83 + end
  84 +
  85 + # Return the number of -1 comments (downvotes)
  86 + def downvotes
  87 + notes.select(&:downvote?).size
  88 + end
  89 +
  90 + def downvotes_in_percent
  91 + if votes_count.zero?
  92 + 0
  93 + else
  94 + 100.0 - upvotes_in_percent
  95 + end
  96 + end
  97 +
  98 + # Return the total number of votes
  99 + def votes_count
  100 + upvotes + downvotes
  101 + end
  102 +end
... ...
app/models/issue.rb
... ... @@ -17,8 +17,7 @@
17 17 #
18 18  
19 19 class Issue < ActiveRecord::Base
20   - include IssueCommonality
21   - include Votes
  20 + include Issuable
22 21  
23 22 attr_accessible :title, :assignee_id, :closed, :position, :description,
24 23 :milestone_id, :label_list, :author_id_of_changes
... ...
app/models/merge_request.rb
... ... @@ -23,8 +23,7 @@ require Rails.root.join(&quot;app/models/commit&quot;)
23 23 require Rails.root.join("lib/static_model")
24 24  
25 25 class MergeRequest < ActiveRecord::Base
26   - include IssueCommonality
27   - include Votes
  26 + include Issuable
28 27  
29 28 attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch, :milestone_id,
30 29 :author_id_of_changes
... ...
app/models/project.rb
... ... @@ -21,7 +21,7 @@
21 21 require "grit"
22 22  
23 23 class Project < ActiveRecord::Base
24   - include GitHost
  24 + include Gitolited
25 25  
26 26 class TransferError < StandardError; end
27 27  
... ... @@ -408,7 +408,7 @@ class Project &lt; ActiveRecord::Base
408 408  
409 409 Gitlab::ProjectMover.new(self, old_dir, new_dir).execute
410 410  
411   - git_host.move_repository(old_repo, self)
  411 + gitolite.move_repository(old_repo, self)
412 412  
413 413 save!
414 414 end
... ... @@ -670,7 +670,7 @@ class Project &lt; ActiveRecord::Base
670 670 end
671 671  
672 672 def url_to_repo
673   - git_host.url_to_repo(path_with_namespace)
  673 + gitolite.url_to_repo(path_with_namespace)
674 674 end
675 675  
676 676 def path_to_repo
... ... @@ -682,11 +682,11 @@ class Project &lt; ActiveRecord::Base
682 682 end
683 683  
684 684 def update_repository
685   - git_host.update_repository(self)
  685 + gitolite.update_repository(self)
686 686 end
687 687  
688 688 def destroy_repository
689   - git_host.remove_repository(self)
  689 + gitolite.remove_repository(self)
690 690 end
691 691  
692 692 def repo_exists?
... ...
app/models/protected_branch.rb
... ... @@ -10,7 +10,7 @@
10 10 #
11 11  
12 12 class ProtectedBranch < ActiveRecord::Base
13   - include GitHost
  13 + include Gitolited
14 14  
15 15 attr_accessible :name
16 16  
... ... @@ -22,7 +22,7 @@ class ProtectedBranch &lt; ActiveRecord::Base
22 22 after_destroy :update_repository
23 23  
24 24 def update_repository
25   - git_host.update_repository(project)
  25 + gitolite.update_repository(project)
26 26 end
27 27  
28 28 def commit
... ...
app/models/users_project.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12  
13 13 class UsersProject < ActiveRecord::Base
14   - include GitHost
  14 + include Gitolited
15 15  
16 16 GUEST = 10
17 17 REPORTER = 20
... ... @@ -152,7 +152,7 @@ class UsersProject &lt; ActiveRecord::Base
152 152 end
153 153  
154 154 def update_repository
155   - git_host.update_repository(project)
  155 + gitolite.update_repository(project)
156 156 end
157 157  
158 158 def project_access_human
... ...
app/observers/key_observer.rb
1 1 class KeyObserver < ActiveRecord::Observer
2   - include GitHost
  2 + include Gitolited
3 3  
4 4 def after_save(key)
5   - git_host.set_key(key.identifier, key.key, key.projects)
  5 + gitolite.set_key(key.identifier, key.key, key.projects)
6 6 end
7 7  
8 8 def after_destroy(key)
9 9 return if key.is_deploy_key && !key.last_deploy?
10   - git_host.remove_key(key.identifier, key.projects)
  10 + gitolite.remove_key(key.identifier, key.projects)
11 11 end
12 12 end
... ...
lib/git_host.rb
... ... @@ -1,11 +0,0 @@
1   -# == GitHost role
2   -#
3   -# Provide a shortcut to Gitlab::Gitolite instance
4   -#
5   -# Used by Project, UsersProject
6   -#
7   -module GitHost
8   - def git_host
9   - Gitlab::Gitolite.new
10   - end
11   -end
lib/gitolited.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +# == Gitolited mixin
  2 +#
  3 +# Provide a shortcut to Gitlab::Gitolite instance by gitolite
  4 +#
  5 +# Used by Project, UsersProject, etc
  6 +#
  7 +module Gitolited
  8 + def gitolite
  9 + Gitlab::Gitolite.new
  10 + end
  11 +end
... ...
lib/issue_commonality.rb
... ... @@ -1,71 +0,0 @@
1   -# == IssueCommonality role
2   -#
3   -# Contains common functionality shared between Issues and MergeRequests
4   -#
5   -# Used by Issue, MergeRequest
6   -#
7   -module IssueCommonality
8   - extend ActiveSupport::Concern
9   -
10   - included do
11   - belongs_to :project
12   - belongs_to :author, class_name: "User"
13   - belongs_to :assignee, class_name: "User"
14   - belongs_to :milestone
15   - has_many :notes, as: :noteable, dependent: :destroy
16   -
17   - validates :project, presence: true
18   - validates :author, presence: true
19   - validates :title, presence: true, length: { within: 0..255 }
20   - validates :closed, inclusion: { in: [true, false] }
21   -
22   - scope :opened, where(closed: false)
23   - scope :closed, where(closed: true)
24   - scope :of_group, ->(group) { where(project_id: group.project_ids) }
25   - scope :assigned, ->(u) { where(assignee_id: u.id)}
26   - scope :recent, order("created_at DESC")
27   -
28   - delegate :name,
29   - :email,
30   - to: :author,
31   - prefix: true
32   -
33   - delegate :name,
34   - :email,
35   - to: :assignee,
36   - allow_nil: true,
37   - prefix: true
38   -
39   - attr_accessor :author_id_of_changes
40   - end
41   -
42   - module ClassMethods
43   - def search(query)
44   - where("title like :query", query: "%#{query}%")
45   - end
46   - end
47   -
48   - def today?
49   - Date.today == created_at.to_date
50   - end
51   -
52   - def new?
53   - today? && created_at == updated_at
54   - end
55   -
56   - def is_assigned?
57   - !!assignee_id
58   - end
59   -
60   - def is_being_reassigned?
61   - assignee_id_changed?
62   - end
63   -
64   - def is_being_closed?
65   - closed_changed? && closed
66   - end
67   -
68   - def is_being_reopened?
69   - closed_changed? && !closed
70   - end
71   -end
lib/votes.rb
... ... @@ -1,39 +0,0 @@
1   -# == Votes role
2   -#
3   -# Provides functionality to upvote/downvote entity
4   -# based on +1 and -1 notes
5   -#
6   -# Used for Issue and Merge Request
7   -#
8   -module Votes
9   - # Return the number of +1 comments (upvotes)
10   - def upvotes
11   - notes.select(&:upvote?).size
12   - end
13   -
14   - def upvotes_in_percent
15   - if votes_count.zero?
16   - 0
17   - else
18   - 100.0 / votes_count * upvotes
19   - end
20   - end
21   -
22   - # Return the number of -1 comments (downvotes)
23   - def downvotes
24   - notes.select(&:downvote?).size
25   - end
26   -
27   - def downvotes_in_percent
28   - if votes_count.zero?
29   - 0
30   - else
31   - 100.0 - upvotes_in_percent
32   - end
33   - end
34   -
35   - # Return the total number of votes
36   - def votes_count
37   - upvotes + downvotes
38   - end
39   -end