diff --git a/app/helpers/commits_helper.rb b/app/helpers/commits_helper.rb
index 16ecfb1f89b69eadd6088f33499b40302b52ebc5..4b35b0ac910f15ba6fa849dee7ed209fb6329a34 100644
--- a/app/helpers/commits_helper.rb
+++ b/app/helpers/commits_helper.rb
@@ -17,7 +17,7 @@ module CommitsHelper
     line_old = 1
     line_new = 1
     type = nil
-    
+
     lines_arr = ::Gitlab::InlineDiff.processing diff_arr
     lines_arr.each do |line|
       next if line.match(/^\-\-\- \/dev\/null/)
diff --git a/app/helpers/merge_requests_helper.rb b/app/helpers/merge_requests_helper.rb
index 62ac5e55d43e94cb42b3d6aaf49e3ecb8517101e..16855989bdd98900273b55db7e0e2731258b4538 100644
--- a/app/helpers/merge_requests_helper.rb
+++ b/app/helpers/merge_requests_helper.rb
@@ -24,7 +24,7 @@ module MergeRequestsHelper
   def new_mr_path_from_push_event(event)
     new_project_merge_request_path(
       event.project,
-      merge_request: { 
+      merge_request: {
         source_branch: event.branch_name,
         target_branch: event.project.root_ref,
         title: event.branch_name.titleize
diff --git a/app/models/ability.rb b/app/models/ability.rb
index 20ded59b5113b272a31988cd233aed9dd2015306..d3180b76e7c90ad247b4cff93dd3754c162d6c82 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -1,61 +1,61 @@
 class Ability
-  def self.allowed(object, subject)
-    case subject.class.name
-    when "Project" then project_abilities(object, subject)
-    when "Issue" then issue_abilities(object, subject)
-    when "Note" then note_abilities(object, subject)
-    when "Snippet" then snippet_abilities(object, subject)
-    when "MergeRequest" then merge_request_abilities(object, subject)
-    else []
+  class << self
+    def allowed(object, subject)
+      case subject.class.name
+      when "Project" then project_abilities(object, subject)
+      when "Issue" then issue_abilities(object, subject)
+      when "Note" then note_abilities(object, subject)
+      when "Snippet" then snippet_abilities(object, subject)
+      when "MergeRequest" then merge_request_abilities(object, subject)
+      else []
+      end
     end
-  end
 
-  def self.project_abilities(user, project)
-    rules = []
+    def project_abilities(user, project)
+      rules = []
 
-    rules << [
-      :read_project,
-      :read_wiki,
-      :read_issue,
-      :read_milestone,
-      :read_snippet,
-      :read_team_member,
-      :read_merge_request,
-      :read_note,
-      :write_project,
-      :write_issue,
-      :write_note
-    ] if project.guest_access_for?(user)
+      rules << [
+        :read_project,
+        :read_wiki,
+        :read_issue,
+        :read_milestone,
+        :read_snippet,
+        :read_team_member,
+        :read_merge_request,
+        :read_note,
+        :write_project,
+        :write_issue,
+        :write_note
+      ] if project.guest_access_for?(user)
 
-    rules << [
-      :download_code,
-      :write_merge_request,
-      :write_snippet
-    ] if project.report_access_for?(user)
+      rules << [
+        :download_code,
+        :write_merge_request,
+        :write_snippet
+      ] if project.report_access_for?(user)
 
-    rules << [
-      :write_wiki
-    ] if project.dev_access_for?(user)
+      rules << [
+        :write_wiki
+      ] if project.dev_access_for?(user)
 
-    rules << [
-      :modify_issue,
-      :modify_snippet,
-      :modify_merge_request,
-      :admin_project,
-      :admin_issue,
-      :admin_milestone,
-      :admin_snippet,
-      :admin_team_member,
-      :admin_merge_request,
-      :admin_note,
-      :accept_mr,
-      :admin_wiki
-    ] if project.master_access_for?(user) || project.owner == user
+      rules << [
+        :modify_issue,
+        :modify_snippet,
+        :modify_merge_request,
+        :admin_project,
+        :admin_issue,
+        :admin_milestone,
+        :admin_snippet,
+        :admin_team_member,
+        :admin_merge_request,
+        :admin_note,
+        :accept_mr,
+        :admin_wiki
+      ] if project.master_access_for?(user) || project.owner == user
 
-    rules.flatten
-  end
+      rules.flatten
+    end
 
-  class << self
     [:issue, :note, :snippet, :merge_request].each do |name|
       define_method "#{name}_abilities" do |user, subject|
         if subject.author == user
@@ -72,8 +72,7 @@ class Ability
             :"modify_#{name}",
           ]
         else
-          subject.respond_to?(:project) ?
-            project_abilities(user, subject.project) : []
+          subject.respond_to?(:project) ? project_abilities(user, subject.project) : []
         end
       end
     end
diff --git a/app/models/event.rb b/app/models/event.rb
index 8a698a9b6cf996a42d5f640a340989b187e7b9a9..17cc1d58477930d3ae14ed6a0a149097d93c07b1 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -27,19 +27,22 @@ class Event < ActiveRecord::Base
   # For Hash only
   serialize :data
 
+  # Scopes
   scope :recent, order("created_at DESC")
   scope :code_push, where(action: Pushed)
 
-  def self.determine_action(record)
-    if [Issue, MergeRequest].include? record.class
-      Event::Created
-    elsif record.kind_of? Note
-      Event::Commented
+  class << self
+    def determine_action(record)
+      if [Issue, MergeRequest].include? record.class
+        Event::Created
+      elsif record.kind_of? Note
+        Event::Commented
+      end
     end
-  end
 
-  def self.recent_for_user user
-    where(project_id: user.projects.map(&:id)).recent
+    def recent_for_user user
+      where(project_id: user.projects.map(&:id)).recent
+    end
   end
 
   # Next events currently enabled for system
diff --git a/app/models/group.rb b/app/models/group.rb
index 780d5fddca0e4e857f9704acb4da10d48ac07269..012b12856ec14cf0b784264e2a3feadd9bfeabab 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -1,15 +1,3 @@
-# == Schema Information
-#
-# Table name: groups
-#
-#  id         :integer         not null, primary key
-#  name       :string(255)     not null
-#  code       :string(255)     not null
-#  owner_id   :integer         not null
-#  created_at :datetime        not null
-#  updated_at :datetime        not null
-#
-
 class Group < ActiveRecord::Base
   attr_accessible :code, :name, :owner_id
 
@@ -18,7 +6,7 @@ class Group < ActiveRecord::Base
 
   validates :name, presence: true, uniqueness: true
   validates :code, presence: true, uniqueness: true
-  validates :owner_id, presence: true
+  validates :owner, presence: true
 
   delegate :name, to: :owner, allow_nil: true, prefix: true
 
@@ -31,6 +19,18 @@ class Group < ActiveRecord::Base
   end
 
   def users
-    User.joins(:users_projects).where('users_projects.project_id' => project_ids).uniq
+    User.joins(:users_projects).where(users_projects: {project_id: project_ids}).uniq
   end
 end
+
+# == Schema Information
+#
+# Table name: groups
+#
+#  id         :integer         not null, primary key
+#  name       :string(255)     not null
+#  code       :string(255)     not null
+#  owner_id   :integer         not null
+#  created_at :datetime        not null
+#  updated_at :datetime        not null
+#
diff --git a/app/models/key.rb b/app/models/key.rb
index dc1f3cdb9f90d3e9996261343110f8053721265c..43db095067d078f669d8316657ff9cb06529aba2 100644
--- a/app/models/key.rb
+++ b/app/models/key.rb
@@ -6,15 +6,14 @@ class Key < ActiveRecord::Base
 
   attr_accessible :key, :title
 
+  before_validation :strip_white_space
+  before_save :set_identifier
+
   validates :title, presence: true, length: { within: 0..255 }
-  validates :key, presence: true,
-            length: { within: 0..5000 },
-            format: { :with => /ssh-.{3} / }
+  validates :key, presence: true, length: { within: 0..5000 }, format: { :with => /ssh-.{3} / }
+  validate :unique_key, :fingerprintable_key
 
-  before_save :set_identifier
-  before_validation :strip_white_space
   delegate :name, :email, to: :user, prefix: true
-  validate :unique_key, :fingerprintable_key
 
   def strip_white_space
     self.key = self.key.strip unless self.key.blank?
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 8428c7c4cc6d077abae6a75dd1ac0ee20dc449b8..dc44d820560ef8bffb5eb0c0c5fc5dd256a665e5 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -7,6 +7,8 @@ class MergeRequest < ActiveRecord::Base
   attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch,
                   :author_id_of_changes
 
+  attr_accessor :should_remove_source_branch
+
   BROKEN_DIFF = "--broken-diff"
 
   UNCHECKED = 1
@@ -16,9 +18,8 @@ class MergeRequest < ActiveRecord::Base
   serialize :st_commits
   serialize :st_diffs
 
-  attr_accessor :should_remove_source_branch
-
-  validates_presence_of :source_branch, :target_branch
+  validates :source_branch, presence: true
+  validates :target_branch, presence: true
   validate :validate_branches
 
   def self.find_all_by_branch(branch_name)
diff --git a/app/models/milestone.rb b/app/models/milestone.rb
index a861995401750cdc1f7d762c1d68cc9246d0dd80..096376e31b1980ad2255b34b3bdec3afc4502aad 100644
--- a/app/models/milestone.rb
+++ b/app/models/milestone.rb
@@ -4,7 +4,8 @@ class Milestone < ActiveRecord::Base
   belongs_to :project
   has_many :issues
 
-  validates_presence_of :title, :project_id
+  validates :title, presence: true
+  validates :project, presence: true
 
   def self.active
     where("due_date > ? OR due_date IS NULL", Date.today)
diff --git a/app/models/note.rb b/app/models/note.rb
index 23ced536b6755e9820562f082769fbc267f51d83..55b011f8b2971b0539f5b6d1384b215e62d7907b 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -2,10 +2,13 @@ require 'carrierwave/orm/activerecord'
 require 'file_size_validator'
 
 class Note < ActiveRecord::Base
-  mount_uploader  :attachment, AttachmentUploader
+
   attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id,
                   :attachment, :line_code
 
+  attr_accessor :notify
+  attr_accessor :notify_author
+
   belongs_to :project
   belongs_to :noteable, polymorphic: true
   belongs_to :author, class_name: "User"
@@ -13,18 +16,17 @@ class Note < ActiveRecord::Base
   delegate :name, to: :project, prefix: true
   delegate :name, :email, to: :author, prefix: true
 
-  attr_accessor :notify
-  attr_accessor :notify_author
-
-  validates_presence_of :project
-
+  validates :project, presence: true
   validates :note, presence: true, length: { within: 0..5000 }
   validates :attachment, file_size: { maximum: 10.megabytes.to_i }
 
+  mount_uploader  :attachment, AttachmentUploader
+
+  # Scopes
   scope :common, where(noteable_id: nil)
   scope :today, where("created_at >= :date", date: Date.today)
   scope :last_week, where("created_at  >= :date", date: (Date.today - 7.days))
-  scope :since, lambda { |day| where("created_at  >= :date", date: (day)) }
+  scope :since, ->(day) { where("created_at  >= :date", date: (day)) }
   scope :fresh, order("created_at ASC, id ASC")
   scope :inc_author_project, includes(:project, :author)
   scope :inc_author, includes(:author)
diff --git a/app/models/project.rb b/app/models/project.rb
index 982b746365d65f56e6cec8669bf1f652c9f0ec6f..f4d868874b90ca32ccf5cbbf93f8941a56080f0b 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -28,42 +28,62 @@ class Project < ActiveRecord::Base
 
   delegate :name, to: :owner, allow_nil: true, prefix: true
 
+  # Validations
+  validates :owner, presence: true
+  validates :description, length: { within: 0..2000 }
+  validates :name, uniqueness: true, presence: true, length: { within: 0..255 }
+  validates :path, uniqueness: true, presence: true, length: { within: 0..255 },
+            format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
+                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
+  validates :code, presence: true, uniqueness: true, length: { within: 1..255 },
+            format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
+                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
+  validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
+            :wiki_enabled, inclusion: { in: [true, false] }
+  validate :check_limit, :repo_name
+
   # Scopes
   scope :public_only, where(private_flag: false)
   scope :without_user, ->(user)  { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) }
   scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }
 
-  def self.active
-    joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
-  end
-
-  def self.search query
-    where("name LIKE :query OR code LIKE :query OR path LIKE :query", query: "%#{query}%")
-  end
-
-  def self.create_by_user(params, user)
-    project = Project.new params
-
-    Project.transaction do
-      project.owner = user
-      project.save!
+  class << self
+    def active
+      joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
+    end
 
-      # Add user as project master
-      project.users_projects.create!(project_access: UsersProject::MASTER, user: user)
+    def search query
+      where("name LIKE :query OR code LIKE :query OR path LIKE :query", query: "%#{query}%")
+    end
 
-      # when project saved no team member exist so
-      # project repository should be updated after first user add
-      project.update_repository
+    def create_by_user(params, user)
+      project = Project.new params
+
+      Project.transaction do
+        project.owner = user
+        project.save!
+
+        # Add user as project master
+        project.users_projects.create!(project_access: UsersProject::MASTER, user: user)
+
+        # when project saved no team member exist so
+        # project repository should be updated after first user add
+        project.update_repository
+      end
+
+      project
+    rescue Gitlab::Gitolite::AccessDenied => ex
+      project.error_code = :gitolite
+      project
+    rescue => ex
+      project.error_code = :db
+      project.errors.add(:base, "Can't save project. Please try again later")
+      project
     end
 
-    project
-  rescue Gitlab::Gitolite::AccessDenied => ex
-    project.error_code = :gitolite
-    project
-  rescue => ex
-    project.error_code = :db
-    project.errors.add(:base, "Can't save project. Please try again later")
-    project
+    def access_options
+      UsersProject.access_roles
+    end
   end
 
   def git_error?
@@ -74,20 +94,6 @@ class Project < ActiveRecord::Base
     id && valid?
   end
 
-  # Validations
-  validates :owner, presence: true
-  validates :description, length: { within: 0..2000 }
-  validates :name, uniqueness: true, presence: true, length: { within: 0..255 }
-  validates :path, uniqueness: true, presence: true, length: { within: 0..255 },
-            format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
-                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
-  validates :code, presence: true, uniqueness: true, length: { within: 1..255 },
-            format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
-                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
-  validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
-            :wiki_enabled, inclusion: { in: [true, false] }
-  validate :check_limit, :repo_name
-
   def check_limit
     unless owner.can_create_project?
       errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
@@ -102,10 +108,6 @@ class Project < ActiveRecord::Base
     end
   end
 
-  def self.access_options
-    UsersProject.access_roles
-  end
-
   def to_param
     code
   end
diff --git a/app/models/protected_branch.rb b/app/models/protected_branch.rb
index c9e88d6bff62c48903ac0eaa98d767bf339a490e..cd93dc1bfe6a735b8d048922eafaa80e942cc351 100644
--- a/app/models/protected_branch.rb
+++ b/app/models/protected_branch.rb
@@ -4,7 +4,8 @@ class ProtectedBranch < ActiveRecord::Base
   attr_accessible :name
 
   belongs_to :project
-  validates_presence_of :name, :project_id
+  validates :name, presence: true
+  validates :project, presence: true
 
   after_save :update_repository
   after_destroy :update_repository
diff --git a/app/models/snippet.rb b/app/models/snippet.rb
index b37d6bef1397dfe626563ebfa8394715ee2fc4d8..e8670cdbcf26c773b78beaf257eb53fffda8db43 100644
--- a/app/models/snippet.rb
+++ b/app/models/snippet.rb
@@ -9,11 +9,13 @@ class Snippet < ActiveRecord::Base
 
   delegate :name, :email, to: :author, prefix: true
 
-  validates_presence_of :author_id, :project_id
+  validates :author, presence: true
+  validates :project, presence: true
   validates :title, presence: true, length: { within: 0..255 }
   validates :file_name, presence: true, length: { within: 0..255 }
   validates :content, presence: true, length: { within: 0..10000 }
 
+  # Scopes
   scope :fresh, order("created_at DESC")
   scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current])
   scope :expired, where(["expires_at IS NOT NULL AND expires_at < ?", Time.current])
diff --git a/app/models/system_hook.rb b/app/models/system_hook.rb
index 376f336d37a156a5f60617ed48ef31ed1ae6eb91..8ec8374bae07a7b1936e2649587cbb197503b183 100644
--- a/app/models/system_hook.rb
+++ b/app/models/system_hook.rb
@@ -1,13 +1,13 @@
 class SystemHook < WebHook
-  def async_execute(data)
-    Resque.enqueue(SystemHookWorker, id, data)
-  end
-
   def self.all_hooks_fire(data)
     SystemHook.all.each do |sh|
       sh.async_execute data
     end
   end
+
+  def async_execute(data)
+    Resque.enqueue(SystemHookWorker, id, data)
+  end
 end
 
 # == Schema Information
diff --git a/app/models/user.rb b/app/models/user.rb
index cf12fd607086c01d06a431e7c9a634d423837a46..da0e9a0a202f9c2eb3744aa725fd04c118a6efe5 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -27,53 +27,56 @@ class User < ActiveRecord::Base
   validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider}
   validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
 
-  scope :not_in_project, lambda { |project|  where("id not in (:ids)", ids: project.users.map(&:id) ) }
-  scope :admins, where(admin:  true)
-  scope :blocked, where(blocked:  true)
-  scope :active, where(blocked:  false)
-
   before_validation :generate_password, on: :create
   before_save :ensure_authentication_token
   alias_attribute :private_token, :authentication_token
 
-  def generate_password
-    if self.force_random_password
-      self.password = self.password_confirmation = Devise.friendly_token.first(8)
+  # Scopes
+  scope :not_in_project, ->(project) { where("id not in (:ids)", ids: project.users.map(&:id) ) }
+  scope :admins, where(admin:  true)
+  scope :blocked, where(blocked:  true)
+  scope :active, where(blocked:  false)
+
+  class << self
+    def filter filter_name
+      case filter_name
+      when "admins"; self.admins
+      when "blocked"; self.blocked
+      when "wop"; self.without_projects
+      else
+        self.active
+      end
     end
-  end
 
-  def self.filter filter_name
-    case filter_name
-    when "admins"; self.admins
-    when "blocked"; self.blocked
-    when "wop"; self.without_projects
-    else
-      self.active
+    def without_projects
+      where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')
     end
-  end
 
-  def self.without_projects
-    where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')
-  end
+    def create_from_omniauth(auth, ldap = false)
+      gitlab_auth.create_from_omniauth(auth, ldap)
+    end
 
-  def self.create_from_omniauth(auth, ldap = false)
-    gitlab_auth.create_from_omniauth(auth, ldap)
-  end
+    def find_or_new_for_omniauth(auth)
+      gitlab_auth.find_or_new_for_omniauth(auth)
+    end
 
-  def self.find_or_new_for_omniauth(auth)
-    gitlab_auth.find_or_new_for_omniauth(auth)
-  end
+    def find_for_ldap_auth(auth, signed_in_resource = nil)
+      gitlab_auth.find_for_ldap_auth(auth, signed_in_resource)
+    end
 
-  def self.find_for_ldap_auth(auth, signed_in_resource = nil)
-    gitlab_auth.find_for_ldap_auth(auth, signed_in_resource)
-  end
+    def gitlab_auth
+      Gitlab::Auth.new
+    end
 
-  def self.gitlab_auth
-    Gitlab::Auth.new
+    def search query
+      where("name LIKE :query or email LIKE :query", query: "%#{query}%")
+    end
   end
 
-  def self.search query
-    where("name LIKE :query OR email LIKE :query", query: "%#{query}%")
+  def generate_password
+    if self.force_random_password
+      self.password = self.password_confirmation = Devise.friendly_token.first(8)
+    end
   end
 end
 
diff --git a/app/models/users_project.rb b/app/models/users_project.rb
index 510b90cb9da908e68ffa82773505638093e0db43..a3d5116f70df3081a6165dd80eb8606a96256da4 100644
--- a/app/models/users_project.rb
+++ b/app/models/users_project.rb
@@ -14,62 +14,64 @@ class UsersProject < ActiveRecord::Base
   after_save :update_repository
   after_destroy :update_repository
 
-  validates_uniqueness_of :user_id, scope: [:project_id], message: "already exists in project"
-  validates_presence_of :user_id
-  validates_presence_of :project_id
+  validates :user, presence: true
+  validates :user_id, uniqueness: { :scope => [:project_id], message: "already exists in project" }
+  validates :project, presence: true
 
   delegate :name, :email, to: :user, prefix: true
 
-  def self.bulk_delete(project, user_ids)
-    UsersProject.transaction do
-      UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
-        users_project.destroy
+  class << self
+    def bulk_delete(project, user_ids)
+      UsersProject.transaction do
+        UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
+          users_project.destroy
+        end
       end
     end
-  end
 
-  def self.bulk_update(project, user_ids, project_access)
-    UsersProject.transaction do
-      UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
-        users_project.project_access = project_access
-        users_project.save
+    def bulk_update(project, user_ids, project_access)
+      UsersProject.transaction do
+        UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
+          users_project.project_access = project_access
+          users_project.save
+        end
       end
     end
-  end
 
-  def self.bulk_import(project, user_ids, project_access)
-    UsersProject.transaction do
-      user_ids.each do |user_id|
-        users_project = UsersProject.new(
-          project_access: project_access,
-          user_id: user_id
-        )
-        users_project.project = project
-        users_project.save
+    def bulk_import(project, user_ids, project_access)
+      UsersProject.transaction do
+        user_ids.each do |user_id|
+          users_project = UsersProject.new(
+            project_access: project_access,
+            user_id: user_id
+          )
+          users_project.project = project
+          users_project.save
+        end
       end
     end
-  end
 
-  def self.user_bulk_import(user, project_ids, project_access)
-    UsersProject.transaction do
-      project_ids.each do |project_id|
-        users_project = UsersProject.new(
-          project_access: project_access,
-        )
-        users_project.project_id = project_id
-        users_project.user_id = user.id
-        users_project.save
+    def user_bulk_import(user, project_ids, project_access)
+      UsersProject.transaction do
+        project_ids.each do |project_id|
+          users_project = UsersProject.new(
+            project_access: project_access,
+          )
+          users_project.project_id = project_id
+          users_project.user_id = user.id
+          users_project.save
+        end
       end
     end
-  end
 
-  def self.access_roles
-    {
-      "Guest"     => GUEST,
-      "Reporter"  => REPORTER,
-      "Developer" => DEVELOPER,
-      "Master"    => MASTER
-    }
+    def access_roles
+      {
+        "Guest"     => GUEST,
+        "Reporter"  => REPORTER,
+        "Developer" => DEVELOPER,
+        "Master"    => MASTER
+      }
+    end
   end
 
   def role_access
diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb
index edfab8861ca2a0bb5e3c9dc012ca820e45a47daf..779b3a99f5563f22a573696f177b8f2cb71cb9f5 100644
--- a/app/models/web_hook.rb
+++ b/app/models/web_hook.rb
@@ -7,7 +7,7 @@ class WebHook < ActiveRecord::Base
   default_timeout 10
 
   validates :url, presence: true,
-            format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
+                  format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
 
   def execute(data)
     parsed_url = URI.parse(url)
diff --git a/app/models/wiki.rb b/app/models/wiki.rb
index c87ac89e503bbfec811c7ea194266d768b3ac5a8..3a431e4cb5b28c7d821d3d4cbf4f498333ef40bc 100644
--- a/app/models/wiki.rb
+++ b/app/models/wiki.rb
@@ -5,8 +5,9 @@ class Wiki < ActiveRecord::Base
   belongs_to :user
   has_many :notes, as: :noteable, dependent: :destroy
 
-  validates :content, :title, :user_id, presence: true
-  validates :title, length: 1..250
+  validates :content, presence: true
+  validates :user, presence: true
+  validates :title, presence: true, length: 1..250
 
   before_update :set_slug
 
@@ -16,21 +17,20 @@ class Wiki < ActiveRecord::Base
 
   protected
 
+  def self.regenerate_from wiki
+    regenerated_field = [:slug, :content, :title]
+
+    new_wiki = Wiki.new
+    regenerated_field.each do |field|
+      new_wiki.send("#{field}=", wiki.send(field))
+    end
+    new_wiki
+  end
+
   def set_slug
     self.slug = self.title.parameterize
   end
 
-  class << self
-    def regenerate_from wiki
-      regenerated_field = [:slug, :content, :title]
-
-      new_wiki = Wiki.new
-      regenerated_field.each do |field|
-        new_wiki.send("#{field}=", wiki.send(field))
-      end
-      new_wiki
-    end
-  end
 end
 
 # == Schema Information
diff --git a/app/roles/account.rb b/app/roles/account.rb
index af5b7c8843776227cc2bc25b1d7fbc76ccc57a40..21545b91ce1a30d1cdbfc3c6b56a286417dd39c7 100644
--- a/app/roles/account.rb
+++ b/app/roles/account.rb
@@ -45,7 +45,7 @@ module Account
   # Remove user from all projects and
   # set blocked attribute to true
   def block
-    users_projects.all.each do |membership|
+    users_projects.find_each do |membership|
       return false unless membership.destroy
     end
 
diff --git a/app/roles/authority.rb b/app/roles/authority.rb
index 9d9153db66ebcbb0dd0c1e1fbf2009874fffa9ba..dbdd98397b93fdef77d3a0784567d484ebca1946 100644
--- a/app/roles/authority.rb
+++ b/app/roles/authority.rb
@@ -2,12 +2,12 @@ module Authority
   # Compatible with all access rights
   # Should be rewrited for new access rights
   def add_access(user, *access)
-    access = if access.include?(:admin) 
-               { project_access: UsersProject::MASTER } 
+    access = if access.include?(:admin)
+               { project_access: UsersProject::MASTER }
              elsif access.include?(:write)
-               { project_access: UsersProject::DEVELOPER } 
+               { project_access: UsersProject::DEVELOPER }
              else
-               { project_access: UsersProject::REPORTER } 
+               { project_access: UsersProject::REPORTER }
              end
     opts = { user: user }
     opts.merge!(access)
diff --git a/app/roles/issue_commonality.rb b/app/roles/issue_commonality.rb
index 304f08fda6c09b78652cf20256a770c28a840aff..b21d92a30838f5069a8f9ad07ae8b92fafa32914 100644
--- a/app/roles/issue_commonality.rb
+++ b/app/roles/issue_commonality.rb
@@ -8,12 +8,9 @@ module IssueCommonality
     belongs_to :assignee, class_name: "User"
     has_many :notes, as: :noteable, dependent: :destroy
 
-    validates_presence_of :project_id
-    validates_presence_of :author_id
-
-    validates :title,
-              presence: true,
-              length: { within: 0..255 }
+    validates :project, presence: true
+    validates :author, presence: true
+    validates :title, presence: true, length: { within: 0..255 }
     validates :closed, inclusion: { in: [true, false] }
 
     scope :opened, where(closed: false)
diff --git a/app/roles/push_event.rb b/app/roles/push_event.rb
index 40549f56b2f0036444070f87c43b08178e8e1de1..8ce71b540459da145bb05aeadbece8df0e08104f 100644
--- a/app/roles/push_event.rb
+++ b/app/roles/push_event.rb
@@ -5,11 +5,11 @@ module PushEvent
     false
   end
 
-  def tag? 
+  def tag?
     data[:ref]["refs/tags"]
   end
 
-  def branch? 
+  def branch?
     data[:ref]["refs/heads"]
   end
 
@@ -25,7 +25,7 @@ module PushEvent
     commit_to =~ /^00000/
   end
 
-  def md_ref? 
+  def md_ref?
     !(rm_ref? || new_ref?)
   end
 
@@ -37,7 +37,7 @@ module PushEvent
     data[:after]
   end
 
-  def ref_name 
+  def ref_name
     if tag?
       tag_name
     else
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index fd7db4b25d9be3a2ceacb4ea412e5150abbbfb56..5ae40658823df18fe2547998f0677bc09e86955e 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -20,5 +20,5 @@ describe Group do
   it { should validate_uniqueness_of(:name) }
   it { should validate_presence_of :code }
   it { should validate_uniqueness_of(:code) }
-  it { should validate_presence_of :owner_id }
+  it { should validate_presence_of :owner }
 end
diff --git a/spec/models/milestone_spec.rb b/spec/models/milestone_spec.rb
index 9c11a7b104383af1f2c2dc0d6e7bb71a977d4b4b..ef50e0127228e30a8f288f6fc94e95cb8b454111 100644
--- a/spec/models/milestone_spec.rb
+++ b/spec/models/milestone_spec.rb
@@ -12,7 +12,7 @@ describe Milestone do
 
   describe "Validation" do
     it { should validate_presence_of(:title) }
-    it { should validate_presence_of(:project_id) }
+    it { should validate_presence_of(:project) }
     it { should ensure_inclusion_of(:closed).in_array([true, false]) }
   end
 
diff --git a/spec/models/protected_branch_spec.rb b/spec/models/protected_branch_spec.rb
index 4b2923624dded4e065ce7ed7ef417fbcb2cdc4e5..c6b09f358fb2743fea331137257bc8c73a3c777f 100644
--- a/spec/models/protected_branch_spec.rb
+++ b/spec/models/protected_branch_spec.rb
@@ -10,7 +10,7 @@ describe ProtectedBranch do
   end
 
   describe 'Validation' do
-    it { should validate_presence_of(:project_id) }
+    it { should validate_presence_of(:project) }
     it { should validate_presence_of(:name) }
   end
 
diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb
index 66c36e51ec762f0c2b80376cc6ac5d455a24ee34..465612e0f199046c6b95976aa9c64deb9b64d711 100644
--- a/spec/models/snippet_spec.rb
+++ b/spec/models/snippet_spec.rb
@@ -13,8 +13,8 @@ describe Snippet do
   end
 
   describe "Validation" do
-    it { should validate_presence_of(:author_id) }
-    it { should validate_presence_of(:project_id) }
+    it { should validate_presence_of(:author) }
+    it { should validate_presence_of(:project) }
 
     it { should validate_presence_of(:title) }
     it { should ensure_length_of(:title).is_within(0..255) }
diff --git a/spec/models/users_project_spec.rb b/spec/models/users_project_spec.rb
index a13a08db17ad47f7815ea457d3b181d49a48ef3c..a64ff3678bf5858581c90184a62a32f9c35cb11c 100644
--- a/spec/models/users_project_spec.rb
+++ b/spec/models/users_project_spec.rb
@@ -13,10 +13,10 @@ describe UsersProject do
   describe "Validation" do
     let!(:users_project) { create(:users_project) }
 
-    it { should validate_presence_of(:user_id) }
+    it { should validate_presence_of(:user) }
     it { should validate_uniqueness_of(:user_id).scoped_to(:project_id).with_message(/already exists/) }
 
-    it { should validate_presence_of(:project_id) }
+    it { should validate_presence_of(:project) }
   end
 
   describe "Delegate methods" do
diff --git a/spec/models/wiki_spec.rb b/spec/models/wiki_spec.rb
index 1e27954cb84c1d32142b7c1da9a8918598d38f95..7830c8215fcecb82b18d13340f8c8b87f562e1a8 100644
--- a/spec/models/wiki_spec.rb
+++ b/spec/models/wiki_spec.rb
@@ -16,6 +16,6 @@ describe Wiki do
     it { should validate_presence_of(:title) }
     it { should ensure_length_of(:title).is_within(1..250) }
     it { should validate_presence_of(:content) }
-    it { should validate_presence_of(:user_id) }
+    it { should validate_presence_of(:user) }
   end
 end
diff --git a/spec/roles/issue_commonality_spec.rb b/spec/roles/issue_commonality_spec.rb
index 77b98b46ed91ae9f164c97b97b64520fb9777b82..fc4114e3a77f01a049319e6079e9b34378c1abcf 100644
--- a/spec/roles/issue_commonality_spec.rb
+++ b/spec/roles/issue_commonality_spec.rb
@@ -11,8 +11,8 @@ describe Issue, "IssueCommonality" do
   end
 
   describe "Validation" do
-    it { should validate_presence_of(:project_id) }
-    it { should validate_presence_of(:author_id) }
+    it { should validate_presence_of(:project) }
+    it { should validate_presence_of(:author) }
     it { should validate_presence_of(:title) }
     it { should ensure_length_of(:title).is_at_least(0).is_at_most(255) }
   end