Class: Dugway::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/dugway/theme.rb

Constant Summary collapse

REQUIRED_FILES =
%w( cart.html contact.html home.html layout.html maintenance.html product.html products.html screenshot.jpg settings.json theme.css theme.js )
THEME_COLOR_ATTRIBUTE_MAPPINGS =
YAML.load_file(
  File.join(__dir__, 'config', 'theme_color_attribute_mappings.yml')
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overridden_customization = {}) ⇒ Theme

Returns a new instance of Theme.



18
19
20
# File 'lib/dugway/theme.rb', line 18

def initialize(overridden_customization={})
  @overridden_customization = overridden_customization.stringify_keys
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/dugway/theme.rb', line 16

def errors
  @errors
end

Instance Method Details

#build_file(name) ⇒ Object



75
76
77
78
# File 'lib/dugway/theme.rb', line 75

def build_file(name)
  @building = true
  file_content(name)
end

#customizationObject



42
43
44
45
46
47
48
49
50
# File 'lib/dugway/theme.rb', line 42

def customization
  Hash.new.tap do |customization|
    %w( fonts colors options images image_sets ).each do |type|
      customization.update(customization_for_type(type))
    end

    customization.update(@overridden_customization)
  end
end

#file_content(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dugway/theme.rb', line 60

def file_content(name)
  case name
  when 'theme.js'
    if @building
      Terser.new.compile(sprockets[name].to_s)
    else
      sprockets[name].to_s
    end
  when 'theme.css'
    sprockets[name].to_s
  else
    read_source_file(name)
  end
end

#filesObject



80
81
82
# File 'lib/dugway/theme.rb', line 80

def files
  REQUIRED_FILES + image_files + font_files
end

#font_filesObject



90
91
92
93
94
# File 'lib/dugway/theme.rb', line 90

def font_files
  Dir.glob(File.join(source_dir, 'fonts', '**', '*.{eot,ttf,otf,woff,svg}')).map do |i|
    i.gsub(source_dir, '')[1..-1]
  end
end

#fontsObject



30
31
32
# File 'lib/dugway/theme.rb', line 30

def fonts
  customization_for_type('fonts')
end

#image_filesObject



84
85
86
87
88
# File 'lib/dugway/theme.rb', line 84

def image_files
  Dir.glob(File.join(source_dir, 'images', '**', '*.{png,jpg,jpeg,gif,ico,svg}')).map do |i|
    i.gsub(source_dir, '')[1..-1]
  end
end

#image_setsObject



38
39
40
# File 'lib/dugway/theme.rb', line 38

def image_sets
  customization_for_type('image_sets')
end

#imagesObject



34
35
36
# File 'lib/dugway/theme.rb', line 34

def images
  customization_for_type('images')
end

#layoutObject



22
23
24
# File 'lib/dugway/theme.rb', line 22

def layout
  read_source_file('layout.html')
end

#nameObject



52
53
54
# File 'lib/dugway/theme.rb', line 52

def name
  settings['name']
end

#settingsObject



26
27
28
# File 'lib/dugway/theme.rb', line 26

def settings
  JSON.parse(read_source_file('settings.json'))
end

#valid?(validate_colors: true, validate_layout_attributes: true) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dugway/theme.rb', line 96

def valid?(validate_colors: true, validate_layout_attributes: true)
  @errors = []

  REQUIRED_FILES.each do |file|
    @errors << "Missing source/#{ file }" if read_source_file(file).nil?
  end

  @errors << 'Missing theme name in source/settings.json' if name.blank?
  @errors << 'Invalid theme version in source/settings.json (ex: 1.0.3)' unless !!(version =~ /\d+\.\d+\.\d+/)

  if settings['preset_styles']
    validate_preview
    if settings['preset_styles']['presets']
      settings['preset_styles']['presets'].each do |preset|
        validate_preset_styles(preset)
        validate_style_references(preset)
      end
    else
      @errors << "Missing presets"
    end
  end

  validate_required_color_settings if validate_colors
  validate_required_layout_attributes if validate_layout_attributes

  @errors.empty?
end

#validate_required_color_settingsObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dugway/theme.rb', line 124

def validate_required_color_settings
  required_colors_attribute_names = THEME_COLOR_ATTRIBUTE_MAPPINGS['required_color_attributes']

  theme_colors = settings['colors'].map { |c| c['variable'] }
  mappings = THEME_COLOR_ATTRIBUTE_MAPPINGS[name] || {}

  missing_colors = required_colors_attribute_names.reject do |color|
    theme_colors.include?(color) ||
    (mappings.key?(color) && (mappings[color].nil? || theme_colors.include?(mappings[color])))
  end

  unless missing_colors.empty?
    @errors << "Missing required color settings: #{missing_colors.join(', ')}"
  end
end

#validate_required_layout_attributesObject

Validate that the Layout file has expected attributes for:

  • data-bc-page-type on the body tag

  • one data-bc-hook=“header” and one data-bc-hook=“footer” somewhere



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dugway/theme.rb', line 143

def validate_required_layout_attributes
  layout_content = read_source_file('layout.html')

  unless layout_content =~ /<body.*?\bdata-bc-page-type\b/
    @errors << "layout.html missing `data-bc-page-type` attribute on body tag"
  end

  header_hooks = layout_content.scan(/data-bc-hook=(?:"|')header(?:"|')/).size
  footer_hooks = layout_content.scan(/data-bc-hook=(?:"|')footer(?:"|')/).size

  @errors << "layout.html must have exactly one `data-bc-hook=\"header\"`" if header_hooks != 1
  @errors << "layout.html must have exactly one `data-bc-hook=\"footer\"`" if footer_hooks != 1
end

#versionObject



56
57
58
# File 'lib/dugway/theme.rb', line 56

def version
  settings['version']
end