BigW Consortium Gitlab

note.rb 2.33 KB
Newer Older
gitlabhq committed
1 2 3 4 5 6 7 8 9
require 'carrierwave/orm/activerecord'
require 'file_size_validator'

class Note < ActiveRecord::Base
  belongs_to :project
  belongs_to :noteable, :polymorphic => true
  belongs_to :author,
    :class_name => "User"

10
  delegate :name,
11 12
           :email,
           :to => :author,
13 14
           :prefix => true

Nihad Abbasov committed
15
  attr_protected :author, :author_id
Valery Sizov committed
16
  attr_accessor :notify
17
  attr_accessor :notify_author
gitlabhq committed
18 19 20 21 22

  validates_presence_of :project

  validates :note,
            :presence => true,
gitlabhq committed
23
            :length   => { :within => 0..5000 }
gitlabhq committed
24

Nihad Abbasov committed
25 26 27 28
  validates :attachment,
            :file_size => {
              :maximum => 10.megabytes.to_i
            }
gitlabhq committed
29 30 31

  scope :common, where(:noteable_id => nil)

Drew committed
32
  scope :today, where("created_at >= :date", :date => Date.today)
gitlabhq committed
33
  scope :last_week, where("created_at  >= :date", :date => (Date.today - 7.days))
gitlabhq committed
34
  scope :since, lambda { |day| where("created_at  >= :date", :date => (day)) }
gitlabhq committed
35
  scope :fresh, order("created_at DESC")
gitlabhq committed
36 37
  scope :inc_author_project, includes(:project, :author)
  scope :inc_author, includes(:author)
gitlabhq committed
38

gitlabhq committed
39
  mount_uploader :attachment, AttachmentUploader
Valery Sizov committed
40 41 42 43

  def notify
    @notify ||= false
  end
44 45 46 47

  def notify_author
    @notify_author ||= false
  end
48 49 50 51 52 53 54

  def target
    if noteable_type == "Commit" 
      project.commit(noteable_id)
    else 
      noteable
    end
55 56 57 58
  # Temp fix to prevent app crash
  # if note commit id doesnt exist
  rescue 
    nil
59
  end
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  # Check if we can notify commit author
  # with email about our comment
  #
  # If commit author email exist in project 
  # and commit author is not passed user we can 
  # send email to him
  #
  # params:
  #   user - current user
  # 
  # return:
  #   Boolean
  #
  def notify_only_author?(user)
    commit? && commit_author &&
      commit_author.email != user.email
  end

  def commit?
    noteable_type == "Commit"
  end

  def commit_author
    @commit_author ||= 
      project.users.find_by_email(target.author_email) || 
      project.users.find_by_name(target.author_name)
  rescue 
    nil
  end
gitlabhq committed
90 91 92 93 94 95
end
# == Schema Information
#
# Table name: notes
#
#  id            :integer         not null, primary key
Dmitriy Zaporozhets committed
96
#  note          :text
gitlabhq committed
97 98 99 100 101 102 103
#  noteable_id   :string(255)
#  noteable_type :string(255)
#  author_id     :integer
#  created_at    :datetime
#  updated_at    :datetime
#  project_id    :integer
#  attachment    :string(255)
104
#  line_code     :string(255)
gitlabhq committed
105 106
#