Commit 841e4fbd08d2d642c127506001a76a973ea3d536

Authored by Nihad Abbasov
1 parent 2a4359a5

cosmetical cleanup of models

app/models/ability.rb
... ... @@ -52,7 +52,6 @@ class Ability
52 52 :admin_wiki
53 53 ] if project.master_access_for?(user) || project.owner == user
54 54  
55   -
56 55 rules.flatten
57 56 end
58 57  
... ...
app/models/commit.rb
... ... @@ -4,24 +4,11 @@ class Commit
4 4 include StaticModel
5 5 extend ActiveModel::Naming
6 6  
7   - attr_accessor :commit
8   - attr_accessor :head
9   - attr_accessor :refs
10   -
11   - delegate :message,
12   - :authored_date,
13   - :committed_date,
14   - :parents,
15   - :sha,
16   - :date,
17   - :committer,
18   - :author,
19   - :message,
20   - :diffs,
21   - :tree,
22   - :id,
23   - :to_patch,
24   - to: :commit
  7 + attr_accessor :commit, :head, :refs
  8 +
  9 + delegate :message, :authored_date, :committed_date, :parents, :sha,
  10 + :date, :committer, :author, :message, :diffs, :tree, :id,
  11 + :to_patch, to: :commit
25 12  
26 13 class << self
27 14 def find_or_first(repo, commit_id = nil, root_ref)
... ... @@ -30,6 +17,7 @@ class Commit
30 17 else
31 18 repo.commits(root_ref).first
32 19 end
  20 +
33 21 Commit.new(commit) if commit
34 22 end
35 23  
... ...
app/models/event.rb
... ... @@ -16,6 +16,10 @@ class Event &lt; ActiveRecord::Base
16 16 Joined = 8 # User joined project
17 17 Left = 9 # User left project
18 18  
  19 + delegate :name, :email, to: :author, prefix: true, allow_nil: true
  20 + delegate :title, to: :issue, prefix: true, allow_nil: true
  21 + delegate :title, to: :merge_request, prefix: true, allow_nil: true
  22 +
19 23 belongs_to :project
20 24 belongs_to :target, polymorphic: true
21 25  
... ... @@ -134,12 +138,8 @@ class Event &lt; ActiveRecord::Base
134 138 "opened"
135 139 end
136 140 end
137   -
138   -
139   - delegate :name, :email, to: :author, prefix: true, allow_nil: true
140   - delegate :title, to: :issue, prefix: true, allow_nil: true
141   - delegate :title, to: :merge_request, prefix: true, allow_nil: true
142 141 end
  142 +
143 143 # == Schema Information
144 144 #
145 145 # Table name: events
... ... @@ -155,4 +155,3 @@ end
155 155 # action :integer
156 156 # author_id :integer
157 157 #
158   -
... ...
app/models/issue.rb
... ... @@ -9,8 +9,7 @@ class Issue &lt; ActiveRecord::Base
9 9  
10 10 belongs_to :milestone
11 11  
12   - validates :description,
13   - length: { within: 0..2000 }
  12 + validates :description, length: { within: 0..2000 }
14 13  
15 14 def self.open_for(user)
16 15 opened.assigned(user)
... ... @@ -32,6 +31,7 @@ class Issue &lt; ActiveRecord::Base
32 31 closed_changed? && !closed
33 32 end
34 33 end
  34 +
35 35 # == Schema Information
36 36 #
37 37 # Table name: issues
... ... @@ -49,4 +49,3 @@ end
49 49 # description :text
50 50 # milestone_id :integer
51 51 #
52   -
... ...
app/models/key.rb
... ... @@ -6,14 +6,10 @@ class Key &lt; ActiveRecord::Base
6 6  
7 7 attr_accessible :key, :title
8 8  
9   - validates :title,
10   - presence: true,
11   - length: { within: 0..255 }
12   -
13   - validates :key,
14   - presence: true,
15   - format: { :with => /ssh-.{3} / },
16   - length: { within: 0..5000 }
  9 + validates :title, presence: true, length: { within: 0..255 }
  10 + validates :key, presence: true,
  11 + length: { within: 0..5000 },
  12 + format: { :with => /ssh-.{3} / }
17 13  
18 14 before_save :set_identifier
19 15 before_validation :strip_white_space
... ... @@ -34,7 +30,7 @@ class Key &lt; ActiveRecord::Base
34 30  
35 31 def set_identifier
36 32 if is_deploy_key
37   - self.identifier = "deploy_" + Digest::MD5.hexdigest(key)
  33 + self.identifier = "deploy_#{Digest::MD5.hexdigest(key)}"
38 34 else
39 35 self.identifier = "#{user.identifier}_#{Time.now.to_i}"
40 36 end
... ... @@ -57,6 +53,7 @@ class Key &lt; ActiveRecord::Base
57 53 Key.where(identifier: identifier).count == 0
58 54 end
59 55 end
  56 +
60 57 # == Schema Information
61 58 #
62 59 # Table name: keys
... ... @@ -70,4 +67,3 @@ end
70 67 # identifier :string(255)
71 68 # project_id :integer
72 69 #
73   -
... ...
app/models/merge_request.rb
... ... @@ -18,8 +18,7 @@ class MergeRequest &lt; ActiveRecord::Base
18 18  
19 19 attr_accessor :should_remove_source_branch
20 20  
21   - validates_presence_of :source_branch
22   - validates_presence_of :target_branch
  21 + validates_presence_of :source_branch, :target_branch
23 22 validate :validate_branches
24 23  
25 24 def self.find_all_by_branch(branch_name)
... ... @@ -187,6 +186,7 @@ class MergeRequest &lt; ActiveRecord::Base
187 186 patch_path
188 187 end
189 188 end
  189 +
190 190 # == Schema Information
191 191 #
192 192 # Table name: merge_requests
... ... @@ -206,4 +206,3 @@ end
206 206 # merged :boolean default(FALSE), not null
207 207 # state :integer default(1), not null
208 208 #
209   -
... ...
app/models/milestone.rb
1   -# == Schema Information
2   -#
3   -# Table name: milestones
4   -#
5   -# id :integer not null, primary key
6   -# title :string(255) not null
7   -# project_id :integer not null
8   -# description :text
9   -# due_date :date
10   -# closed :boolean default(FALSE), not null
11   -# created_at :datetime not null
12   -# updated_at :datetime not null
13   -#
14   -
15 1 class Milestone < ActiveRecord::Base
16 2 attr_accessible :title, :description, :due_date, :closed
17 3  
18 4 belongs_to :project
19 5 has_many :issues
20 6  
21   - validates_presence_of :project_id
22   - validates_presence_of :title
  7 + validates_presence_of :title, :project_id
23 8  
24 9 def self.active
25 10 where("due_date > ? OR due_date IS NULL", Date.today)
... ... @@ -39,3 +24,17 @@ class Milestone &lt; ActiveRecord::Base
39 24 "expires at #{due_date.stamp("Aug 21, 2011")}" if due_date
40 25 end
41 26 end
  27 +
  28 +# == Schema Information
  29 +#
  30 +# Table name: milestones
  31 +#
  32 +# id :integer not null, primary key
  33 +# title :string(255) not null
  34 +# project_id :integer not null
  35 +# description :text
  36 +# due_date :date
  37 +# closed :boolean default(FALSE), not null
  38 +# created_at :datetime not null
  39 +# updated_at :datetime not null
  40 +#
... ...
app/models/note.rb
... ... @@ -2,39 +2,26 @@ require &#39;carrierwave/orm/activerecord&#39;
2 2 require 'file_size_validator'
3 3  
4 4 class Note < ActiveRecord::Base
  5 + mount_uploader :attachment, AttachmentUploader
5 6 attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id,
6 7 :attachment, :line_code
7 8  
8 9 belongs_to :project
9 10 belongs_to :noteable, polymorphic: true
10   - belongs_to :author,
11   - class_name: "User"
  11 + belongs_to :author, class_name: "User"
12 12  
13   - delegate :name,
14   - to: :project,
15   - prefix: true
16   -
17   - delegate :name,
18   - :email,
19   - to: :author,
20   - prefix: true
  13 + delegate :name, to: :project, prefix: true
  14 + delegate :name, :email, to: :author, prefix: true
21 15  
22 16 attr_accessor :notify
23 17 attr_accessor :notify_author
24 18  
25 19 validates_presence_of :project
26 20  
27   - validates :note,
28   - presence: true,
29   - length: { within: 0..5000 }
30   -
31   - validates :attachment,
32   - file_size: {
33   - maximum: 10.megabytes.to_i
34   - }
  21 + validates :note, presence: true, length: { within: 0..5000 }
  22 + validates :attachment, file_size: { maximum: 10.megabytes.to_i }
35 23  
36 24 scope :common, where(noteable_id: nil)
37   -
38 25 scope :today, where("created_at >= :date", date: Date.today)
39 26 scope :last_week, where("created_at >= :date", date: (Date.today - 7.days))
40 27 scope :since, lambda { |day| where("created_at >= :date", date: (day)) }
... ... @@ -42,14 +29,13 @@ class Note &lt; ActiveRecord::Base
42 29 scope :inc_author_project, includes(:project, :author)
43 30 scope :inc_author, includes(:author)
44 31  
45   - mount_uploader :attachment, AttachmentUploader
46   -
47 32 def self.create_status_change_note(noteable, author, status)
48   - create({ noteable: noteable,
49   - project: noteable.project,
50   - author: author,
51   - note: "_Status changed to #{status}_" },
52   - without_protection: true)
  33 + create({
  34 + noteable: noteable,
  35 + project: noteable.project,
  36 + author: author,
  37 + note: "_Status changed to #{status}_"
  38 + }, without_protection: true)
53 39 end
54 40  
55 41 def notify
... ... @@ -114,6 +100,7 @@ class Note &lt; ActiveRecord::Base
114 100 note.start_with?('-1') || note.start_with?(':-1:')
115 101 end
116 102 end
  103 +
117 104 # == Schema Information
118 105 #
119 106 # Table name: notes
... ... @@ -129,4 +116,3 @@ end
129 116 # attachment :string(255)
130 117 # line_code :string(255)
131 118 #
132   -
... ...
app/models/project.rb
... ... @@ -8,10 +8,9 @@ class Project &lt; ActiveRecord::Base
8 8  
9 9 attr_accessible :name, :path, :description, :code, :default_branch, :issues_enabled,
10 10 :wall_enabled, :merge_requests_enabled, :wiki_enabled
  11 + attr_accessor :error_code
11 12  
12   - #
13 13 # Relations
14   - #
15 14 belongs_to :owner, class_name: "User"
16 15 has_many :users, through: :users_projects
17 16 has_many :events, dependent: :destroy
... ... @@ -26,11 +25,7 @@ class Project &lt; ActiveRecord::Base
26 25 has_many :wikis, dependent: :destroy
27 26 has_many :protected_branches, dependent: :destroy
28 27  
29   - attr_accessor :error_code
30   -
31   - #
32 28 # Scopes
33   - #
34 29 scope :public_only, where(private_flag: false)
35 30 scope :without_user, lambda { |user| where("id not in (:ids)", ids: user.projects.map(&:id) ) }
36 31  
... ... @@ -47,7 +42,6 @@ class Project &lt; ActiveRecord::Base
47 42  
48 43 Project.transaction do
49 44 project.owner = user
50   -
51 45 project.save!
52 46  
53 47 # Add user as project master
... ... @@ -76,36 +70,19 @@ class Project &lt; ActiveRecord::Base
76 70 id && valid?
77 71 end
78 72  
79   - #
80 73 # Validations
81   - #
82   - validates :name,
83   - uniqueness: true,
84   - presence: true,
85   - length: { within: 0..255 }
86   -
87   - validates :path,
88   - uniqueness: true,
89   - presence: true,
  74 + validates :owner, presence: true
  75 + validates :description, length: { within: 0..2000 }
  76 + validates :name, uniqueness: true, presence: true, length: { within: 0..255 }
  77 + validates :path, uniqueness: true, presence: true, length: { within: 0..255 },
90 78 format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/,
91   - message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" },
92   - length: { within: 0..255 }
93   -
94   - validates :description,
95   - length: { within: 0..2000 }
96   -
97   - validates :code,
98   - presence: true,
99   - uniqueness: true,
  79 + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
  80 + validates :code, presence: true, uniqueness: true, length: { within: 1..255 },
100 81 format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/,
101   - message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" },
102   - length: { within: 1..255 }
103   -
104   - validates :owner, presence: true
  82 + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
105 83 validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
106 84 :wiki_enabled, inclusion: { in: [true, false] }
107   - validate :check_limit
108   - validate :repo_name
  85 + validate :check_limit, :repo_name
109 86  
110 87 def check_limit
111 88 unless owner.can_create_project?
... ... @@ -197,4 +174,3 @@ end
197 174 # merge_requests_enabled :boolean default(TRUE), not null
198 175 # wiki_enabled :boolean default(TRUE), not null
199 176 #
200   -
... ...
app/models/project_hook.rb
  1 +class ProjectHook < WebHook
  2 + belongs_to :project
  3 +end
  4 +
1 5 # == Schema Information
2 6 #
3 7 # Table name: web_hooks
... ... @@ -9,7 +13,3 @@
9 13 # updated_at :datetime not null
10 14 # type :string(255) default("ProjectHook")
11 15 #
12   -
13   -class ProjectHook < WebHook
14   - belongs_to :project
15   -end
... ...
app/models/protected_branch.rb
... ... @@ -4,8 +4,7 @@ class ProtectedBranch &lt; ActiveRecord::Base
4 4 attr_accessible :name
5 5  
6 6 belongs_to :project
7   - validates_presence_of :project_id
8   - validates_presence_of :name
  7 + validates_presence_of :name, :project_id
9 8  
10 9 after_save :update_repository
11 10 after_destroy :update_repository
... ... @@ -18,6 +17,7 @@ class ProtectedBranch &lt; ActiveRecord::Base
18 17 project.commit(self.name)
19 18 end
20 19 end
  20 +
21 21 # == Schema Information
22 22 #
23 23 # Table name: protected_branches
... ... @@ -28,4 +28,3 @@ end
28 28 # created_at :datetime not null
29 29 # updated_at :datetime not null
30 30 #
31   -
... ...
app/models/snippet.rb
... ... @@ -7,25 +7,12 @@ class Snippet &lt; ActiveRecord::Base
7 7 belongs_to :author, class_name: "User"
8 8 has_many :notes, as: :noteable, dependent: :destroy
9 9  
10   - delegate :name,
11   - :email,
12   - to: :author,
13   - prefix: true
  10 + delegate :name, :email, to: :author, prefix: true
14 11  
15   - validates_presence_of :project_id
16   - validates_presence_of :author_id
17   -
18   - validates :title,
19   - presence: true,
20   - length: { within: 0..255 }
21   -
22   - validates :file_name,
23   - presence: true,
24   - length: { within: 0..255 }
25   -
26   - validates :content,
27   - presence: true,
28   - length: { within: 0..10000 }
  12 + validates_presence_of :author_id, :project_id
  13 + validates :title, presence: true, length: { within: 0..255 }
  14 + validates :file_name, presence: true, length: { within: 0..255 }
  15 + validates :content, presence: true, length: { within: 0..10000 }
29 16  
30 17 scope :fresh, order("created_at DESC")
31 18 scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current])
... ... @@ -59,6 +46,7 @@ class Snippet &lt; ActiveRecord::Base
59 46 expires_at && expires_at < Time.current
60 47 end
61 48 end
  49 +
62 50 # == Schema Information
63 51 #
64 52 # Table name: snippets
... ... @@ -73,4 +61,3 @@ end
73 61 # file_name :string(255)
74 62 # expires_at :datetime
75 63 #
76   -
... ...
app/models/system_hook.rb
1   -# == Schema Information
2   -#
3   -# Table name: web_hooks
4   -#
5   -# id :integer not null, primary key
6   -# url :string(255)
7   -# project_id :integer
8   -# created_at :datetime not null
9   -# updated_at :datetime not null
10   -# type :string(255) default("ProjectHook")
11   -#
12   -
13 1 class SystemHook < WebHook
14   -
15 2 def async_execute(data)
16 3 Resque.enqueue(SystemHookWorker, id, data)
17 4 end
... ... @@ -21,5 +8,16 @@ class SystemHook &lt; WebHook
21 8 sh.async_execute data
22 9 end
23 10 end
24   -
25 11 end
  12 +
  13 +# == Schema Information
  14 +#
  15 +# Table name: web_hooks
  16 +#
  17 +# id :integer not null, primary key
  18 +# url :string(255)
  19 +# project_id :integer
  20 +# created_at :datetime not null
  21 +# updated_at :datetime not null
  22 +# type :string(255) default("ProjectHook")
  23 +#
... ...
app/models/tree.rb
... ... @@ -2,16 +2,8 @@ class Tree
2 2 include Linguist::BlobHelper
3 3 attr_accessor :path, :tree, :project, :ref
4 4  
5   - delegate :contents,
6   - :basename,
7   - :name,
8   - :data,
9   - :mime_type,
10   - :mode,
11   - :size,
12   - :text?,
13   - :colorize,
14   - to: :tree
  5 + delegate :contents, :basename, :name, :data, :mime_type,
  6 + :mode, :size, :text?, :colorize, to: :tree
15 7  
16 8 def initialize(raw_tree, project, ref = nil, path = nil)
17 9 @project, @ref, @path = project, ref, path
... ...
app/models/user.rb
1 1 class User < ActiveRecord::Base
2   -
3 2 include Account
4 3  
5 4 devise :database_authenticatable, :token_authenticatable, :lockable,
6 5 :recoverable, :rememberable, :trackable, :validatable, :omniauthable
7 6  
8   - attr_accessible :email, :password, :password_confirmation, :remember_me, :bio,
9   - :name, :skype, :linkedin, :twitter, :dark_scheme,
10   - :theme_id, :force_random_password, :extern_uid, :provider, :as => [:default, :admin]
  7 + attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, :name,
  8 + :skype, :linkedin, :twitter, :dark_scheme, :theme_id, :force_random_password,
  9 + :extern_uid, :provider, :as => [:default, :admin]
11 10 attr_accessible :projects_limit, :as => :admin
12 11  
13 12 attr_accessor :force_random_password
14 13  
15   - has_many :users_projects, dependent: :destroy
  14 + has_many :keys, dependent: :destroy
16 15 has_many :projects, through: :users_projects
  16 + has_many :users_projects, dependent: :destroy
  17 + has_many :issues, foreign_key: :author_id, dependent: :destroy
  18 + has_many :notes, foreign_key: :author_id, dependent: :destroy
  19 + has_many :merge_requests, foreign_key: :author_id, dependent: :destroy
17 20 has_many :my_own_projects, class_name: "Project", foreign_key: :owner_id
18   - has_many :keys, dependent: :destroy
19   -
20   - has_many :events,
21   - class_name: "Event",
22   - foreign_key: :author_id,
23   - dependent: :destroy
24   -
25   - has_many :recent_events,
26   - class_name: "Event",
27   - foreign_key: :author_id,
28   - order: "id DESC"
29   -
30   - has_many :issues,
31   - foreign_key: :author_id,
32   - dependent: :destroy
33   -
34   - has_many :notes,
35   - foreign_key: :author_id,
36   - dependent: :destroy
37   -
38   - has_many :assigned_issues,
39   - class_name: "Issue",
40   - foreign_key: :assignee_id,
41   - dependent: :destroy
42   -
43   - has_many :merge_requests,
44   - foreign_key: :author_id,
45   - dependent: :destroy
46   -
47   - has_many :assigned_merge_requests,
48   - class_name: "MergeRequest",
49   - foreign_key: :assignee_id,
50   - dependent: :destroy
51   -
52   - validates :projects_limit,
53   - presence: true,
54   - numericality: {greater_than_or_equal_to: 0}
  21 + has_many :events, class_name: "Event", foreign_key: :author_id, dependent: :destroy
  22 + has_many :recent_events, class_name: "Event", foreign_key: :author_id, order: "id DESC"
  23 + has_many :assigned_issues, class_name: "Issue", foreign_key: :assignee_id, dependent: :destroy
  24 + has_many :assigned_merge_requests, class_name: "MergeRequest", foreign_key: :assignee_id, dependent: :destroy
55 25  
56 26 validates :bio, length: { within: 0..255 }
57   -
58 27 validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider}
59   -
60   - before_save :ensure_authentication_token
61   - alias_attribute :private_token, :authentication_token
  28 + validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
62 29  
63 30 scope :not_in_project, lambda { |project| where("id not in (:ids)", ids: project.users.map(&:id) ) }
64 31 scope :admins, where(admin: true)
... ... @@ -66,6 +33,8 @@ class User &lt; ActiveRecord::Base
66 33 scope :active, where(blocked: false)
67 34  
68 35 before_validation :generate_password, on: :create
  36 + before_save :ensure_authentication_token
  37 + alias_attribute :private_token, :authentication_token
69 38  
70 39 def generate_password
71 40 if self.force_random_password
... ... @@ -107,6 +76,7 @@ class User &lt; ActiveRecord::Base
107 76 where("name like :query or email like :query", query: "%#{query}%")
108 77 end
109 78 end
  79 +
110 80 # == Schema Information
111 81 #
112 82 # Table name: users
... ... @@ -140,4 +110,3 @@ end
140 110 # extern_uid :string(255)
141 111 # provider :string(255)
142 112 #
143   -
... ...
app/models/users_project.rb
... ... @@ -88,6 +88,7 @@ class UsersProject &lt; ActiveRecord::Base
88 88 self.class.access_roles.invert[self.project_access]
89 89 end
90 90 end
  91 +
91 92 # == Schema Information
92 93 #
93 94 # Table name: users_projects
... ... @@ -99,4 +100,3 @@ end
99 100 # updated_at :datetime not null
100 101 # project_access :integer default(0), not null
101 102 #
102   -
... ...
app/models/web_hook.rb
... ... @@ -6,26 +6,23 @@ class WebHook &lt; ActiveRecord::Base
6 6 # HTTParty timeout
7 7 default_timeout 10
8 8  
9   - validates :url,
10   - presence: true,
11   - format: {
12   - with: URI::regexp(%w(http https)),
13   - message: "should be a valid url" }
  9 + validates :url, presence: true,
  10 + format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
14 11  
15 12 def execute(data)
16 13 parsed_url = URI.parse(url)
17 14 if parsed_url.userinfo.blank?
18 15 WebHook.post(url, body: data.to_json, headers: { "Content-Type" => "application/json" })
19 16 else
20   - post_url = url.gsub(parsed_url.userinfo+"@", "")
  17 + post_url = url.gsub("#{parsed_url.userinfo}@", "")
21 18 WebHook.post(post_url,
22 19 body: data.to_json,
23   - headers: { "Content-Type" => "application/json" },
  20 + headers: {"Content-Type" => "application/json"},
24 21 basic_auth: {username: parsed_url.user, password: parsed_url.password})
25 22 end
26 23 end
27   -
28 24 end
  25 +
29 26 # == Schema Information
30 27 #
31 28 # Table name: web_hooks
... ... @@ -37,4 +34,3 @@ end
37 34 # updated_at :datetime not null
38 35 # type :string(255) default("ProjectHook")
39 36 #
40   -
... ...
app/models/wiki.rb
... ... @@ -32,6 +32,7 @@ class Wiki &lt; ActiveRecord::Base
32 32 end
33 33 end
34 34 end
  35 +
35 36 # == Schema Information
36 37 #
37 38 # Table name: wikis
... ... @@ -45,4 +46,3 @@ end
45 46 # slug :string(255)
46 47 # user_id :integer
47 48 #
48   -
... ...