Commit df7c52489a890ac38491682b4a3399beb7cdbc86

Authored by Andrey Kumanyaev
1 parent a635b9da

simple refactoring

app/helpers/commits_helper.rb
@@ -17,7 +17,7 @@ module CommitsHelper @@ -17,7 +17,7 @@ module CommitsHelper
17 line_old = 1 17 line_old = 1
18 line_new = 1 18 line_new = 1
19 type = nil 19 type = nil
20 - 20 +
21 lines_arr = ::Gitlab::InlineDiff.processing diff_arr 21 lines_arr = ::Gitlab::InlineDiff.processing diff_arr
22 lines_arr.each do |line| 22 lines_arr.each do |line|
23 next if line.match(/^\-\-\- \/dev\/null/) 23 next if line.match(/^\-\-\- \/dev\/null/)
app/helpers/merge_requests_helper.rb
@@ -24,7 +24,7 @@ module MergeRequestsHelper @@ -24,7 +24,7 @@ module MergeRequestsHelper
24 def new_mr_path_from_push_event(event) 24 def new_mr_path_from_push_event(event)
25 new_project_merge_request_path( 25 new_project_merge_request_path(
26 event.project, 26 event.project,
27 - merge_request: { 27 + merge_request: {
28 source_branch: event.branch_name, 28 source_branch: event.branch_name,
29 target_branch: event.project.root_ref, 29 target_branch: event.project.root_ref,
30 title: event.branch_name.titleize 30 title: event.branch_name.titleize
app/models/ability.rb
1 class Ability 1 class Ability
2 - def self.allowed(object, subject)  
3 - case subject.class.name  
4 - when "Project" then project_abilities(object, subject)  
5 - when "Issue" then issue_abilities(object, subject)  
6 - when "Note" then note_abilities(object, subject)  
7 - when "Snippet" then snippet_abilities(object, subject)  
8 - when "MergeRequest" then merge_request_abilities(object, subject)  
9 - else [] 2 + class << self
  3 + def allowed(object, subject)
  4 + case subject.class.name
  5 + when "Project" then project_abilities(object, subject)
  6 + when "Issue" then issue_abilities(object, subject)
  7 + when "Note" then note_abilities(object, subject)
  8 + when "Snippet" then snippet_abilities(object, subject)
  9 + when "MergeRequest" then merge_request_abilities(object, subject)
  10 + else []
  11 + end
10 end 12 end
11 - end  
12 13
13 - def self.project_abilities(user, project)  
14 - rules = [] 14 + def project_abilities(user, project)
  15 + rules = []
15 16
16 - rules << [  
17 - :read_project,  
18 - :read_wiki,  
19 - :read_issue,  
20 - :read_milestone,  
21 - :read_snippet,  
22 - :read_team_member,  
23 - :read_merge_request,  
24 - :read_note,  
25 - :write_project,  
26 - :write_issue,  
27 - :write_note  
28 - ] if project.guest_access_for?(user) 17 + rules << [
  18 + :read_project,
  19 + :read_wiki,
  20 + :read_issue,
  21 + :read_milestone,
  22 + :read_snippet,
  23 + :read_team_member,
  24 + :read_merge_request,
  25 + :read_note,
  26 + :write_project,
  27 + :write_issue,
  28 + :write_note
  29 + ] if project.guest_access_for?(user)
29 30
30 - rules << [  
31 - :download_code,  
32 - :write_merge_request,  
33 - :write_snippet  
34 - ] if project.report_access_for?(user) 31 + rules << [
  32 + :download_code,
  33 + :write_merge_request,
  34 + :write_snippet
  35 + ] if project.report_access_for?(user)
35 36
36 - rules << [  
37 - :write_wiki  
38 - ] if project.dev_access_for?(user) 37 + rules << [
  38 + :write_wiki
  39 + ] if project.dev_access_for?(user)
39 40
40 - rules << [  
41 - :modify_issue,  
42 - :modify_snippet,  
43 - :modify_merge_request,  
44 - :admin_project,  
45 - :admin_issue,  
46 - :admin_milestone,  
47 - :admin_snippet,  
48 - :admin_team_member,  
49 - :admin_merge_request,  
50 - :admin_note,  
51 - :accept_mr,  
52 - :admin_wiki  
53 - ] if project.master_access_for?(user) || project.owner == user 41 + rules << [
  42 + :modify_issue,
  43 + :modify_snippet,
  44 + :modify_merge_request,
  45 + :admin_project,
  46 + :admin_issue,
  47 + :admin_milestone,
  48 + :admin_snippet,
  49 + :admin_team_member,
  50 + :admin_merge_request,
  51 + :admin_note,
  52 + :accept_mr,
  53 + :admin_wiki
  54 + ] if project.master_access_for?(user) || project.owner == user
54 55
55 - rules.flatten  
56 - end 56 + rules.flatten
  57 + end
57 58
58 - class << self  
59 [:issue, :note, :snippet, :merge_request].each do |name| 59 [:issue, :note, :snippet, :merge_request].each do |name|
60 define_method "#{name}_abilities" do |user, subject| 60 define_method "#{name}_abilities" do |user, subject|
61 if subject.author == user 61 if subject.author == user
@@ -72,8 +72,7 @@ class Ability @@ -72,8 +72,7 @@ class Ability
72 :"modify_#{name}", 72 :"modify_#{name}",
73 ] 73 ]
74 else 74 else
75 - subject.respond_to?(:project) ?  
76 - project_abilities(user, subject.project) : [] 75 + subject.respond_to?(:project) ? project_abilities(user, subject.project) : []
77 end 76 end
78 end 77 end
79 end 78 end
app/models/event.rb
@@ -27,19 +27,22 @@ class Event &lt; ActiveRecord::Base @@ -27,19 +27,22 @@ class Event &lt; ActiveRecord::Base
27 # For Hash only 27 # For Hash only
28 serialize :data 28 serialize :data
29 29
  30 + # Scopes
30 scope :recent, order("created_at DESC") 31 scope :recent, order("created_at DESC")
31 scope :code_push, where(action: Pushed) 32 scope :code_push, where(action: Pushed)
32 33
33 - def self.determine_action(record)  
34 - if [Issue, MergeRequest].include? record.class  
35 - Event::Created  
36 - elsif record.kind_of? Note  
37 - Event::Commented 34 + class << self
  35 + def determine_action(record)
  36 + if [Issue, MergeRequest].include? record.class
  37 + Event::Created
  38 + elsif record.kind_of? Note
  39 + Event::Commented
  40 + end
38 end 41 end
39 - end  
40 42
41 - def self.recent_for_user user  
42 - where(project_id: user.projects.map(&:id)).recent 43 + def recent_for_user user
  44 + where(project_id: user.projects.map(&:id)).recent
  45 + end
43 end 46 end
44 47
45 # Next events currently enabled for system 48 # Next events currently enabled for system
app/models/group.rb
1 -# == Schema Information  
2 -#  
3 -# Table name: groups  
4 -#  
5 -# id :integer not null, primary key  
6 -# name :string(255) not null  
7 -# code :string(255) not null  
8 -# owner_id :integer not null  
9 -# created_at :datetime not null  
10 -# updated_at :datetime not null  
11 -#  
12 -  
13 class Group < ActiveRecord::Base 1 class Group < ActiveRecord::Base
14 attr_accessible :code, :name, :owner_id 2 attr_accessible :code, :name, :owner_id
15 3
@@ -18,7 +6,7 @@ class Group &lt; ActiveRecord::Base @@ -18,7 +6,7 @@ class Group &lt; ActiveRecord::Base
18 6
19 validates :name, presence: true, uniqueness: true 7 validates :name, presence: true, uniqueness: true
20 validates :code, presence: true, uniqueness: true 8 validates :code, presence: true, uniqueness: true
21 - validates :owner_id, presence: true 9 + validates :owner, presence: true
22 10
23 delegate :name, to: :owner, allow_nil: true, prefix: true 11 delegate :name, to: :owner, allow_nil: true, prefix: true
24 12
@@ -31,6 +19,18 @@ class Group &lt; ActiveRecord::Base @@ -31,6 +19,18 @@ class Group &lt; ActiveRecord::Base
31 end 19 end
32 20
33 def users 21 def users
34 - User.joins(:users_projects).where('users_projects.project_id' => project_ids).uniq 22 + User.joins(:users_projects).where(users_projects: {project_id: project_ids}).uniq
35 end 23 end
36 end 24 end
  25 +
  26 +# == Schema Information
  27 +#
  28 +# Table name: groups
  29 +#
  30 +# id :integer not null, primary key
  31 +# name :string(255) not null
  32 +# code :string(255) not null
  33 +# owner_id :integer not null
  34 +# created_at :datetime not null
  35 +# updated_at :datetime not null
  36 +#
app/models/key.rb
@@ -6,15 +6,14 @@ class Key &lt; ActiveRecord::Base @@ -6,15 +6,14 @@ class Key &lt; ActiveRecord::Base
6 6
7 attr_accessible :key, :title 7 attr_accessible :key, :title
8 8
  9 + before_validation :strip_white_space
  10 + before_save :set_identifier
  11 +
9 validates :title, presence: true, length: { within: 0..255 } 12 validates :title, presence: true, length: { within: 0..255 }
10 - validates :key, presence: true,  
11 - length: { within: 0..5000 },  
12 - format: { :with => /ssh-.{3} / } 13 + validates :key, presence: true, length: { within: 0..5000 }, format: { :with => /ssh-.{3} / }
  14 + validate :unique_key, :fingerprintable_key
13 15
14 - before_save :set_identifier  
15 - before_validation :strip_white_space  
16 delegate :name, :email, to: :user, prefix: true 16 delegate :name, :email, to: :user, prefix: true
17 - validate :unique_key, :fingerprintable_key  
18 17
19 def strip_white_space 18 def strip_white_space
20 self.key = self.key.strip unless self.key.blank? 19 self.key = self.key.strip unless self.key.blank?
app/models/merge_request.rb
@@ -7,6 +7,8 @@ class MergeRequest &lt; ActiveRecord::Base @@ -7,6 +7,8 @@ class MergeRequest &lt; ActiveRecord::Base
7 attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch, 7 attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch,
8 :author_id_of_changes 8 :author_id_of_changes
9 9
  10 + attr_accessor :should_remove_source_branch
  11 +
10 BROKEN_DIFF = "--broken-diff" 12 BROKEN_DIFF = "--broken-diff"
11 13
12 UNCHECKED = 1 14 UNCHECKED = 1
@@ -16,9 +18,8 @@ class MergeRequest &lt; ActiveRecord::Base @@ -16,9 +18,8 @@ class MergeRequest &lt; ActiveRecord::Base
16 serialize :st_commits 18 serialize :st_commits
17 serialize :st_diffs 19 serialize :st_diffs
18 20
19 - attr_accessor :should_remove_source_branch  
20 -  
21 - validates_presence_of :source_branch, :target_branch 21 + validates :source_branch, presence: true
  22 + validates :target_branch, presence: true
22 validate :validate_branches 23 validate :validate_branches
23 24
24 def self.find_all_by_branch(branch_name) 25 def self.find_all_by_branch(branch_name)
app/models/milestone.rb
@@ -4,7 +4,8 @@ class Milestone &lt; ActiveRecord::Base @@ -4,7 +4,8 @@ class Milestone &lt; ActiveRecord::Base
4 belongs_to :project 4 belongs_to :project
5 has_many :issues 5 has_many :issues
6 6
7 - validates_presence_of :title, :project_id 7 + validates :title, presence: true
  8 + validates :project, presence: true
8 9
9 def self.active 10 def self.active
10 where("due_date > ? OR due_date IS NULL", Date.today) 11 where("due_date > ? OR due_date IS NULL", Date.today)
app/models/note.rb
@@ -2,10 +2,13 @@ require &#39;carrierwave/orm/activerecord&#39; @@ -2,10 +2,13 @@ require &#39;carrierwave/orm/activerecord&#39;
2 require 'file_size_validator' 2 require 'file_size_validator'
3 3
4 class Note < ActiveRecord::Base 4 class Note < ActiveRecord::Base
5 - mount_uploader :attachment, AttachmentUploader 5 +
6 attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id, 6 attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id,
7 :attachment, :line_code 7 :attachment, :line_code
8 8
  9 + attr_accessor :notify
  10 + attr_accessor :notify_author
  11 +
9 belongs_to :project 12 belongs_to :project
10 belongs_to :noteable, polymorphic: true 13 belongs_to :noteable, polymorphic: true
11 belongs_to :author, class_name: "User" 14 belongs_to :author, class_name: "User"
@@ -13,18 +16,17 @@ class Note &lt; ActiveRecord::Base @@ -13,18 +16,17 @@ class Note &lt; ActiveRecord::Base
13 delegate :name, to: :project, prefix: true 16 delegate :name, to: :project, prefix: true
14 delegate :name, :email, to: :author, prefix: true 17 delegate :name, :email, to: :author, prefix: true
15 18
16 - attr_accessor :notify  
17 - attr_accessor :notify_author  
18 -  
19 - validates_presence_of :project  
20 - 19 + validates :project, presence: true
21 validates :note, presence: true, length: { within: 0..5000 } 20 validates :note, presence: true, length: { within: 0..5000 }
22 validates :attachment, file_size: { maximum: 10.megabytes.to_i } 21 validates :attachment, file_size: { maximum: 10.megabytes.to_i }
23 22
  23 + mount_uploader :attachment, AttachmentUploader
  24 +
  25 + # Scopes
24 scope :common, where(noteable_id: nil) 26 scope :common, where(noteable_id: nil)
25 scope :today, where("created_at >= :date", date: Date.today) 27 scope :today, where("created_at >= :date", date: Date.today)
26 scope :last_week, where("created_at >= :date", date: (Date.today - 7.days)) 28 scope :last_week, where("created_at >= :date", date: (Date.today - 7.days))
27 - scope :since, lambda { |day| where("created_at >= :date", date: (day)) } 29 + scope :since, ->(day) { where("created_at >= :date", date: (day)) }
28 scope :fresh, order("created_at ASC, id ASC") 30 scope :fresh, order("created_at ASC, id ASC")
29 scope :inc_author_project, includes(:project, :author) 31 scope :inc_author_project, includes(:project, :author)
30 scope :inc_author, includes(:author) 32 scope :inc_author, includes(:author)
app/models/project.rb
@@ -28,42 +28,62 @@ class Project &lt; ActiveRecord::Base @@ -28,42 +28,62 @@ class Project &lt; ActiveRecord::Base
28 28
29 delegate :name, to: :owner, allow_nil: true, prefix: true 29 delegate :name, to: :owner, allow_nil: true, prefix: true
30 30
  31 + # Validations
  32 + validates :owner, presence: true
  33 + validates :description, length: { within: 0..2000 }
  34 + validates :name, uniqueness: true, presence: true, length: { within: 0..255 }
  35 + validates :path, uniqueness: true, presence: true, length: { within: 0..255 },
  36 + format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
  37 + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
  38 + validates :code, presence: true, uniqueness: true, length: { within: 1..255 },
  39 + format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
  40 + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
  41 + validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
  42 + :wiki_enabled, inclusion: { in: [true, false] }
  43 + validate :check_limit, :repo_name
  44 +
31 # Scopes 45 # Scopes
32 scope :public_only, where(private_flag: false) 46 scope :public_only, where(private_flag: false)
33 scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) } 47 scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) }
34 scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) } 48 scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }
35 49
36 - def self.active  
37 - joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")  
38 - end  
39 -  
40 - def self.search query  
41 - where("name LIKE :query OR code LIKE :query OR path LIKE :query", query: "%#{query}%")  
42 - end  
43 -  
44 - def self.create_by_user(params, user)  
45 - project = Project.new params  
46 -  
47 - Project.transaction do  
48 - project.owner = user  
49 - project.save! 50 + class << self
  51 + def active
  52 + joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
  53 + end
50 54
51 - # Add user as project master  
52 - project.users_projects.create!(project_access: UsersProject::MASTER, user: user) 55 + def search query
  56 + where("name LIKE :query OR code LIKE :query OR path LIKE :query", query: "%#{query}%")
  57 + end
53 58
54 - # when project saved no team member exist so  
55 - # project repository should be updated after first user add  
56 - project.update_repository 59 + def create_by_user(params, user)
  60 + project = Project.new params
  61 +
  62 + Project.transaction do
  63 + project.owner = user
  64 + project.save!
  65 +
  66 + # Add user as project master
  67 + project.users_projects.create!(project_access: UsersProject::MASTER, user: user)
  68 +
  69 + # when project saved no team member exist so
  70 + # project repository should be updated after first user add
  71 + project.update_repository
  72 + end
  73 +
  74 + project
  75 + rescue Gitlab::Gitolite::AccessDenied => ex
  76 + project.error_code = :gitolite
  77 + project
  78 + rescue => ex
  79 + project.error_code = :db
  80 + project.errors.add(:base, "Can't save project. Please try again later")
  81 + project
57 end 82 end
58 83
59 - project  
60 - rescue Gitlab::Gitolite::AccessDenied => ex  
61 - project.error_code = :gitolite  
62 - project  
63 - rescue => ex  
64 - project.error_code = :db  
65 - project.errors.add(:base, "Can't save project. Please try again later")  
66 - project 84 + def access_options
  85 + UsersProject.access_roles
  86 + end
67 end 87 end
68 88
69 def git_error? 89 def git_error?
@@ -74,20 +94,6 @@ class Project &lt; ActiveRecord::Base @@ -74,20 +94,6 @@ class Project &lt; ActiveRecord::Base
74 id && valid? 94 id && valid?
75 end 95 end
76 96
77 - # Validations  
78 - validates :owner, presence: true  
79 - validates :description, length: { within: 0..2000 }  
80 - validates :name, uniqueness: true, presence: true, length: { within: 0..255 }  
81 - validates :path, uniqueness: true, presence: true, length: { within: 0..255 },  
82 - format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,  
83 - message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }  
84 - validates :code, presence: true, uniqueness: true, length: { within: 1..255 },  
85 - format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,  
86 - message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }  
87 - validates :issues_enabled, :wall_enabled, :merge_requests_enabled,  
88 - :wiki_enabled, inclusion: { in: [true, false] }  
89 - validate :check_limit, :repo_name  
90 -  
91 def check_limit 97 def check_limit
92 unless owner.can_create_project? 98 unless owner.can_create_project?
93 errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") 99 errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
@@ -102,10 +108,6 @@ class Project &lt; ActiveRecord::Base @@ -102,10 +108,6 @@ class Project &lt; ActiveRecord::Base
102 end 108 end
103 end 109 end
104 110
105 - def self.access_options  
106 - UsersProject.access_roles  
107 - end  
108 -  
109 def to_param 111 def to_param
110 code 112 code
111 end 113 end
app/models/protected_branch.rb
@@ -4,7 +4,8 @@ class ProtectedBranch &lt; ActiveRecord::Base @@ -4,7 +4,8 @@ class ProtectedBranch &lt; ActiveRecord::Base
4 attr_accessible :name 4 attr_accessible :name
5 5
6 belongs_to :project 6 belongs_to :project
7 - validates_presence_of :name, :project_id 7 + validates :name, presence: true
  8 + validates :project, presence: true
8 9
9 after_save :update_repository 10 after_save :update_repository
10 after_destroy :update_repository 11 after_destroy :update_repository
app/models/snippet.rb
@@ -9,11 +9,13 @@ class Snippet &lt; ActiveRecord::Base @@ -9,11 +9,13 @@ class Snippet &lt; ActiveRecord::Base
9 9
10 delegate :name, :email, to: :author, prefix: true 10 delegate :name, :email, to: :author, prefix: true
11 11
12 - validates_presence_of :author_id, :project_id 12 + validates :author, presence: true
  13 + validates :project, presence: true
13 validates :title, presence: true, length: { within: 0..255 } 14 validates :title, presence: true, length: { within: 0..255 }
14 validates :file_name, presence: true, length: { within: 0..255 } 15 validates :file_name, presence: true, length: { within: 0..255 }
15 validates :content, presence: true, length: { within: 0..10000 } 16 validates :content, presence: true, length: { within: 0..10000 }
16 17
  18 + # Scopes
17 scope :fresh, order("created_at DESC") 19 scope :fresh, order("created_at DESC")
18 scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current]) 20 scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current])
19 scope :expired, where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) 21 scope :expired, where(["expires_at IS NOT NULL AND expires_at < ?", Time.current])
app/models/system_hook.rb
1 class SystemHook < WebHook 1 class SystemHook < WebHook
2 - def async_execute(data)  
3 - Resque.enqueue(SystemHookWorker, id, data)  
4 - end  
5 -  
6 def self.all_hooks_fire(data) 2 def self.all_hooks_fire(data)
7 SystemHook.all.each do |sh| 3 SystemHook.all.each do |sh|
8 sh.async_execute data 4 sh.async_execute data
9 end 5 end
10 end 6 end
  7 +
  8 + def async_execute(data)
  9 + Resque.enqueue(SystemHookWorker, id, data)
  10 + end
11 end 11 end
12 12
13 # == Schema Information 13 # == Schema Information
app/models/user.rb
@@ -27,53 +27,56 @@ class User &lt; ActiveRecord::Base @@ -27,53 +27,56 @@ class User &lt; ActiveRecord::Base
27 validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider} 27 validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider}
28 validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0} 28 validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
29 29
30 - scope :not_in_project, lambda { |project| where("id not in (:ids)", ids: project.users.map(&:id) ) }  
31 - scope :admins, where(admin: true)  
32 - scope :blocked, where(blocked: true)  
33 - scope :active, where(blocked: false)  
34 -  
35 before_validation :generate_password, on: :create 30 before_validation :generate_password, on: :create
36 before_save :ensure_authentication_token 31 before_save :ensure_authentication_token
37 alias_attribute :private_token, :authentication_token 32 alias_attribute :private_token, :authentication_token
38 33
39 - def generate_password  
40 - if self.force_random_password  
41 - self.password = self.password_confirmation = Devise.friendly_token.first(8) 34 + # Scopes
  35 + scope :not_in_project, ->(project) { where("id not in (:ids)", ids: project.users.map(&:id) ) }
  36 + scope :admins, where(admin: true)
  37 + scope :blocked, where(blocked: true)
  38 + scope :active, where(blocked: false)
  39 +
  40 + class << self
  41 + def filter filter_name
  42 + case filter_name
  43 + when "admins"; self.admins
  44 + when "blocked"; self.blocked
  45 + when "wop"; self.without_projects
  46 + else
  47 + self.active
  48 + end
42 end 49 end
43 - end  
44 50
45 - def self.filter filter_name  
46 - case filter_name  
47 - when "admins"; self.admins  
48 - when "blocked"; self.blocked  
49 - when "wop"; self.without_projects  
50 - else  
51 - self.active 51 + def without_projects
  52 + where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')
52 end 53 end
53 - end  
54 54
55 - def self.without_projects  
56 - where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')  
57 - end 55 + def create_from_omniauth(auth, ldap = false)
  56 + gitlab_auth.create_from_omniauth(auth, ldap)
  57 + end
58 58
59 - def self.create_from_omniauth(auth, ldap = false)  
60 - gitlab_auth.create_from_omniauth(auth, ldap)  
61 - end 59 + def find_or_new_for_omniauth(auth)
  60 + gitlab_auth.find_or_new_for_omniauth(auth)
  61 + end
62 62
63 - def self.find_or_new_for_omniauth(auth)  
64 - gitlab_auth.find_or_new_for_omniauth(auth)  
65 - end 63 + def find_for_ldap_auth(auth, signed_in_resource = nil)
  64 + gitlab_auth.find_for_ldap_auth(auth, signed_in_resource)
  65 + end
66 66
67 - def self.find_for_ldap_auth(auth, signed_in_resource = nil)  
68 - gitlab_auth.find_for_ldap_auth(auth, signed_in_resource)  
69 - end 67 + def gitlab_auth
  68 + Gitlab::Auth.new
  69 + end
70 70
71 - def self.gitlab_auth  
72 - Gitlab::Auth.new 71 + def search query
  72 + where("name LIKE :query or email LIKE :query", query: "%#{query}%")
  73 + end
73 end 74 end
74 75
75 - def self.search query  
76 - where("name LIKE :query OR email LIKE :query", query: "%#{query}%") 76 + def generate_password
  77 + if self.force_random_password
  78 + self.password = self.password_confirmation = Devise.friendly_token.first(8)
  79 + end
77 end 80 end
78 end 81 end
79 82
app/models/users_project.rb
@@ -14,62 +14,64 @@ class UsersProject &lt; ActiveRecord::Base @@ -14,62 +14,64 @@ class UsersProject &lt; ActiveRecord::Base
14 after_save :update_repository 14 after_save :update_repository
15 after_destroy :update_repository 15 after_destroy :update_repository
16 16
17 - validates_uniqueness_of :user_id, scope: [:project_id], message: "already exists in project"  
18 - validates_presence_of :user_id  
19 - validates_presence_of :project_id 17 + validates :user, presence: true
  18 + validates :user_id, uniqueness: { :scope => [:project_id], message: "already exists in project" }
  19 + validates :project, presence: true
20 20
21 delegate :name, :email, to: :user, prefix: true 21 delegate :name, :email, to: :user, prefix: true
22 22
23 - def self.bulk_delete(project, user_ids)  
24 - UsersProject.transaction do  
25 - UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|  
26 - users_project.destroy 23 + class << self
  24 + def bulk_delete(project, user_ids)
  25 + UsersProject.transaction do
  26 + UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
  27 + users_project.destroy
  28 + end
27 end 29 end
28 end 30 end
29 - end  
30 31
31 - def self.bulk_update(project, user_ids, project_access)  
32 - UsersProject.transaction do  
33 - UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|  
34 - users_project.project_access = project_access  
35 - users_project.save 32 + def bulk_update(project, user_ids, project_access)
  33 + UsersProject.transaction do
  34 + UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
  35 + users_project.project_access = project_access
  36 + users_project.save
  37 + end
36 end 38 end
37 end 39 end
38 - end  
39 40
40 - def self.bulk_import(project, user_ids, project_access)  
41 - UsersProject.transaction do  
42 - user_ids.each do |user_id|  
43 - users_project = UsersProject.new(  
44 - project_access: project_access,  
45 - user_id: user_id  
46 - )  
47 - users_project.project = project  
48 - users_project.save 41 + def bulk_import(project, user_ids, project_access)
  42 + UsersProject.transaction do
  43 + user_ids.each do |user_id|
  44 + users_project = UsersProject.new(
  45 + project_access: project_access,
  46 + user_id: user_id
  47 + )
  48 + users_project.project = project
  49 + users_project.save
  50 + end
49 end 51 end
50 end 52 end
51 - end  
52 53
53 - def self.user_bulk_import(user, project_ids, project_access)  
54 - UsersProject.transaction do  
55 - project_ids.each do |project_id|  
56 - users_project = UsersProject.new(  
57 - project_access: project_access,  
58 - )  
59 - users_project.project_id = project_id  
60 - users_project.user_id = user.id  
61 - users_project.save 54 + def user_bulk_import(user, project_ids, project_access)
  55 + UsersProject.transaction do
  56 + project_ids.each do |project_id|
  57 + users_project = UsersProject.new(
  58 + project_access: project_access,
  59 + )
  60 + users_project.project_id = project_id
  61 + users_project.user_id = user.id
  62 + users_project.save
  63 + end
62 end 64 end
63 end 65 end
64 - end  
65 66
66 - def self.access_roles  
67 - {  
68 - "Guest" => GUEST,  
69 - "Reporter" => REPORTER,  
70 - "Developer" => DEVELOPER,  
71 - "Master" => MASTER  
72 - } 67 + def access_roles
  68 + {
  69 + "Guest" => GUEST,
  70 + "Reporter" => REPORTER,
  71 + "Developer" => DEVELOPER,
  72 + "Master" => MASTER
  73 + }
  74 + end
73 end 75 end
74 76
75 def role_access 77 def role_access
app/models/web_hook.rb
@@ -7,7 +7,7 @@ class WebHook &lt; ActiveRecord::Base @@ -7,7 +7,7 @@ class WebHook &lt; ActiveRecord::Base
7 default_timeout 10 7 default_timeout 10
8 8
9 validates :url, presence: true, 9 validates :url, presence: true,
10 - format: { with: URI::regexp(%w(http https)), message: "should be a valid url" } 10 + format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
11 11
12 def execute(data) 12 def execute(data)
13 parsed_url = URI.parse(url) 13 parsed_url = URI.parse(url)
app/models/wiki.rb
@@ -5,8 +5,9 @@ class Wiki &lt; ActiveRecord::Base @@ -5,8 +5,9 @@ class Wiki &lt; ActiveRecord::Base
5 belongs_to :user 5 belongs_to :user
6 has_many :notes, as: :noteable, dependent: :destroy 6 has_many :notes, as: :noteable, dependent: :destroy
7 7
8 - validates :content, :title, :user_id, presence: true  
9 - validates :title, length: 1..250 8 + validates :content, presence: true
  9 + validates :user, presence: true
  10 + validates :title, presence: true, length: 1..250
10 11
11 before_update :set_slug 12 before_update :set_slug
12 13
@@ -16,21 +17,20 @@ class Wiki &lt; ActiveRecord::Base @@ -16,21 +17,20 @@ class Wiki &lt; ActiveRecord::Base
16 17
17 protected 18 protected
18 19
  20 + def self.regenerate_from wiki
  21 + regenerated_field = [:slug, :content, :title]
  22 +
  23 + new_wiki = Wiki.new
  24 + regenerated_field.each do |field|
  25 + new_wiki.send("#{field}=", wiki.send(field))
  26 + end
  27 + new_wiki
  28 + end
  29 +
19 def set_slug 30 def set_slug
20 self.slug = self.title.parameterize 31 self.slug = self.title.parameterize
21 end 32 end
22 33
23 - class << self  
24 - def regenerate_from wiki  
25 - regenerated_field = [:slug, :content, :title]  
26 -  
27 - new_wiki = Wiki.new  
28 - regenerated_field.each do |field|  
29 - new_wiki.send("#{field}=", wiki.send(field))  
30 - end  
31 - new_wiki  
32 - end  
33 - end  
34 end 34 end
35 35
36 # == Schema Information 36 # == Schema Information
app/roles/account.rb
@@ -41,7 +41,7 @@ module Account @@ -41,7 +41,7 @@ module Account
41 # Remove user from all projects and 41 # Remove user from all projects and
42 # set blocked attribute to true 42 # set blocked attribute to true
43 def block 43 def block
44 - users_projects.all.each do |membership| 44 + users_projects.find_each do |membership|
45 return false unless membership.destroy 45 return false unless membership.destroy
46 end 46 end
47 47
app/roles/authority.rb
@@ -2,12 +2,12 @@ module Authority @@ -2,12 +2,12 @@ module Authority
2 # Compatible with all access rights 2 # Compatible with all access rights
3 # Should be rewrited for new access rights 3 # Should be rewrited for new access rights
4 def add_access(user, *access) 4 def add_access(user, *access)
5 - access = if access.include?(:admin)  
6 - { project_access: UsersProject::MASTER } 5 + access = if access.include?(:admin)
  6 + { project_access: UsersProject::MASTER }
7 elsif access.include?(:write) 7 elsif access.include?(:write)
8 - { project_access: UsersProject::DEVELOPER } 8 + { project_access: UsersProject::DEVELOPER }
9 else 9 else
10 - { project_access: UsersProject::REPORTER } 10 + { project_access: UsersProject::REPORTER }
11 end 11 end
12 opts = { user: user } 12 opts = { user: user }
13 opts.merge!(access) 13 opts.merge!(access)
app/roles/issue_commonality.rb
@@ -8,12 +8,9 @@ module IssueCommonality @@ -8,12 +8,9 @@ module IssueCommonality
8 belongs_to :assignee, class_name: "User" 8 belongs_to :assignee, class_name: "User"
9 has_many :notes, as: :noteable, dependent: :destroy 9 has_many :notes, as: :noteable, dependent: :destroy
10 10
11 - validates_presence_of :project_id  
12 - validates_presence_of :author_id  
13 -  
14 - validates :title,  
15 - presence: true,  
16 - length: { within: 0..255 } 11 + validates :project, presence: true
  12 + validates :author, presence: true
  13 + validates :title, presence: true, length: { within: 0..255 }
17 validates :closed, inclusion: { in: [true, false] } 14 validates :closed, inclusion: { in: [true, false] }
18 15
19 scope :opened, where(closed: false) 16 scope :opened, where(closed: false)
app/roles/push_event.rb
@@ -5,11 +5,11 @@ module PushEvent @@ -5,11 +5,11 @@ module PushEvent
5 false 5 false
6 end 6 end
7 7
8 - def tag? 8 + def tag?
9 data[:ref]["refs/tags"] 9 data[:ref]["refs/tags"]
10 end 10 end
11 11
12 - def branch? 12 + def branch?
13 data[:ref]["refs/heads"] 13 data[:ref]["refs/heads"]
14 end 14 end
15 15
@@ -25,7 +25,7 @@ module PushEvent @@ -25,7 +25,7 @@ module PushEvent
25 commit_to =~ /^00000/ 25 commit_to =~ /^00000/
26 end 26 end
27 27
28 - def md_ref? 28 + def md_ref?
29 !(rm_ref? || new_ref?) 29 !(rm_ref? || new_ref?)
30 end 30 end
31 31
@@ -37,7 +37,7 @@ module PushEvent @@ -37,7 +37,7 @@ module PushEvent
37 data[:after] 37 data[:after]
38 end 38 end
39 39
40 - def ref_name 40 + def ref_name
41 if tag? 41 if tag?
42 tag_name 42 tag_name
43 else 43 else