diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-01-02 23:35:11 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-01-02 23:35:11 +0200 |
commit | cac7723451e575ce39a6930990178450a2a972f0 (patch) | |
tree | 32b258ab1929ac710665b19a7f8b62addad83538 /lib | |
parent | 1b25a8f4374363d546d4a58f47c6fe00c3b3af07 (diff) | |
download | gitlab-ce-cac7723451e575ce39a6930990178450a2a972f0.tar.gz |
Get rid of roles
Diffstat (limited to 'lib')
-rw-r--r-- | lib/git_host.rb | 11 | ||||
-rw-r--r-- | lib/issue_commonality.rb | 71 | ||||
-rw-r--r-- | lib/static_model.rb | 47 | ||||
-rw-r--r-- | lib/votes.rb | 39 |
4 files changed, 168 insertions, 0 deletions
diff --git a/lib/git_host.rb b/lib/git_host.rb new file mode 100644 index 00000000000..2410e0fec42 --- /dev/null +++ b/lib/git_host.rb @@ -0,0 +1,11 @@ +# == GitHost role +# +# Provide a shortcut to Gitlab::Gitolite instance +# +# Used by Project, UsersProject +# +module GitHost + def git_host + Gitlab::Gitolite.new + end +end diff --git a/lib/issue_commonality.rb b/lib/issue_commonality.rb new file mode 100644 index 00000000000..b755936c9c1 --- /dev/null +++ b/lib/issue_commonality.rb @@ -0,0 +1,71 @@ +# == 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 diff --git a/lib/static_model.rb b/lib/static_model.rb new file mode 100644 index 00000000000..5b64be1f041 --- /dev/null +++ b/lib/static_model.rb @@ -0,0 +1,47 @@ +# Provides an ActiveRecord-like interface to a model whose data is not persisted to a database. +module StaticModel + extend ActiveSupport::Concern + + module ClassMethods + # Used by ActiveRecord's polymorphic association to set object_id + def primary_key + 'id' + end + + # Used by ActiveRecord's polymorphic association to set object_type + def base_class + self + end + end + + # Used by AR for fetching attributes + # + # Pass it along if we respond to it. + def [](key) + send(key) if respond_to?(key) + end + + def to_param + id + end + + def new_record? + false + end + + def persisted? + false + end + + def destroyed? + false + end + + def ==(other) + if other.is_a? StaticModel + id == other.id + else + super + end + end +end diff --git a/lib/votes.rb b/lib/votes.rb new file mode 100644 index 00000000000..dfd751b1b11 --- /dev/null +++ b/lib/votes.rb @@ -0,0 +1,39 @@ +# == 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 |