Commit 1413c23c502d5a5cbc9b81f553a245103c1d6e50

Authored by Dmitriy Zaporozhets
2 parents 5fd0e7ba b7f9b822

Merge pull request #1208 from tsigo/issue_commonality

Consolidate functionality shared between Issue and MergeRequest
app/models/issue.rb
1 class Issue < ActiveRecord::Base 1 class Issue < ActiveRecord::Base
  2 + include IssueCommonality
2 include Upvote 3 include Upvote
3 4
4 acts_as_taggable_on :labels 5 acts_as_taggable_on :labels
5 6
6 - belongs_to :project  
7 belongs_to :milestone 7 belongs_to :milestone
8 - belongs_to :author, :class_name => "User"  
9 - belongs_to :assignee, :class_name => "User"  
10 - has_many :notes, :as => :noteable, :dependent => :destroy  
11 -  
12 - attr_protected :author, :author_id, :project, :project_id  
13 - attr_accessor :author_id_of_changes  
14 -  
15 - validates_presence_of :project_id  
16 - validates_presence_of :author_id  
17 -  
18 - delegate :name,  
19 - :email,  
20 - :to => :author,  
21 - :prefix => true  
22 -  
23 - delegate :name,  
24 - :email,  
25 - :to => :assignee,  
26 - :allow_nil => true,  
27 - :prefix => true  
28 -  
29 - validates :title,  
30 - :presence => true,  
31 - :length => { :within => 0..255 }  
32 8
33 validates :description, 9 validates :description,
34 :length => { :within => 0..2000 } 10 :length => { :within => 0..2000 }
35 11
36 - scope :opened, where(:closed => false)  
37 - scope :closed, where(:closed => true)  
38 - scope :assigned, lambda { |u| where(:assignee_id => u.id)}  
39 -  
40 acts_as_list 12 acts_as_list
41 13
42 def self.open_for(user) 14 def self.open_for(user)
43 opened.assigned(user) 15 opened.assigned(user)
44 end 16 end
45 17
46 - def self.search query  
47 - where("title like :query", :query => "%#{query}%")  
48 - end  
49 -  
50 - def today?  
51 - Date.today == created_at.to_date  
52 - end  
53 -  
54 - def new?  
55 - today? && created_at == updated_at  
56 - end  
57 -  
58 def is_assigned? 18 def is_assigned?
59 !!assignee_id 19 !!assignee_id
60 end 20 end
app/models/merge_request.rb
1 require File.join(Rails.root, "app/models/commit") 1 require File.join(Rails.root, "app/models/commit")
2 2
3 class MergeRequest < ActiveRecord::Base 3 class MergeRequest < ActiveRecord::Base
  4 + include IssueCommonality
4 include Upvote 5 include Upvote
5 6
6 BROKEN_DIFF = "--broken-diff" 7 BROKEN_DIFF = "--broken-diff"
@@ -9,47 +10,15 @@ class MergeRequest &lt; ActiveRecord::Base @@ -9,47 +10,15 @@ class MergeRequest &lt; ActiveRecord::Base
9 CAN_BE_MERGED = 2 10 CAN_BE_MERGED = 2
10 CANNOT_BE_MERGED = 3 11 CANNOT_BE_MERGED = 3
11 12
12 - belongs_to :project  
13 - belongs_to :author, :class_name => "User"  
14 - belongs_to :assignee, :class_name => "User"  
15 - has_many :notes, :as => :noteable, :dependent => :destroy  
16 -  
17 serialize :st_commits 13 serialize :st_commits
18 serialize :st_diffs 14 serialize :st_diffs
19 15
20 - attr_protected :author, :author_id, :project, :project_id  
21 - attr_accessor :author_id_of_changes,  
22 - :should_remove_source_branch 16 + attr_accessor :should_remove_source_branch
23 17
24 - validates_presence_of :project_id  
25 - validates_presence_of :author_id  
26 validates_presence_of :source_branch 18 validates_presence_of :source_branch
27 validates_presence_of :target_branch 19 validates_presence_of :target_branch
28 validate :validate_branches 20 validate :validate_branches
29 21
30 - delegate :name,  
31 - :email,  
32 - :to => :author,  
33 - :prefix => true  
34 -  
35 - delegate :name,  
36 - :email,  
37 - :to => :assignee,  
38 - :allow_nil => true,  
39 - :prefix => true  
40 -  
41 - validates :title,  
42 - :presence => true,  
43 - :length => { :within => 0..255 }  
44 -  
45 - scope :opened, where(:closed => false)  
46 - scope :closed, where(:closed => true)  
47 - scope :assigned, lambda { |u| where(:assignee_id => u.id)}  
48 -  
49 - def self.search query  
50 - where("title like :query", :query => "%#{query}%")  
51 - end  
52 -  
53 def self.find_all_by_branch(branch_name) 22 def self.find_all_by_branch(branch_name)
54 where("source_branch like :branch or target_branch like :branch", :branch => branch_name) 23 where("source_branch like :branch or target_branch like :branch", :branch => branch_name)
55 end 24 end
@@ -95,14 +64,6 @@ class MergeRequest &lt; ActiveRecord::Base @@ -95,14 +64,6 @@ class MergeRequest &lt; ActiveRecord::Base
95 self.save 64 self.save
96 end 65 end
97 66
98 - def today?  
99 - Date.today == created_at.to_date  
100 - end  
101 -  
102 - def new?  
103 - today? && created_at == updated_at  
104 - end  
105 -  
106 def diffs 67 def diffs
107 st_diffs || [] 68 st_diffs || []
108 end 69 end
@@ -135,7 +96,7 @@ class MergeRequest &lt; ActiveRecord::Base @@ -135,7 +96,7 @@ class MergeRequest &lt; ActiveRecord::Base
135 commits.first 96 commits.first
136 end 97 end
137 98
138 - def merged? 99 + def merged?
139 merged && merge_event 100 merged && merge_event
140 end 101 end
141 102
@@ -152,7 +113,7 @@ class MergeRequest &lt; ActiveRecord::Base @@ -152,7 +113,7 @@ class MergeRequest &lt; ActiveRecord::Base
152 end 113 end
153 114
154 def probably_merged? 115 def probably_merged?
155 - unmerged_commits.empty? && 116 + unmerged_commits.empty? &&
156 commits.any? && open? 117 commits.any? && open?
157 end 118 end
158 119
@@ -170,8 +131,8 @@ class MergeRequest &lt; ActiveRecord::Base @@ -170,8 +131,8 @@ class MergeRequest &lt; ActiveRecord::Base
170 self.update_attributes :state => CANNOT_BE_MERGED 131 self.update_attributes :state => CANNOT_BE_MERGED
171 end 132 end
172 133
173 - def reloaded_commits  
174 - if open? && unmerged_commits.any? 134 + def reloaded_commits
  135 + if open? && unmerged_commits.any?
175 self.st_commits = unmerged_commits 136 self.st_commits = unmerged_commits
176 save 137 save
177 end 138 end
app/roles/issue_commonality.rb 0 → 100644
@@ -0,0 +1,52 @@ @@ -0,0 +1,52 @@
  1 +# Contains common functionality shared between Issues and MergeRequests
  2 +module IssueCommonality
  3 + extend ActiveSupport::Concern
  4 +
  5 + included do
  6 + attr_protected :author, :author_id, :project, :project_id
  7 +
  8 + belongs_to :project
  9 + belongs_to :author, :class_name => "User"
  10 + belongs_to :assignee, :class_name => "User"
  11 + has_many :notes, :as => :noteable, :dependent => :destroy
  12 +
  13 + validates_presence_of :project_id
  14 + validates_presence_of :author_id
  15 +
  16 + validates :title,
  17 + :presence => true,
  18 + :length => { :within => 0..255 }
  19 +
  20 +
  21 + scope :opened, where(:closed => false)
  22 + scope :closed, where(:closed => true)
  23 + scope :assigned, lambda { |u| where(:assignee_id => u.id)}
  24 +
  25 + delegate :name,
  26 + :email,
  27 + :to => :author,
  28 + :prefix => true
  29 +
  30 + delegate :name,
  31 + :email,
  32 + :to => :assignee,
  33 + :allow_nil => true,
  34 + :prefix => true
  35 +
  36 + attr_accessor :author_id_of_changes
  37 + end
  38 +
  39 + module ClassMethods
  40 + def search(query)
  41 + where("title like :query", :query => "%#{query}%")
  42 + end
  43 + end
  44 +
  45 + def today?
  46 + Date.today == created_at.to_date
  47 + end
  48 +
  49 + def new?
  50 + today? && created_at == updated_at
  51 + end
  52 +end
spec/models/issue_spec.rb
@@ -106,6 +106,14 @@ describe Issue do @@ -106,6 +106,14 @@ describe Issue do
106 end 106 end
107 end 107 end
108 108
  109 + describe ".search" do
  110 + let!(:issue) { Factory.create(:issue, :title => "Searchable issue",
  111 + :project => Factory.create(:project)) }
  112 +
  113 + it "matches by title" do
  114 + Issue.search('able').all.should == [issue]
  115 + end
  116 + end
109 end 117 end
110 # == Schema Information 118 # == Schema Information
111 # 119 #
spec/models/merge_request_spec.rb
@@ -56,6 +56,15 @@ describe MergeRequest do @@ -56,6 +56,15 @@ describe MergeRequest do
56 subject.upvotes.should == 2 56 subject.upvotes.should == 2
57 end 57 end
58 end 58 end
  59 +
  60 + describe ".search" do
  61 + let!(:issue) { Factory.create(:issue, :title => "Searchable issue",
  62 + :project => Factory.create(:project)) }
  63 +
  64 + it "matches by title" do
  65 + Issue.search('able').all.should == [issue]
  66 + end
  67 + end
59 end 68 end
60 # == Schema Information 69 # == Schema Information
61 # 70 #