Class: Page

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#change_descObject

Returns the value of attribute change_desc.



11
12
13
# File 'app/models/page.rb', line 11

def change_desc
  @change_desc
end

#user_idObject

Returns the value of attribute user_id.



11
12
13
# File 'app/models/page.rb', line 11

def user_id
  @user_id
end

#version_enableObject

Returns the value of attribute version_enable.



11
12
13
# File 'app/models/page.rb', line 11

def version_enable
  @version_enable
end

Class Method Details

.find_by_slug(slug) ⇒ Object



71
72
73
# File 'app/models/page.rb', line 71

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

Instance Method Details

#append_editorObject



21
22
23
24
25
# File 'app/models/page.rb', line 21

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

#as_indexed_json(_options = {}) ⇒ Object



46
47
48
# File 'app/models/page.rb', line 46

def as_indexed_json(_options = {})
  as_json(only: [:slug, :title, :body])
end

#create_versionObject



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

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



67
68
69
# File 'app/models/page.rb', line 67

def editors
  User.where(id: editor_ids)
end

#indexed_changed?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/page.rb', line 50

def indexed_changed?
  slug_changed? || title_changed? || body_changed?
end

#revert_version(version) ⇒ Object

撤掉到指定版本



59
60
61
62
63
64
65
# File 'app/models/page.rb', line 59

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

#to_paramObject



54
55
56
# File 'app/models/page.rb', line 54

def to_param
  slug
end