Class: Page

Inherits:
ApplicationRecord show all
Includes:
BaseModel, MarkdownBody, Searchable
Defined in:
app/models/page.rb

Constant Summary

Constants included from ApplicationHelper

ApplicationHelper::ALLOW_ATTRIBUTES, ApplicationHelper::ALLOW_TAGS, ApplicationHelper::EMPTY_STRING, ApplicationHelper::LANGUAGES_LISTS, ApplicationHelper::MOBILE_USER_AGENTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

#admin?, #birthday_tag, #cache, #cached_asset_path, #controller_javascript_include_tag, #controller_stylesheet_link_tag, #highlight, #icon_tag, #insert_code_menu_items_tag, #javascript_include_tag_with_cached, #memory_cache, #mobile?, #notice_message, #owner?, #random_tips, #render_list, #render_page_title, #sanitize_markdown, #spaceless, #stylesheet_link_tag_with_cached, #timeago, #wiki_editor?

Methods included from BaseModel

#pull, #push

Instance Attribute Details

#change_descObject

Returns the value of attribute change_desc


13
14
15
# File 'app/models/page.rb', line 13

def change_desc
  @change_desc
end

#user_idObject

Returns the value of attribute user_id


13
14
15
# File 'app/models/page.rb', line 13

def user_id
  @user_id
end

#version_enableObject

Returns the value of attribute version_enable


13
14
15
# File 'app/models/page.rb', line 13

def version_enable
  @version_enable
end

Class Method Details

.find_by_slug(slug) ⇒ Object


65
66
67
# File 'app/models/page.rb', line 65

def self.find_by_slug(slug)
  fetch_by_uniq_keys(slug: slug)
end

Instance Method Details

#append_editorObject


23
24
25
26
27
# File 'app/models/page.rb', line 23

def append_editor
  unless editor_ids.include?(user_id.to_i)
    editor_ids << user_id.to_i
  end
end

#create_versionObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/page.rb', line 31

def create_version
  # 只有当 version_enable 为 true 的时候才记录版本
  # 以免后台,以及其他的一些 update 时被误调用
  return true unless version_enable
  # 只有 body, title, slug 更改了才更新版本
  if self.body_changed? || self.title_changed? || self.slug_changed?
    update_column(:version, self.version + 1)
    PageVersion.create(user_id: user_id,
                       page_id: id,
                       desc: change_desc || '',
                       version: version,
                       body: body,
                       title: title,
                       slug: slug)
  end
end

#editorsObject


61
62
63
# File 'app/models/page.rb', line 61

def editors
  User.where(id: editor_ids)
end

#revert_version(version) ⇒ Object

撤掉到指定版本


53
54
55
56
57
58
59
# File 'app/models/page.rb', line 53

def revert_version(version)
  page_version = PageVersion.where(page_id: id, version: version).first
  return false if page_version.blank?
  update_attributes(body: page_version.body,
                    title: page_version.title,
                    slug: page_version.slug)
end

#to_paramObject


48
49
50
# File 'app/models/page.rb', line 48

def to_param
  slug
end