Class: PufferPages::Backends::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveUUID::UUID, Mixins::Importable, Mixins::Localable, Mixins::Renderable
Defined in:
lib/puffer_pages/backends/models/page.rb

Direct Known Subclasses

Page

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Localable

#locales

Methods included from Mixins::Renderable

#template

Class Method Details

.controller_scope(scope) ⇒ Object



23
24
25
# File 'lib/puffer_pages/backends/models/page.rb', line 23

def self.controller_scope scope
  where scope
end

.export_jsonObject



66
67
68
69
70
# File 'lib/puffer_pages/backends/models/page.rb', line 66

def self.export_json
  includes(:page_parts).order(:lft).as_json(
    include: :page_parts, except: [:lft, :rgt, :depth, :location]
  )
end

.find_page(location) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


31
32
33
34
35
36
37
38
# File 'lib/puffer_pages/backends/models/page.rb', line 31

def self.find_page location
  location = normalize_path(location)
  page = PufferPages.single_section_page_path ?
    find_by_slug(location) : find_by_location(location)
  raise ActiveRecord::RecordNotFound unless page
  raise PufferPages::DraftPage.new("PufferPages can`t show page `#{page.location}` because it is draft") if page.draft?
  page
end

.find_view_page(location, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puffer_pages/backends/models/page.rb', line 40

def self.find_view_page location, options = {}
  location = normalize_path(location)
  depth = location.to_s.count('/').next
  formats = options[:formats].presence || [:html]

  page = if location.blank?
    roots.first
  else
    formats.inject(nil) do |page, format|
      page ||= begin
        loc = if format == :html
          where(['? like location', location])
        else
          where(['? like location', [location, format].join('.')]).where(['not ? like location', [location, format, ''].join('.')])
        end
        loc.where(['status not in (?)', 'draft']).where(:"#{depth_column_name}" => depth).order('lft desc').first
      end
    end
  end

  raise PufferPages::LayoutMissed.new(
    "PufferPages can`t render page for `#{location}` with formats `#{formats.join('`, `')}` because layout page missed or draft"
  ) unless page
  page
end

.import_destroyObject



72
73
74
# File 'lib/puffer_pages/backends/models/page.rb', line 72

def self.import_destroy
  roots.destroy_all
end

.inherited(base) ⇒ Object



12
13
14
15
# File 'lib/puffer_pages/backends/models/page.rb', line 12

def self.inherited base
  base.acts_as_nested_set
  super
end

.normalize_path(path) ⇒ Object



27
28
29
# File 'lib/puffer_pages/backends/models/page.rb', line 27

def self.normalize_path path
  path.to_s.split('/').delete_if(&:blank?).join('/').presence
end

.statusesObject



19
20
21
# File 'lib/puffer_pages/backends/models/page.rb', line 19

def self.statuses
  %w(draft hidden published)
end

Instance Method Details

#additional_render_optionsObject



190
191
192
# File 'lib/puffer_pages/backends/models/page.rb', line 190

def additional_render_options
  { registers: { page: self }, drops: { page: self, self: self } }
end

#ancestors_page_partsObject



138
139
140
# File 'lib/puffer_pages/backends/models/page.rb', line 138

def ancestors_page_parts
  self_and_ancestors_page_parts.where('pages.id != ?', id)
end

#content_typeObject



214
215
216
# File 'lib/puffer_pages/backends/models/page.rb', line 214

def content_type
  Rack::Mime.mime_type(File.extname(slug.to_s), 'text/html')
end

#current_layoutObject



202
203
204
# File 'lib/puffer_pages/backends/models/page.rb', line 202

def current_layout
  @current_layout ||= inherited_layout_page.try(:layout)
end

#defaultize_attributesObject



103
104
105
106
107
# File 'lib/puffer_pages/backends/models/page.rb', line 103

def defaultize_attributes
  self.status ||= 'draft'
  self.slug = slug.presence
  self.location = [parent.try(:location), slug].compact.join('/').presence
end

#formatObject



134
135
136
# File 'lib/puffer_pages/backends/models/page.rb', line 134

def format
  File.extname(slug)[1..-1].to_s.to_sym.presence || :html
end

#inherited_layoutObject



206
207
208
# File 'lib/puffer_pages/backends/models/page.rb', line 206

def inherited_layout
  @inherited_layout ||= PufferPages::Layout.find_layout(current_layout.name) if current_layout
end

#inherited_layout_nameObject



198
199
200
# File 'lib/puffer_pages/backends/models/page.rb', line 198

def inherited_layout_name
  @inherited_layout_name ||= inherited_layout_page.try(:layout_name)
end

#inherited_layout_pageObject



194
195
196
# File 'lib/puffer_pages/backends/models/page.rb', line 194

def inherited_layout_page
  @inherited_layout_page ||= layout_name? ? self : parent.try(:inherited_layout_page)
end

#inherited_page_part(name) ⇒ Object



152
153
154
# File 'lib/puffer_pages/backends/models/page.rb', line 152

def inherited_page_part name
  inherited_page_parts.detect { |part| part.name == name }
end

#inherited_page_partsObject



148
149
150
# File 'lib/puffer_pages/backends/models/page.rb', line 148

def inherited_page_parts
  @inherited_page_parts ||= self_and_ancestors_page_parts.group_by(&:name).map { |(_, group)| group.first }
end

#layout_for_renderObject



210
211
212
# File 'lib/puffer_pages/backends/models/page.rb', line 210

def layout_for_render
  "layouts/#{inherited_layout_name}" unless inherited_layout
end

#locales_translationsObject



181
# File 'lib/puffer_pages/backends/models/page.rb', line 181

def locales_translations; locales; end

#locales_translations=(value) ⇒ Object



182
# File 'lib/puffer_pages/backends/models/page.rb', line 182

def locales_translations=(value); self.locales = value; end

#locationObject



109
110
111
# File 'lib/puffer_pages/backends/models/page.rb', line 109

def location
  read_attribute(:location) || [parent.try(:location), slug].compact.join('/').presence
end

#page_translationsObject



184
185
186
187
188
# File 'lib/puffer_pages/backends/models/page.rb', line 184

def page_translations
  self_and_ancestors.each_with_object({}) do |page, result|
    result.deep_merge! page.locales.translations
  end
end

#render(*args) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/puffer_pages/backends/models/page.rb', line 156

def render *args
  source, context = normalize_render_options *args
  context = merge_context context, additional_render_options
  source ||= inherited_layout

  contextualize page_translations: page_translations do
    if source
      if source.respond_to?(:render)
        instrument_render! context do
          source.render context
        end
      else
        render_template source, context
      end
    else
      instrument_render! context do
        inherited_page_parts.map do |part|
          result = part.render context
          part.main? ? result : "<% content_for :'#{part.name}' do %>#{result}<% end %>"
        end.join
      end
    end
  end
end

#segmentsObject



126
127
128
# File 'lib/puffer_pages/backends/models/page.rb', line 126

def segments
  location.to_s.split ?/
end

#self_and_ancestors_page_partsObject



142
143
144
145
146
# File 'lib/puffer_pages/backends/models/page.rb', line 142

def self_and_ancestors_page_parts
  PufferPages::PagePart
    .where("#{q_left} <= ? AND #{q_right} >= ?", left, right)
    .joins(:page).order('name, pages.lft desc')
end

#statusObject



122
123
124
# File 'lib/puffer_pages/backends/models/page.rb', line 122

def status
  ActiveSupport::StringInquirer.new(read_attribute(:status)) if status?
end

#to_liquidObject



218
219
220
# File 'lib/puffer_pages/backends/models/page.rb', line 218

def to_liquid
  @to_liquid ||= ::PufferPages::Liquid::PageDrop.new(self)
end

#to_locationObject



130
131
132
# File 'lib/puffer_pages/backends/models/page.rb', line 130

def to_location
  PufferPages.single_section_page_path ? slug : location
end

#update_locationsObject



114
115
116
# File 'lib/puffer_pages/backends/models/page.rb', line 114

def update_locations
  self.class.update_all "location = replace(location, '#{location_was}', '#{location}')", ["location like ?", location_was + '%']
end