Class: Spree::Page

Inherits:
Object
  • Object
show all
Extended by:
FriendlyId
Includes:
Linkable, Previewable
Defined in:
app/models/spree/page.rb

Instance Method Summary collapse

Methods included from Previewable

#preview?

Instance Method Details

#create_previewObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/spree/page.rb', line 67

def create_preview
  return self if preview? # no need to create a preview for the preview

  ActiveRecord::Base.connected_to(role: :writing) do
    ApplicationRecord.transaction do
      new_preview = dup
      new_preview.parent = self
      new_preview.duplicating = true
      new_preview.save!

      sections.includes(:links, { asset_attachment: :blob }, { blocks: [:rich_text_text, :links] }).each do |section|
        section.deep_clone(new_preview)
      end

      new_preview
    end
  end
end

#custom?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'app/models/spree/page.rb', line 128

def custom?
  type == 'Spree::Pages::Custom'
end

#customizable?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/spree/page.rb', line 102

def customizable?
  false
end

#default_sectionsObject



98
99
100
# File 'app/models/spree/page.rb', line 98

def default_sections
  []
end

#display_nameObject



120
121
122
123
124
125
126
# File 'app/models/spree/page.rb', line 120

def display_name
  if custom?
    name
  else
    name.titleize
  end
end

#duplicate(target_pageable) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/spree/page.rb', line 132

def duplicate(target_pageable)
  duplicated_page = dup
  duplicated_page.duplicating = true
  duplicated_page.pageable = target_pageable
  duplicated_page.save!

  sections_scope = sections.includes(:links, :blocks, { asset_attachment: :blob },
                                     { blocks: [:rich_text_text, :links, { asset_attachment: :blob }] })
  sections_scope.each { |section| section.deep_clone(duplicated_page) }

  duplicated_page
end

#icon_nameObject



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

def icon_name
  'file-earmark-richtext'
end

#layout_sections?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/spree/page.rb', line 110

def layout_sections?
  true
end

#linkable?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/spree/page.rb', line 106

def linkable?
  false
end

#preview_url(theme_preview = nil, page_preview = nil) ⇒ Object



114
115
116
117
118
# File 'app/models/spree/page.rb', line 114

def preview_url(theme_preview = nil, page_preview = nil)
  return if page_builder_url.blank?

  "#{page_builder_url}?#{url_query_params(theme_preview, page_preview).to_query}"
end

#promoteObject

Promotes the page preview to the main page



87
88
89
90
91
92
93
94
95
96
# File 'app/models/spree/page.rb', line 87

def promote
  return unless preview?

  ApplicationRecord.transaction do
    old_page = parent
    old_page.page_links.update_all(linkable_id: id)
    update(parent: nil) # clear reference to the old page
    Spree::Page.find(old_page.id).destroy # destroy the old page with their other previews, etc.
  end
end

#storeObject



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

def store
  if pageable.is_a?(Spree::Store)
    pageable
  else
    pageable.store
  end
end

#themeObject



145
146
147
148
149
150
151
# File 'app/models/spree/page.rb', line 145

def theme
  @theme ||= if pageable.is_a?(Spree::Theme)
               pageable
             else
               pageable.default_theme
             end
end