Class: Spree::Theme

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/theme.rb

Constant Summary collapse

DEFAULT_NAME =
%w(default)
DEFAULT_STATE =
'drafted'
TEMPLATE_FILE_CONTENT_TYPE =
'application/zip'
STATES =
%w(drafted compiled published)
THEMES_PATH =
File.join(Rails.root, 'public', 'vinsol_spree_themes')
CURRENT_THEME_PATH =
File.join(THEMES_PATH, 'current')
ASSET_CACHE_PATH =
File.join(Rails.root, 'tmp', 'cache')

Instance Method Summary collapse

Instance Method Details

#apply_new_themeObject



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

def apply_new_theme
  source_path = File.join(THEMES_PATH, name)
  FileUtils.ln_sf(source_path, CURRENT_THEME_PATH)
  AssetsPrecompilerService.new(self).copy_assets
end

#assets_precompileObject



82
83
84
# File 'app/models/spree/theme.rb', line 82

def assets_precompile
  AssetsPrecompilerService.new(self).minify
end

#close_previewObject



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

def close_preview
  remove_cache
  # update_cache_timestamp
end

#draftedObject

STATE MACHINES ##



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/spree/theme.rb', line 49

state_machine initial: :drafted do
  before_transition drafted: :compiled do |theme, transition|
    begin
      theme.assets_precompile
      theme.update_cache_timestamp
    rescue Exception => e
      theme.errors.add(:base, e)
    end
  end

  before_transition compiled: :published do |theme, transition|
    begin
      theme.remove_current_theme
      theme.apply_new_theme
      theme.update_cache_timestamp
    rescue Exception => e
      theme.errors.add(:base, e)
    end
  end

  event :draft do
    transition from: [:published, :compiled], to: :drafted
  end

  event :compile do
    transition from: :drafted, to: :compiled
  end

  event :publish do
    transition from: [:compiled, :drafted], to: :published
  end
end

#open_previewObject



97
98
99
100
101
102
103
# File 'app/models/spree/theme.rb', line 97

def open_preview
  precompile_assets = AssetsPrecompilerService.new(self)
  precompile_assets.minify
  precompile_assets.copy_preview_assets
  remove_cache
  # update_cache_timestamp
end

#publishedObject

SCOPES ##



41
# File 'app/models/spree/theme.rb', line 41

scope :published, -> { where(state: 'published') }

#remove_current_themeObject



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

def remove_current_theme
  Spree::Theme.published.each(&:draft)
  File.delete(CURRENT_THEME_PATH) if File.exist?(CURRENT_THEME_PATH)
end

#template_fileObject

VALIDATIONS ##



16
17
# File 'app/models/spree/theme.rb', line 16

validates_attachment :template_file, presence: true,
content_type: { content_type: TEMPLATE_FILE_CONTENT_TYPE }

#themes_templatesObject Also known as: templates

ASSOCIATIONS ##



28
# File 'app/models/spree/theme.rb', line 28

has_many :themes_templates, dependent: :destroy

#update_cache_timestampObject



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

def update_cache_timestamp
  Rails.cache.write(Spree::ThemesTemplate::CacheResolver.cache_key, Time.current)
end