summaryrefslogtreecommitdiff
path: root/app/models/wiki.rb
blob: 7f488ca7625cf83851e32a19cf96dc402b83ed0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# == Schema Information
#
# Table name: wikis
#
#  id         :integer          not null, primary key
#  title      :string(255)
#  content    :text
#  project_id :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  slug       :string(255)
#  user_id    :integer
#

class Wiki < ActiveRecord::Base
  attr_accessible :title, :content, :slug

  belongs_to :project
  belongs_to :user
  has_many :notes, as: :noteable, dependent: :destroy

  validates :content, presence: true
  validates :user, presence: true
  validates :title, presence: true, length: 1..250

  before_update :set_slug

  scope :ordered, order("created_at DESC")

  def to_param
    slug
  end

  class << self
    def search(query)
      where("title like :query OR content like :query", query: "%#{query}%")
    end
  end

  protected

  def self.regenerate_from wiki
    regenerated_field = [:slug, :content, :title]

    new_wiki = Wiki.new
    regenerated_field.each do |field|
      new_wiki.send("#{field}=", wiki.send(field))
    end
    new_wiki
  end

  def set_slug
    self.slug = self.title.parameterize
  end
end