Class: Spree::Theme
- Inherits:
-
Base
- Object
- Base
- Spree::Theme
- 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
- #apply_new_theme ⇒ Object
- #assets_precompile ⇒ Object
- #close_preview ⇒ Object
-
#drafted ⇒ Object
STATE MACHINES ##.
- #open_preview ⇒ Object
-
#published ⇒ Object
SCOPES ##.
- #remove_current_theme ⇒ Object
-
#template_file ⇒ Object
VALIDATIONS ##.
-
#themes_templates ⇒ Object
(also: #templates)
ASSOCIATIONS ##.
- #update_cache_timestamp ⇒ Object
Instance Method Details
#apply_new_theme ⇒ Object
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_precompile ⇒ Object
82 83 84 |
# File 'app/models/spree/theme.rb', line 82 def assets_precompile AssetsPrecompilerService.new(self).minify end |
#close_preview ⇒ Object
105 106 107 108 |
# File 'app/models/spree/theme.rb', line 105 def close_preview remove_cache # update_cache_timestamp end |
#drafted ⇒ Object
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. 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. 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_preview ⇒ Object
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 |
#published ⇒ Object
SCOPES ##
41 |
# File 'app/models/spree/theme.rb', line 41 scope :published, -> { where(state: 'published') } |
#remove_current_theme ⇒ Object
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_file ⇒ Object
VALIDATIONS ##
16 17 |
# File 'app/models/spree/theme.rb', line 16 :template_file, presence: true, content_type: { content_type: TEMPLATE_FILE_CONTENT_TYPE } |
#themes_templates ⇒ Object Also known as: templates
ASSOCIATIONS ##
28 |
# File 'app/models/spree/theme.rb', line 28 has_many :themes_templates, dependent: :destroy |
#update_cache_timestamp ⇒ Object
110 111 112 |
# File 'app/models/spree/theme.rb', line 110 def Rails.cache.write(Spree::ThemesTemplate::CacheResolver.cache_key, Time.current) end |