Module: Spree::ThemeHelper
- Defined in:
- app/helpers/spree/theme_helper.rb
Instance Method Summary collapse
- #block_attributes(block, allowed_styles: :all) ⇒ Object
- #block_background_color_style(block) ⇒ Object
- #block_css_classes(block) ⇒ Object
- #block_styles(block, allowed_styles: :all) ⇒ Object
- #current_header_logo ⇒ Object
- #current_page ⇒ Object
- #current_page_or_preview ⇒ Object
- #current_page_preview ⇒ Object
- #current_theme ⇒ Object
- #current_theme_or_preview ⇒ Object
- #current_theme_preview ⇒ Object
- #hex_color_to_rgb(hex) ⇒ Object
- #hex_color_to_rgba(hex) ⇒ Object
- #link_attributes(link, as_html: true) ⇒ Object
- #page_builder_enabled? ⇒ Boolean
- #section_heading_styles(section) ⇒ Object
- #section_styles(section) ⇒ Object
- #theme_layout_sections ⇒ Object
- #theme_setting(name) ⇒ Object
-
#theme_setting_rgb_components(name) ⇒ Object
This helper allows us to specify opacity in Tailwind’s color palette.
Instance Method Details
#block_attributes(block, allowed_styles: :all) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/helpers/spree/theme_helper.rb', line 131 def block_attributes(block, allowed_styles: :all) has_width_desktop = block.respond_to?(:preferred_width_desktop) && block.preferred_width_desktop.present? ? "width-desktop='true'" : nil attributes = { data: { editor_id: "block-#{block.id}", editor_name: block.display_name, editor_parent_id: "section-#{block.section_id}", editor_link: spree.respond_to?(:edit_admin_page_section_block_path) ? spree.edit_admin_page_section_block_path(block.section, block) : nil }, id: "block-#{block.id}", class: "block-#{block.class.name.demodulize.underscore.dasherize}", style: block_styles(block, allowed_styles: allowed_styles), width_desktop: has_width_desktop }.compact_blank tag.attributes(attributes) end |
#block_background_color_style(block) ⇒ Object
232 233 234 235 236 |
# File 'app/helpers/spree/theme_helper.rb', line 232 def block_background_color_style(block) return nil unless block.respond_to?(:preferred_background_color) && block.preferred_background_color.present? "background-color: #{block.preferred_background_color};" end |
#block_css_classes(block) ⇒ Object
238 239 240 241 242 |
# File 'app/helpers/spree/theme_helper.rb', line 238 def block_css_classes(block) classes = [] classes << "justify-#{block.preferred_justify}" if block.respond_to?(:preferred_justify) && block.preferred_justify.present? classes.join(',') end |
#block_styles(block, allowed_styles: :all) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'app/helpers/spree/theme_helper.rb', line 187 def block_styles(block, allowed_styles: :all) styles = {} styles['text-align'] = block.preferred_text_alignment if block.respond_to?(:preferred_text_alignment) && block.preferred_text_alignment.present? styles['width'] = "#{block.preferred_width_desktop}%" if block.respond_to?(:preferred_width_desktop) && block.preferred_width_desktop.present? if block.respond_to?(:preferred_container_alignment) styles['margin'] = case block.preferred_container_alignment when 'center' '0 auto' when 'right' '0 0 0 auto' else '0 auto 0 0' end end styles['color'] = if block.respond_to?(:preferred_text_color) && block.preferred_text_color.present? block.preferred_text_color else 'var(--section-color)' end styles['padding-top'] = "#{block.preferred_top_padding}px" if block.respond_to?(:preferred_top_padding) && block.preferred_top_padding.present? if block.respond_to?(:preferred_bottom_padding) && block.preferred_bottom_padding.present? styles['padding-bottom'] = "#{block.preferred_bottom_padding}px" end styles['text-transform'] = :uppercase if theme_setting('headings_uppercase') && block.type == 'heading' styles['background-color'] = if block.respond_to?(:preferred_background_color) && block.preferred_background_color.present? block.preferred_background_color else 'var(--section-background)' end if block.respond_to?(:preferred_button_background_color) && block..present? styles['--button-background-color'] = block. end if block.respond_to?(:preferred_button_text_color) && block..present? styles['--button-text-color'] = block. end styles = styles.compact_blank styles = styles.slice(*allowed_styles) if allowed_styles != :all styles.map { |k, v| "#{k}: #{v}" }.join(';') end |
#current_header_logo ⇒ Object
37 38 39 |
# File 'app/helpers/spree/theme_helper.rb', line 37 def current_header_logo @current_header_logo ||= current_theme_or_preview.sections.find_by(type: 'Spree::PageSections::Header')&.logo end |
#current_page ⇒ Object
3 4 5 |
# File 'app/helpers/spree/theme_helper.rb', line 3 def current_page @current_page ||= current_theme.pages.find_by(type: 'Spree::Pages::Homepage') end |
#current_page_or_preview ⇒ Object
29 30 31 |
# File 'app/helpers/spree/theme_helper.rb', line 29 def current_page_or_preview @current_page_or_preview ||= current_page_preview || current_page end |
#current_page_preview ⇒ Object
23 24 25 26 27 |
# File 'app/helpers/spree/theme_helper.rb', line 23 def current_page_preview return if params[:page_preview_id].blank? @current_page_preview ||= current_page.previews.find_by(id: params[:page_preview_id]) end |
#current_theme ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'app/helpers/spree/theme_helper.rb', line 7 def current_theme @current_theme ||= if params[:theme_id].present? current_store.themes.find_by(id: params[:theme_id]) else current_store.default_theme || current_store.themes.first end ensure @current_theme ||= current_store.themes.first end |
#current_theme_or_preview ⇒ Object
33 34 35 |
# File 'app/helpers/spree/theme_helper.rb', line 33 def current_theme_or_preview @current_theme_or_preview ||= current_theme_preview || current_theme end |
#current_theme_preview ⇒ Object
17 18 19 20 21 |
# File 'app/helpers/spree/theme_helper.rb', line 17 def current_theme_preview return if params[:theme_preview_id].blank? @current_theme_preview ||= current_theme.previews.find_by(id: params[:theme_preview_id]) end |
#hex_color_to_rgb(hex) ⇒ Object
76 77 78 79 80 81 |
# File 'app/helpers/spree/theme_helper.rb', line 76 def hex_color_to_rgb(hex) return unless hex.present? rgb = hex[0..6].match(/^#(..)(..)(..)$/).captures.map(&:hex) "rgb(#{rgb.join(', ')})" end |
#hex_color_to_rgba(hex) ⇒ Object
83 84 85 86 87 88 89 |
# File 'app/helpers/spree/theme_helper.rb', line 83 def hex_color_to_rgba(hex) return unless hex.present? *rgb, alpha = hex.match(/^#(..)(..)(..)(..)?$/).captures.map { |hex_pair| hex_pair&.hex } opacity = (alpha || 255) / 255.0 "rgba(#{rgb.join(', ')}, #{opacity.round(2)})" end |
#link_attributes(link, as_html: true) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/helpers/spree/theme_helper.rb', line 150 def link_attributes(link, as_html: true) parent_type = case link.parent_type when 'Spree::PageSection' :section when 'Spree::PageBlock' :block else return '' end attributes = if spree.respond_to?(:admin) && spree.respond_to?(:edit_admin_page_link_path) && spree.respond_to?(:edit_admin_page_section_block_path) { data: { editor_id: "link-#{link.id}", editor_name: link.label, editor_parent_id: "#{parent_type}-#{link.parent_id}", editor_link: case parent_type when :section spree.edit_admin_page_link_path(link, page_section_id: link.parent_id) when :block spree.edit_admin_page_link_path(link, block_id: link.parent_id) end }, id: "link-#{link.id}", class: "link-#{link.class.name.demodulize.underscore.dasherize}" } else {} end if as_html tag.attributes(attributes) else attributes end end |
#page_builder_enabled? ⇒ Boolean
41 42 43 |
# File 'app/helpers/spree/theme_helper.rb', line 41 def page_builder_enabled? @page_builder_enabled ||= (current_theme_preview.present? || current_page_preview.present?) && params[:page_builder] == 'true' end |
#section_heading_styles(section) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/helpers/spree/theme_helper.rb', line 119 def section_heading_styles(section) styles = {} styles['text-transform'] = :uppercase if theme_setting('headings_uppercase') if section.respond_to?(:preferred_heading_bottom_padding) && section.preferred_heading_bottom_padding.present? styles['padding-bottom'] = "#{section.preferred_heading_bottom_padding}px" end styles.compact_blank.map { |k, v| "#{k}: #{v}" }.join(';') end |
#section_styles(section) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/helpers/spree/theme_helper.rb', line 91 def section_styles(section) styles = {} bg_color = section.preferred_background_color.presence || theme_setting('background_color') styles['background-color'] = bg_color styles['--section-background'] = bg_color text_color = section.preferred_text_color.presence || theme_setting('text_color') styles['color'] = text_color styles['--section-color'] = text_color styles['border-color'] = section.preferred_border_color.presence || theme_setting('border_color') styles['padding-top'] = "#{section.preferred_top_padding.presence}px" styles['padding-bottom'] = "#{section.preferred_bottom_padding.presence}px" styles['border-top-width'] = "#{section.preferred_top_border_width.presence}px" border_bottom_width = "#{section.preferred_bottom_border_width.presence}px" styles['border-bottom-width'] = border_bottom_width styles['--section--border-bottom-width'] = border_bottom_width if section.respond_to?(:preferred_button_text_color) && section..present? styles['--button-text-color'] = section. end if section.respond_to?(:preferred_button_background_color) && section..present? styles['--button-background-color'] = section. end styles.map { |k, v| "#{k}: #{v}" }.join(';') end |
#theme_layout_sections ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/helpers/spree/theme_helper.rb', line 45 def theme_layout_sections @theme_layout_sections ||= current_theme_or_preview.sections.includes(:links, { asset_attachment: :blob }, { blocks: [:rich_text_text, :links] }).all.each_with_object({}) do |section, hash| hash[section.type.to_s.demodulize.underscore] = section end rescue StandardError => e raise e unless Rails.env.production? Rails.error.report(e, context: { theme_id: current_theme_or_preview.id }, source: 'spree.storefront') {} end |
#theme_setting(name) ⇒ Object
58 59 60 61 62 63 64 |
# File 'app/helpers/spree/theme_helper.rb', line 58 def theme_setting(name) if current_theme_preview.present? current_theme_preview.preferences.with_indifferent_access[name] elsif current_theme.present? current_theme.preferences.with_indifferent_access[name] end end |
#theme_setting_rgb_components(name) ⇒ Object
This helper allows us to specify opacity in Tailwind’s color palette
67 68 69 70 71 72 73 |
# File 'app/helpers/spree/theme_helper.rb', line 67 def theme_setting_rgb_components(name) hex_color = theme_setting(name) return unless hex_color.present? rgb = hex_color[0..6].match(/^#(..)(..)(..)$/).captures.map(&:hex) rgb.join(', ') end |