diff options
author | Robert Speicher <rspeicher@gmail.com> | 2015-05-03 06:11:21 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2015-05-26 22:48:30 +0300 |
commit | c0faf91ff23815404a95cf4510b43dcf5e331c4f (patch) | |
tree | 81769f125569dd6ea012920544ada1e8666ba4e5 /app | |
parent | b06dc74d611192744d34acda944d7ed9e554342a (diff) |
Add `to_reference` for models that support references
Now there is a single source of information for which attribute a model
uses to be referenced, and its special character.
Diffstat (limited to 'app')
-rw-r--r-- | app/models/commit.rb | 16 | ||||
-rw-r--r-- | app/models/commit_range.rb | 9 | ||||
-rw-r--r-- | app/models/external_issue.rb | 6 | ||||
-rw-r--r-- | app/models/group.rb | 10 | ||||
-rw-r--r-- | app/models/issue.rb | 21 | ||||
-rw-r--r-- | app/models/label.rb | 27 | ||||
-rw-r--r-- | app/models/merge_request.rb | 21 | ||||
-rw-r--r-- | app/models/project.rb | 9 | ||||
-rw-r--r-- | app/models/snippet.rb | 19 | ||||
-rw-r--r-- | app/models/user.rb | 16 |
10 files changed, 138 insertions, 16 deletions
diff --git a/app/models/commit.rb b/app/models/commit.rb index 5dea7dda513..3cc8d11a4aa 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -1,9 +1,11 @@ class Commit - include ActiveModel::Conversion - include StaticModel extend ActiveModel::Naming + + include ActiveModel::Conversion include Mentionable include Participable + include Referable + include StaticModel attr_mentionable :safe_message participant :author, :committer, :notes, :mentioned_users @@ -60,6 +62,14 @@ class Commit (self.class === other) && (raw == other.raw) end + def to_reference(from_project = nil) + if cross_project_reference?(from_project) + "#{project.to_reference}@#{id}" + else + id + end + end + def diff_line_count @diff_line_count ||= Commit::diff_line_count(self.diffs) @diff_line_count @@ -132,7 +142,7 @@ class Commit # Mentionable override. def gfm_reference - "commit #{id}" + "commit #{to_reference}" end def author diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb index e6456198264..b98f939a115 100644 --- a/app/models/commit_range.rb +++ b/app/models/commit_range.rb @@ -19,6 +19,7 @@ # class CommitRange include ActiveModel::Conversion + include Referable attr_reader :sha_from, :notation, :sha_to @@ -59,6 +60,14 @@ class CommitRange "#{sha_from[0..7]}#{notation}#{sha_to[0..7]}" end + def to_reference(from_project = nil) + if cross_project_reference?(from_project) + "#{project.to_reference}@#{to_s}" + else + to_s + end + end + # Returns a String for use in a link's title attribute def reference_title "Commits #{suffixed_sha_from} through #{sha_to}" diff --git a/app/models/external_issue.rb b/app/models/external_issue.rb index 85fdb12bfdc..6fda4a2ab77 100644 --- a/app/models/external_issue.rb +++ b/app/models/external_issue.rb @@ -1,4 +1,6 @@ class ExternalIssue + include Referable + def initialize(issue_identifier, project) @issue_identifier, @project = issue_identifier, project end @@ -7,6 +9,10 @@ class ExternalIssue @issue_identifier.to_s end + def to_reference(_from_project = nil) + id + end + def id @issue_identifier.to_s end diff --git a/app/models/group.rb b/app/models/group.rb index 687458adac4..33d72e0d9ee 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -17,6 +17,8 @@ require 'carrierwave/orm/activerecord' require 'file_size_validator' class Group < Namespace + include Referable + has_many :group_members, dependent: :destroy, as: :source, class_name: 'GroupMember' has_many :users, through: :group_members @@ -36,6 +38,14 @@ class Group < Namespace def sort(method) order_by(method) end + + def reference_prefix + '@' + end + end + + def to_reference(_from_project = nil) + "#{self.class.reference_prefix}#{name}" end def human_name diff --git a/app/models/issue.rb b/app/models/issue.rb index 6e102051387..ff13cbca845 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -21,10 +21,11 @@ require 'carrierwave/orm/activerecord' require 'file_size_validator' class Issue < ActiveRecord::Base - include Issuable include InternalId - include Taskable + include Issuable + include Referable include Sortable + include Taskable ActsAsTaggableOn.strict_case_match = true @@ -49,14 +50,28 @@ class Issue < ActiveRecord::Base state :closed end + def self.reference_prefix + '#' + end + def hook_attrs attributes end + def to_reference(from_project = nil) + reference = "#{self.class.reference_prefix}#{iid}" + + if cross_project_reference?(from_project) + reference = project.to_reference + reference + end + + reference + end + # Mentionable overrides. def gfm_reference - "issue ##{iid}" + "issue #{to_reference}" end # Reset issue events cache diff --git a/app/models/label.rb b/app/models/label.rb index eee28acefc1..013e6bf5978 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -11,6 +11,8 @@ # class Label < ActiveRecord::Base + include Referable + DEFAULT_COLOR = '#428BCA' default_value_for :color, DEFAULT_COLOR @@ -34,6 +36,31 @@ class Label < ActiveRecord::Base alias_attribute :name, :title + def self.reference_prefix + '~' + end + + # Returns the String necessary to reference this Label in Markdown + # + # format - Symbol format to use (default: :id, optional: :name) + # + # Note that its argument differs from other objects implementing Referable. If + # a non-Symbol argument is given (such as a Project), it will default to :id. + # + # Examples: + # + # Label.first.to_reference # => "~1" + # Label.first.to_reference(:name) # => "~\"bug\"" + # + # Returns a String + def to_reference(format = :id) + if format == :name + %(#{self.class.reference_prefix}"#{name}") + else + "#{self.class.reference_prefix}#{id}" + end + end + def open_issues_count issues.opened.count end diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 64f3c39f131..bfbf498591a 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -25,10 +25,11 @@ require Rails.root.join("app/models/commit") require Rails.root.join("lib/static_model") class MergeRequest < ActiveRecord::Base - include Issuable - include Taskable include InternalId + include Issuable + include Referable include Sortable + include Taskable belongs_to :target_project, foreign_key: :target_project_id, class_name: "Project" belongs_to :source_project, foreign_key: :source_project_id, class_name: "Project" @@ -135,6 +136,20 @@ class MergeRequest < ActiveRecord::Base scope :closed, -> { with_states(:closed, :merged) } scope :declined, -> { with_states(:closed) } + def self.reference_prefix + '!' + end + + def to_reference(from_project = nil) + reference = "#{self.class.reference_prefix}#{iid}" + + if cross_project_reference?(from_project) + reference = project.to_reference + reference + end + + reference + end + def validate_branches if target_project == source_project && target_branch == source_branch errors.add :branch_conflict, "You can not use same project/branch for source and target" @@ -291,7 +306,7 @@ class MergeRequest < ActiveRecord::Base # Mentionable override. def gfm_reference - "merge request !#{iid}" + "merge request #{to_reference}" end def target_project_path diff --git a/app/models/project.rb b/app/models/project.rb index 09d3ffd22fe..c943114449a 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -33,11 +33,12 @@ require 'carrierwave/orm/activerecord' require 'file_size_validator' class Project < ActiveRecord::Base - include Sortable + include Gitlab::ConfigHelper include Gitlab::ShellAdapter include Gitlab::VisibilityLevel - include Gitlab::ConfigHelper include Rails.application.routes.url_helpers + include Referable + include Sortable extend Gitlab::ConfigHelper extend Enumerize @@ -305,6 +306,10 @@ class Project < ActiveRecord::Base path end + def to_reference(_from_project = nil) + path_with_namespace + end + def web_url [gitlab_config.url, path_with_namespace].join('/') end diff --git a/app/models/snippet.rb b/app/models/snippet.rb index d2af26539b6..90fada3c114 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -16,10 +16,11 @@ # class Snippet < ActiveRecord::Base - include Sortable - include Linguist::BlobHelper include Gitlab::VisibilityLevel + include Linguist::BlobHelper include Participable + include Referable + include Sortable default_value_for :visibility_level, Snippet::PRIVATE @@ -50,6 +51,20 @@ class Snippet < ActiveRecord::Base participant :author, :notes + def self.reference_prefix + '$' + end + + def to_reference(from_project = nil) + reference = "#{self.class.reference_prefix}#{id}" + + if cross_project_reference?(from_project) + reference = project.to_reference + reference + end + + reference + end + def self.content_types [ ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java", diff --git a/app/models/user.rb b/app/models/user.rb index 4dd37e73564..f546dc015c2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -62,11 +62,13 @@ require 'carrierwave/orm/activerecord' require 'file_size_validator' class User < ActiveRecord::Base - include Sortable - include Gitlab::ConfigHelper - include TokenAuthenticatable extend Gitlab::ConfigHelper + + include Gitlab::ConfigHelper include Gitlab::CurrentSettings + include Referable + include Sortable + include TokenAuthenticatable default_value_for :admin, false default_value_for :can_create_group, gitlab_config.default_can_create_group @@ -247,6 +249,10 @@ class User < ActiveRecord::Base def build_user(attrs = {}) User.new(attrs) end + + def reference_prefix + '@' + end end # @@ -257,6 +263,10 @@ class User < ActiveRecord::Base username end + def to_reference(_from_project = nil) + "#{self.class.reference_prefix}#{username}" + end + def notification @notification ||= Notification.new(self) end |