Commit 85a792ff authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

more refactoring using models/concerns

parent 69cfbf2f381f
# == Issuable concern
#
# Contains common functionality shared between Issues and MergeRequests
#
# Used by Issue, MergeRequest
#
module Issuable
extend ActiveSupport::Concern
included do
belongs_to :project
belongs_to :author, class_name: "User"
belongs_to :assignee, class_name: "User"
belongs_to :milestone
has_many :notes, as: :noteable, dependent: :destroy
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)
scope :closed, where(closed: true)
scope :of_group, ->(group) { where(project_id: group.project_ids) }
scope :assigned, ->(u) { where(assignee_id: u.id)}
scope :recent, order("created_at DESC")
delegate :name,
:email,
to: :author,
prefix: true
delegate :name,
:email,
to: :assignee,
allow_nil: true,
prefix: true
attr_accessor :author_id_of_changes
end
module ClassMethods
def search(query)
where("title like :query", query: "%#{query}%")
end
end
def today?
Date.today == created_at.to_date
end
def new?
today? && created_at == updated_at
end
def is_assigned?
!!assignee_id
end
def is_being_reassigned?
assignee_id_changed?
end
def is_being_closed?
closed_changed? && closed
end
def is_being_reopened?
closed_changed? && !closed
end
# Return the number of +1 comments (upvotes)
def upvotes
notes.select(&:upvote?).size
end
def upvotes_in_percent
if votes_count.zero?
0
else
100.0 / votes_count * upvotes
end
end
# Return the number of -1 comments (downvotes)
def downvotes
notes.select(&:downvote?).size
end
def downvotes_in_percent
if votes_count.zero?
0
else
100.0 - upvotes_in_percent
end
end
# Return the total number of votes
def votes_count
upvotes + downvotes
end
end
......@@ -17,8 +17,7 @@
#
class Issue < ActiveRecord::Base
include IssueCommonality
include Votes
include Issuable
attr_accessible :title, :assignee_id, :closed, :position, :description,
:milestone_id, :label_list, :author_id_of_changes
......
......@@ -23,8 +23,7 @@
require Rails.root.join("lib/static_model")
class MergeRequest < ActiveRecord::Base
include IssueCommonality
include Votes
include Issuable
attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch, :milestone_id,
:author_id_of_changes
......
......@@ -21,7 +21,7 @@
require "grit"
class Project < ActiveRecord::Base
include GitHost
include Gitolited
class TransferError < StandardError; end
......@@ -408,7 +408,7 @@
Gitlab::ProjectMover.new(self, old_dir, new_dir).execute
git_host.move_repository(old_repo, self)
gitolite.move_repository(old_repo, self)
save!
end
......@@ -670,7 +670,7 @@
end
def url_to_repo
git_host.url_to_repo(path_with_namespace)
gitolite.url_to_repo(path_with_namespace)
end
def path_to_repo
......@@ -682,7 +682,7 @@
end
def update_repository
git_host.update_repository(self)
gitolite.update_repository(self)
end
def destroy_repository
......@@ -686,7 +686,7 @@
end
def destroy_repository
git_host.remove_repository(self)
gitolite.remove_repository(self)
end
def repo_exists?
......
......@@ -10,7 +10,7 @@
#
class ProtectedBranch < ActiveRecord::Base
include GitHost
include Gitolited
attr_accessible :name
......@@ -22,7 +22,7 @@
after_destroy :update_repository
def update_repository
git_host.update_repository(project)
gitolite.update_repository(project)
end
def commit
......
......@@ -11,7 +11,7 @@
#
class UsersProject < ActiveRecord::Base
include GitHost
include Gitolited
GUEST = 10
REPORTER = 20
......@@ -152,7 +152,7 @@
end
def update_repository
git_host.update_repository(project)
gitolite.update_repository(project)
end
def project_access_human
......
class KeyObserver < ActiveRecord::Observer
include GitHost
include Gitolited
def after_save(key)
......@@ -3,7 +3,7 @@
def after_save(key)
git_host.set_key(key.identifier, key.key, key.projects)
gitolite.set_key(key.identifier, key.key, key.projects)
end
def after_destroy(key)
return if key.is_deploy_key && !key.last_deploy?
......@@ -6,7 +6,7 @@
end
def after_destroy(key)
return if key.is_deploy_key && !key.last_deploy?
git_host.remove_key(key.identifier, key.projects)
gitolite.remove_key(key.identifier, key.projects)
end
end
# == GitHost role
#
# Provide a shortcut to Gitlab::Gitolite instance
#
# Used by Project, UsersProject
#
module GitHost
def git_host
Gitlab::Gitolite.new
end
end
# == Gitolited mixin
#
# Provide a shortcut to Gitlab::Gitolite instance by gitolite
#
# Used by Project, UsersProject, etc
#
module Gitolited
def gitolite
Gitlab::Gitolite.new
end
end
# == IssueCommonality role
#
# Contains common functionality shared between Issues and MergeRequests
#
# Used by Issue, MergeRequest
#
module IssueCommonality
extend ActiveSupport::Concern
included do
belongs_to :project
belongs_to :author, class_name: "User"
belongs_to :assignee, class_name: "User"
belongs_to :milestone
has_many :notes, as: :noteable, dependent: :destroy
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)
scope :closed, where(closed: true)
scope :of_group, ->(group) { where(project_id: group.project_ids) }
scope :assigned, ->(u) { where(assignee_id: u.id)}
scope :recent, order("created_at DESC")
delegate :name,
:email,
to: :author,
prefix: true
delegate :name,
:email,
to: :assignee,
allow_nil: true,
prefix: true
attr_accessor :author_id_of_changes
end
module ClassMethods
def search(query)
where("title like :query", query: "%#{query}%")
end
end
def today?
Date.today == created_at.to_date
end
def new?
today? && created_at == updated_at
end
def is_assigned?
!!assignee_id
end
def is_being_reassigned?
assignee_id_changed?
end
def is_being_closed?
closed_changed? && closed
end
def is_being_reopened?
closed_changed? && !closed
end
end
# == Votes role
#
# Provides functionality to upvote/downvote entity
# based on +1 and -1 notes
#
# Used for Issue and Merge Request
#
module Votes
# Return the number of +1 comments (upvotes)
def upvotes
notes.select(&:upvote?).size
end
def upvotes_in_percent
if votes_count.zero?
0
else
100.0 / votes_count * upvotes
end
end
# Return the number of -1 comments (downvotes)
def downvotes
notes.select(&:downvote?).size
end
def downvotes_in_percent
if votes_count.zero?
0
else
100.0 - upvotes_in_percent
end
end
# Return the total number of votes
def votes_count
upvotes + downvotes
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment