Class: Spree::Theme

Inherits:
Object
  • Object
show all
Includes:
Previewable, SingleStoreResource
Defined in:
app/models/spree/theme.rb

Direct Known Subclasses

Spree::Themes::Default

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Previewable

#preview?

Class Method Details

.authorsObject



76
77
78
# File 'app/models/spree/theme.rb', line 76

def self.authors
  [:authors]
end

.available_themesArray<Spree::Theme>

Returns an array of available themes, sorted by display name

We need to load the theme classes to get the display name, so we also load the page classes at the same time

Returns:



58
59
60
# File 'app/models/spree/theme.rb', line 58

def self.available_themes
  @available_themes ||= Rails.application.config.spree.themes.sort_by(&:display_name)
end

.descriptionObject



84
85
86
# File 'app/models/spree/theme.rb', line 84

def self.description
  [:description]
end

.display_nameObject



72
73
74
# File 'app/models/spree/theme.rb', line 72

def self.display_name
  [:name].presence || name.demodulize.titleize
end

.licenseObject



80
81
82
# File 'app/models/spree/theme.rb', line 80

def self.license
  [:license]
end

.metadataObject



62
63
64
65
66
67
68
69
70
# File 'app/models/spree/theme.rb', line 62

def self.
  {
    authors: [], # eg. ['Spree Commerce']
    website: '', # eg. 'https://spreecommerce.org'
    license: '', # eg. 'MIT', https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/licensing-a-repository#searching-github-by-license-type
    description: '', # eg. 'A theme for Spree Commerce'
    preview_image_url: '' # eg. 'https://example.com/screenshot.png'
  }
end

.preview_image_urlObject



88
89
90
# File 'app/models/spree/theme.rb', line 88

def self.preview_image_url
  [:preview_image_url]
end

.to_paramObject

Class methods



49
50
51
# File 'app/models/spree/theme.rb', line 49

def self.to_param
  Spree::Theme.to_s
end

.websiteObject



92
93
94
# File 'app/models/spree/theme.rb', line 92

def self.website
  [:website]
end

Instance Method Details

#available_layout_sectionsArray<Class>

Returns an array of available layout section classes for the theme, eg. header, footer, newsletter, etc.

Returns:

  • (Array<Class>)


161
162
163
164
165
166
# File 'app/models/spree/theme.rb', line 161

def available_layout_sections
  [
    *Rails.application.config.spree.theme_layout_sections,
    *custom_layout_sections
  ]
end

#available_page_sectionsArray<Class>

Returns an array of available page section classes for the theme

Returns:

  • (Array<Class>)


180
181
182
183
184
185
186
187
188
189
# File 'app/models/spree/theme.rb', line 180

def available_page_sections
  return @available_page_sections if @available_page_sections

  @available_page_sections ||= [
    *Rails.application.config.spree.page_sections.find_all do |section_class|
      section_class.role == 'content'
    end,
    *custom_page_sections
  ].sort_by(&:name)
end

#create_default_pagesObject



100
101
102
103
104
105
106
# File 'app/models/spree/theme.rb', line 100

def create_default_pages
  Rails.application.config.spree.pages.map(&:to_s).map(&:constantize).each do |page_class|
    next if page_class == Spree::Pages::Custom

    page_class.where(pageable: self).first_or_create!
  end
end

#create_layout_sectionsObject



108
109
110
111
112
113
114
# File 'app/models/spree/theme.rb', line 108

def create_layout_sections
  ApplicationRecord.transaction do
    available_layout_sections.map(&:to_s).map(&:constantize).each do |section_class|
      section_class.where(pageable: self).first_or_create!
    end
  end
end

#create_previewSpree::Theme

Creates a new preview for the theme

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/spree/theme.rb', line 119

def create_preview
  ActiveRecord::Base.connected_to(role: :writing) do
    ApplicationRecord.transaction do
      new_preview = dup
      new_preview.parent = self
      new_preview.duplicating = true
      new_preview.default = false

      # we need to deep clone layout sections and their assets
      sections.includes(:links, { asset_attachment: :blob }, { blocks: [:rich_text_text, :links] }).each do |section|
        section.deep_clone(new_preview)
      end

      new_preview.save!
      new_preview
    end
  end
end

#custom_layout_sectionsArray<Class>

Returns an array of custom layout section classes for the theme

Returns:

  • (Array<Class>)


171
172
173
174
175
# File 'app/models/spree/theme.rb', line 171

def custom_layout_sections
  # you can override this method in your theme to return a list of custom layout sections for your theme
  # [Spree::PageSections::Custom, Spree::PageSections::Custom2]
  []
end

#custom_page_sectionsArray<Class>

Returns an array of custom page section classes for the theme

Returns:

  • (Array<Class>)


194
195
196
197
198
# File 'app/models/spree/theme.rb', line 194

def custom_page_sections
  # you can override this method in your theme to return a list of custom page sections for your theme
  # [Spree::PageSections::Custom, Spree::PageSections::Custom2]
  []
end

#duplicateObject



96
97
98
# File 'app/models/spree/theme.rb', line 96

def duplicate
  Themes::Duplicator.new(self).duplicate
end

#promoteObject

Promotes the preview to the main theme



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/spree/theme.rb', line 139

def promote
  return unless preview?

  ApplicationRecord.transaction do
    old_theme = parent

    # clear reference to the old theme and set default to the old theme's default
    update!(parent: nil, default: old_theme.default)

    # move pages to the new theme
    old_theme.pages.update_all(pageable_id: id)

    # destroy the old theme with their other previews, etc.
    store.themes.find(old_theme.id).destroy

    take_screenshot # update the screenshot
  end
end

#restore_defaults!Object



200
201
202
203
# File 'app/models/spree/theme.rb', line 200

def restore_defaults!
  self.preferences = {}
  save!
end

#take_screenshotObject



205
206
207
208
209
210
211
# File 'app/models/spree/theme.rb', line 205

def take_screenshot
  return if Spree.screenshot_api_token.blank?
  return if preview? # we don't want to take screenshots of previews, they aren't surfaced in the UI
  return if screenshot.attached?

  Spree::Themes::ScreenshotJob.perform_later(id)
end