note.rb 2.19 KB
Newer Older
gitlabhq's avatar
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's avatar
Nihad Abbasov committed
15
  attr_protected :author, :author_id
Valery Sizov's avatar
Valery Sizov committed
16
  attr_accessor :notify
Cedric Gatay's avatar
Cedric Gatay committed
17
  attr_accessor :notify_author
gitlabhq's avatar
gitlabhq committed
18 19 20 21 22

  validates_presence_of :project

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

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

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

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

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

  def notify
    @notify ||= false
  end
Cedric Gatay's avatar
Cedric Gatay committed
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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

  def line_file_id
    @line_file_id ||= line_code.split("_")[1].to_i if line_code
  end

  def line_type_id
    @line_type_id ||= line_code.split("_").first if line_code
  end

  def line_number 
    @line_number ||= line_code.split("_").last.to_i if line_code
  end

  def for_line?(file_id, old_line, new_line)
    line_file_id == file_id && 
      ((line_type_id == "NEW" && line_number == new_line) || (line_type_id == "OLD" && line_number == old_line ))
  end
gitlabhq's avatar
gitlabhq committed
77 78 79 80 81 82
end
# == Schema Information
#
# Table name: notes
#
#  id            :integer         not null, primary key
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83
#  note          :text
gitlabhq's avatar
gitlabhq committed
84 85 86 87 88 89 90
#  noteable_id   :string(255)
#  noteable_type :string(255)
#  author_id     :integer
#  created_at    :datetime
#  updated_at    :datetime
#  project_id    :integer
#  attachment    :string(255)
91
#  line_code     :string(255)
gitlabhq's avatar
gitlabhq committed
92 93
#