diff options
author | Job van der Voort <jobvandervoort@gmail.com> | 2015-04-21 17:21:51 +0300 |
---|---|---|
committer | Job van der Voort <jobvandervoort@gmail.com> | 2015-04-21 17:21:51 +0300 |
commit | a8e93b7f51d968c1380ed210499869b62b07fd15 (patch) | |
tree | c864e80dfd9cf4f83fcede678acc986e3a125bf5 /app/models/concerns | |
parent | 0625b15a481b3a3edd88110b3c18031ad9068d2f (diff) |
Version 7.10.0.rc5v7.10.0.rc5
Diffstat (limited to 'app/models/concerns')
-rw-r--r-- | app/models/concerns/internal_id.rb | 17 | ||||
-rw-r--r-- | app/models/concerns/issuable.rb | 189 | ||||
-rw-r--r-- | app/models/concerns/mentionable.rb | 89 | ||||
-rw-r--r-- | app/models/concerns/notifiable.rb | 15 | ||||
-rw-r--r-- | app/models/concerns/sortable.rb | 35 | ||||
-rw-r--r-- | app/models/concerns/taskable.rb | 51 | ||||
-rw-r--r-- | app/models/concerns/token_authenticatable.rb | 31 |
7 files changed, 0 insertions, 427 deletions
diff --git a/app/models/concerns/internal_id.rb b/app/models/concerns/internal_id.rb deleted file mode 100644 index 821ed54fb98..00000000000 --- a/app/models/concerns/internal_id.rb +++ /dev/null @@ -1,17 +0,0 @@ -module InternalId - extend ActiveSupport::Concern - - included do - validate :set_iid, on: :create - validates :iid, presence: true, numericality: true - end - - def set_iid - max_iid = project.send(self.class.name.tableize).maximum(:iid) - self.iid = max_iid.to_i + 1 - end - - def to_param - iid.to_s - end -end diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb deleted file mode 100644 index 478134dff68..00000000000 --- a/app/models/concerns/issuable.rb +++ /dev/null @@ -1,189 +0,0 @@ -# == Issuable concern -# -# Contains common functionality shared between Issues and MergeRequests -# -# Used by Issue, MergeRequest -# -module Issuable - extend ActiveSupport::Concern - include Mentionable - - included do - belongs_to :author, class_name: "User" - belongs_to :assignee, class_name: "User" - belongs_to :milestone - has_many :notes, as: :noteable, dependent: :destroy - has_many :label_links, as: :target, dependent: :destroy - has_many :labels, through: :label_links - has_many :subscriptions, dependent: :destroy, as: :subscribable - - validates :author, presence: true - validates :title, presence: true, length: { within: 0..255 } - - scope :authored, ->(user) { where(author_id: user) } - scope :assigned_to, ->(u) { where(assignee_id: u.id)} - scope :recent, -> { order("created_at DESC") } - scope :assigned, -> { where("assignee_id IS NOT NULL") } - scope :unassigned, -> { where("assignee_id IS NULL") } - scope :of_projects, ->(ids) { where(project_id: ids) } - scope :opened, -> { with_state(:opened, :reopened) } - scope :only_opened, -> { with_state(:opened) } - scope :only_reopened, -> { with_state(:reopened) } - scope :closed, -> { with_state(:closed) } - scope :order_milestone_due_desc, -> { joins(:milestone).reorder('milestones.due_date DESC, milestones.id DESC') } - scope :order_milestone_due_asc, -> { joins(:milestone).reorder('milestones.due_date ASC, milestones.id ASC') } - - delegate :name, - :email, - to: :author, - prefix: true - - delegate :name, - :email, - to: :assignee, - allow_nil: true, - prefix: true - - attr_mentionable :title, :description - end - - module ClassMethods - def search(query) - where("LOWER(title) like :query", query: "%#{query.downcase}%") - end - - def full_search(query) - where("LOWER(title) like :query OR LOWER(description) like :query", query: "%#{query.downcase}%") - end - - def sort(method) - case method.to_s - when 'milestone_due_asc' then order_milestone_due_asc - when 'milestone_due_desc' then order_milestone_due_desc - else - order_by(method) - end - 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 - - # - # Votes - # - - # Return the number of -1 comments (downvotes) - def downvotes - filter_superceded_votes(notes.select(&:downvote?), notes).size - end - - def downvotes_in_percent - if votes_count.zero? - 0 - else - 100.0 - upvotes_in_percent - end - end - - # Return the number of +1 comments (upvotes) - def upvotes - filter_superceded_votes(notes.select(&:upvote?), notes).size - end - - def upvotes_in_percent - if votes_count.zero? - 0 - else - 100.0 / votes_count * upvotes - end - end - - # Return the total number of votes - def votes_count - upvotes + downvotes - end - - # Return all users participating on the discussion - def participants(current_user = self.author) - users = [] - users << author - users << assignee if is_assigned? - mentions = [] - mentions << self.mentioned_users(current_user) - - notes.each do |note| - users << note.author - mentions << note.mentioned_users(current_user) - end - - users.concat(mentions.reduce([], :|)).uniq - end - - def subscribed?(user) - subscription = subscriptions.find_by_user_id(user.id) - - if subscription - return subscription.subscribed - end - - participants(user).include?(user) - end - - def toggle_subscription(user) - subscriptions. - find_or_initialize_by(user_id: user.id). - update(subscribed: !subscribed?(user)) - end - - def to_hook_data(user) - { - object_kind: self.class.name.underscore, - user: user.hook_attrs, - object_attributes: hook_attrs - } - end - - def label_names - labels.order('title ASC').pluck(:title) - end - - def remove_labels - labels.delete_all - end - - def add_labels_by_names(label_names) - label_names.each do |label_name| - label = project.labels.create_with(color: Label::DEFAULT_COLOR). - find_or_create_by(title: label_name.strip) - self.labels << label - end - end - - private - - def filter_superceded_votes(votes, notes) - filteredvotes = [] + votes - - votes.each do |vote| - if vote.superceded?(notes) - filteredvotes.delete(vote) - end - end - - filteredvotes - end -end diff --git a/app/models/concerns/mentionable.rb b/app/models/concerns/mentionable.rb deleted file mode 100644 index b7882a2bb16..00000000000 --- a/app/models/concerns/mentionable.rb +++ /dev/null @@ -1,89 +0,0 @@ -# == Mentionable concern -# -# Contains functionality related to objects that can mention Users, Issues, MergeRequests, or Commits by -# GFM references. -# -# Used by Issue, Note, MergeRequest, and Commit. -# -module Mentionable - extend ActiveSupport::Concern - - module ClassMethods - # Indicate which attributes of the Mentionable to search for GFM references. - def attr_mentionable(*attrs) - mentionable_attrs.concat(attrs.map(&:to_s)) - end - - # Accessor for attributes marked mentionable. - def mentionable_attrs - @mentionable_attrs ||= [] - end - end - - # Generate a GFM back-reference that will construct a link back to this Mentionable when rendered. Must - # be overridden if this model object can be referenced directly by GFM notation. - def gfm_reference - raise NotImplementedError.new("#{self.class} does not implement #gfm_reference") - end - - # Construct a String that contains possible GFM references. - def mentionable_text - self.class.mentionable_attrs.map { |attr| send(attr) || '' }.join - end - - # The GFM reference to this Mentionable, which shouldn't be included in its #references. - def local_reference - self - end - - # Determine whether or not a cross-reference Note has already been created between this Mentionable and - # the specified target. - def has_mentioned?(target) - Note.cross_reference_exists?(target, local_reference) - end - - def mentioned_users(current_user = nil) - return [] if mentionable_text.blank? - - ext = Gitlab::ReferenceExtractor.new(self.project, current_user) - ext.analyze(mentionable_text) - ext.users.uniq - end - - # Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference. - def references(p = project, current_user = self.author, text = mentionable_text) - return [] if text.blank? - - ext = Gitlab::ReferenceExtractor.new(p, current_user) - ext.analyze(text) - - (ext.issues + ext.merge_requests + ext.commits).uniq - [local_reference] - end - - # Create a cross-reference Note for each GFM reference to another Mentionable found in +mentionable_text+. - def create_cross_references!(p = project, a = author, without = []) - refs = references(p) - without - refs.each do |ref| - Note.create_cross_reference_note(ref, local_reference, a, p) - end - end - - # If the mentionable_text field is about to change, locate any *added* references and create cross references for - # them. Invoke from an observer's #before_save implementation. - def notice_added_references(p = project, a = author) - ch = changed_attributes - original, mentionable_changed = "", false - self.class.mentionable_attrs.each do |attr| - if ch[attr] - original << ch[attr] - mentionable_changed = true - end - end - - # Only proceed if the saved changes actually include a chance to an attr_mentionable field. - return unless mentionable_changed - - preexisting = references(p, self.author, original) - create_cross_references!(p, a, preexisting) - end -end diff --git a/app/models/concerns/notifiable.rb b/app/models/concerns/notifiable.rb deleted file mode 100644 index d7dcd97911d..00000000000 --- a/app/models/concerns/notifiable.rb +++ /dev/null @@ -1,15 +0,0 @@ -# == Notifiable concern -# -# Contains notification functionality -# -module Notifiable - extend ActiveSupport::Concern - - included do - validates :notification_level, inclusion: { in: Notification.project_notification_levels }, presence: true - end - - def notification - @notification ||= Notification.new(self) - end -end diff --git a/app/models/concerns/sortable.rb b/app/models/concerns/sortable.rb deleted file mode 100644 index 0ad2654867d..00000000000 --- a/app/models/concerns/sortable.rb +++ /dev/null @@ -1,35 +0,0 @@ -# == Sortable concern -# -# Set default scope for ordering objects -# -module Sortable - extend ActiveSupport::Concern - - included do - # By default all models should be ordered - # by created_at field starting from newest - default_scope { order(created_at: :desc, id: :desc) } - - scope :order_created_desc, -> { reorder(created_at: :desc, id: :desc) } - scope :order_created_asc, -> { reorder(created_at: :asc, id: :asc) } - scope :order_updated_desc, -> { reorder(updated_at: :desc, id: :desc) } - scope :order_updated_asc, -> { reorder(updated_at: :asc, id: :asc) } - scope :order_name_asc, -> { reorder(name: :asc) } - scope :order_name_desc, -> { reorder(name: :desc) } - end - - module ClassMethods - def order_by(method) - case method.to_s - when 'name_asc' then order_name_asc - when 'name_desc' then order_name_desc - when 'updated_asc' then order_updated_asc - when 'updated_desc' then order_updated_desc - when 'created_asc' then order_created_asc - when 'created_desc' then order_created_desc - else - all - end - end - end -end diff --git a/app/models/concerns/taskable.rb b/app/models/concerns/taskable.rb deleted file mode 100644 index bbb3b301a9f..00000000000 --- a/app/models/concerns/taskable.rb +++ /dev/null @@ -1,51 +0,0 @@ -# Contains functionality for objects that can have task lists in their -# descriptions. Task list items can be added with Markdown like "* [x] Fix -# bugs". -# -# Used by MergeRequest and Issue -module Taskable - TASK_PATTERN_MD = /^(?<bullet> *[*-] *)\[(?<checked>[ xX])\]/.freeze - TASK_PATTERN_HTML = /^<li>(?<p_tag>\s*<p>)?\[(?<checked>[ xX])\]/.freeze - - # Change the state of a task list item for this Taskable. Edit the object's - # description by finding the nth task item and changing its checkbox - # placeholder to "[x]" if +checked+ is true, or "[ ]" if it's false. - # Note: task numbering starts with 1 - def update_nth_task(n, checked) - index = 0 - check_char = checked ? 'x' : ' ' - - # Do this instead of using #gsub! so that ActiveRecord detects that a field - # has changed. - self.description = self.description.gsub(TASK_PATTERN_MD) do |match| - index += 1 - case index - when n then "#{$LAST_MATCH_INFO[:bullet]}[#{check_char}]" - else match - end - end - - save - end - - # Return true if this object's description has any task list items. - def tasks? - description && description.match(TASK_PATTERN_MD) - end - - # Return a string that describes the current state of this Taskable's task - # list items, e.g. "20 tasks (12 done, 8 unfinished)" - def task_status - return nil unless description - - num_tasks = 0 - num_done = 0 - - description.scan(TASK_PATTERN_MD) do - num_tasks += 1 - num_done += 1 unless $LAST_MATCH_INFO[:checked] == ' ' - end - - "#{num_tasks} tasks (#{num_done} done, #{num_tasks - num_done} unfinished)" - end -end diff --git a/app/models/concerns/token_authenticatable.rb b/app/models/concerns/token_authenticatable.rb deleted file mode 100644 index 9b88ec1cc38..00000000000 --- a/app/models/concerns/token_authenticatable.rb +++ /dev/null @@ -1,31 +0,0 @@ -module TokenAuthenticatable - extend ActiveSupport::Concern - - module ClassMethods - def find_by_authentication_token(authentication_token = nil) - if authentication_token - where(authentication_token: authentication_token).first - end - end - end - - def ensure_authentication_token - if authentication_token.blank? - self.authentication_token = generate_authentication_token - end - end - - def reset_authentication_token! - self.authentication_token = generate_authentication_token - save - end - - private - - def generate_authentication_token - loop do - token = Devise.friendly_token - break token unless self.class.unscoped.where(authentication_token: token).first - end - end -end |