Class: Cmsimple::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cmsimple::Page
- Defined in:
- app/models/cmsimple/page.rb
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #as_published! ⇒ Object
- #at_version!(version_id) ⇒ Object
- #descendants ⇒ Object
- #publish! ⇒ Object
-
#published=(val) ⇒ Object
TODO: refactor me, this smells.
- #published? ⇒ Boolean (also: #published)
- #regions ⇒ Object
- #reify(version) ⇒ Object
- #revert_to!(version_id) ⇒ Object
- #root ⇒ Object
-
#select_label ⇒ Object
TODO: move this to a view delegator/presenter.
- #slug ⇒ Object
- #slug=(val) ⇒ Object
-
#template_render_options ⇒ Object
override in app to set optional rendering parameters like layouts on a per page basis.
- #unpublish! ⇒ Object
- #unpublished_changes? ⇒ Boolean (also: #unpublished_changes)
- #update_content(content) ⇒ Object
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 |
.published ⇒ Object
53 54 55 |
# File 'app/models/cmsimple/page.rb', line 53 def self.published where('cmsimple_pages.published_at <= ?', Time.zone.now.utc) end |
.root ⇒ Object
49 50 51 |
# File 'app/models/cmsimple/page.rb', line 49 def self.root where('cmsimple_pages.is_root = ?', true).limit(1) end |
.unpublished ⇒ Object
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(={}) super(.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 |
#descendants ⇒ Object
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
112 113 114 |
# File 'app/models/cmsimple/page.rb', line 112 def published? self.published_at.present? && self.published_at <= Time.zone.now end |
#regions ⇒ Object
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 |
#root ⇒ Object
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_label ⇒ Object
TODO: move this to a view delegator/presenter
45 46 47 |
# File 'app/models/cmsimple/page.rb', line 45 def select_label "#{' ' * (uri ? (uri.count('/') - 1) : 0)}#{title}".html_safe end |
#slug ⇒ Object
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_options ⇒ Object
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 Cmsimple.configuration. 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
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 |