Class: Cmsimple::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cmsimple/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_parent_select(page) ⇒ Object



36
37
38
39
40
41
42
# File 'app/models/cmsimple/page.rb', line 36

def self.for_parent_select(page)
  scope = scoped
  unless page.new_record?
    scope = scope.where('id NOT IN (?)', page.descendants.map(&:id) + [page.id])
  end
  scope.order('position').all
end

.publishedObject



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

def self.published
  where('cmsimple_pages.published_at <= ?', Time.zone.now.utc)
end

.rootObject



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

def self.root
  where('cmsimple_pages.is_root = ?', true).limit(1)
end

.unpublishedObject



57
58
59
# File 'app/models/cmsimple/page.rb', line 57

def self.unpublished
  where('cmsimple_pages.published_at IS NULL')
end

Instance Method Details

#as_json(options = {}) ⇒ Object



160
161
162
# File 'app/models/cmsimple/page.rb', line 160

def as_json(options={})
  super(options.merge({:methods => [:unpublished_changes, :published]}))
end

#as_published!Object



131
132
133
134
135
136
137
# File 'app/models/cmsimple/page.rb', line 131

def as_published!
  return false unless published?

  readonly!
  reify versions.published.first
  return true
end

#at_version!(version_id) ⇒ Object



139
140
141
142
143
144
145
# File 'app/models/cmsimple/page.rb', line 139

def at_version!(version_id)
  return false unless published?

  readonly!
  reify versions.find(version_id)
  return true
end

#descendantsObject



69
70
71
72
73
74
# File 'app/models/cmsimple/page.rb', line 69

def descendants
  children.all.inject([]) do |ary, child|
    ary << child
    ary + child.descendants
  end.sort_by(&:position)
end

#publish!Object



101
102
103
104
105
# File 'app/models/cmsimple/page.rb', line 101

def publish!
  self.published_at = Time.zone.now
  self.save!
  create_new_version
end

#published=(val) ⇒ Object

TODO: refactor me, this smells



123
124
125
126
127
128
129
# File 'app/models/cmsimple/page.rb', line 123

def published=(val)
  if val.to_bool
    self.publish!
  else
    self.unpublish!
  end
end

#published?Boolean Also known as: published

Returns:

  • (Boolean)


112
113
114
# File 'app/models/cmsimple/page.rb', line 112

def published?
  self.published_at.present? && self.published_at <= Time.zone.now
end

#regionsObject



76
77
78
# File 'app/models/cmsimple/page.rb', line 76

def regions
  @regions ||= RegionsProxy.new(self.content)
end

#reify(version) ⇒ Object



147
148
149
150
151
152
# File 'app/models/cmsimple/page.rb', line 147

def reify(version)
  return unless version.present?
  self.content = version.content
  self.template = version.template
  @regions = RegionsProxy.new(self.content)
end

#revert_to!(version_id) ⇒ Object



154
155
156
157
158
# File 'app/models/cmsimple/page.rb', line 154

def revert_to!(version_id)
  version = self.versions.find(version_id)
  reify version
  save!
end

#rootObject



61
62
63
64
65
66
67
# File 'app/models/cmsimple/page.rb', line 61

def root
  parent = self.parent || self
  while parent.try(:parent)
    parent = parent.parent
  end
  parent
end

#select_labelObject

TODO: move this to a view delegator/presenter



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

def select_label
  "#{'&nbsp;&nbsp;' * (uri ? (uri.count('/') - 1) : 0)}#{title}".html_safe
end

#slugObject



87
88
89
# File 'app/models/cmsimple/page.rb', line 87

def slug
  self[:slug] ||= escape(title)
end

#slug=(val) ⇒ Object



91
92
93
# File 'app/models/cmsimple/page.rb', line 91

def slug=(val)
  self[:slug] = escape(val)
end

#template_render_optionsObject

override in app to set optional rendering parameters like layouts on a per page basis



97
98
99
# File 'app/models/cmsimple/page.rb', line 97

def template_render_options
  Cmsimple.configuration.template_render_options
end

#unpublish!Object



107
108
109
110
# File 'app/models/cmsimple/page.rb', line 107

def unpublish!
  self.published_at = nil
  self.save!
end

#unpublished_changes?Boolean Also known as: unpublished_changes

Returns:

  • (Boolean)


117
118
119
# File 'app/models/cmsimple/page.rb', line 117

def unpublished_changes?
  self.published_at.blank? || (self.updated_at - self.published_at).abs > 1.second
end

#update_content(content) ⇒ Object



80
81
82
83
84
85
# File 'app/models/cmsimple/page.rb', line 80

def update_content(content)
  if content.is_a?(String)
    content = JSON.parse(content)
  end
  update_attributes(content: content)
end