2012-11-19 13:24:05 -05:00
|
|
|
# == 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
|
|
|
|
#
|
|
|
|
|
2012-02-19 09:35:31 -05:00
|
|
|
class Wiki < ActiveRecord::Base
|
2012-09-26 14:17:17 -04:00
|
|
|
attr_accessible :title, :content, :slug
|
|
|
|
|
2012-02-19 09:35:31 -05:00
|
|
|
belongs_to :project
|
2012-02-19 14:52:05 -05:00
|
|
|
belongs_to :user
|
2012-08-10 18:07:50 -04:00
|
|
|
has_many :notes, as: :noteable, dependent: :destroy
|
2012-02-19 09:35:31 -05:00
|
|
|
|
2012-10-08 20:10:04 -04:00
|
|
|
validates :content, presence: true
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :title, presence: true, length: 1..250
|
2012-02-19 09:35:31 -05:00
|
|
|
|
2012-02-19 12:05:35 -05:00
|
|
|
before_update :set_slug
|
2012-02-19 09:35:31 -05:00
|
|
|
|
|
|
|
def to_param
|
|
|
|
slug
|
|
|
|
end
|
|
|
|
|
2012-10-16 15:05:11 -04:00
|
|
|
class << self
|
|
|
|
def search(query)
|
|
|
|
where("title like :query OR content like :query", query: "%#{query}%")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-19 09:35:31 -05:00
|
|
|
protected
|
|
|
|
|
2012-10-08 20:10:04 -04:00
|
|
|
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
|
|
|
|
|
2012-02-19 09:35:31 -05:00
|
|
|
def set_slug
|
|
|
|
self.slug = self.title.parameterize
|
|
|
|
end
|
2012-02-19 12:05:35 -05:00
|
|
|
|
2012-02-19 09:35:31 -05:00
|
|
|
end
|