Module: Spree::BaseHelper
- Defined in:
- app/helpers/spree/base_helper.rb
Instance Method Summary collapse
- #available_countries(restrict_to_zone: ) ⇒ Object
- #body_class ⇒ Object
- #display_price(product_or_variant) ⇒ Object
- #flash_messages(opts = {}) ⇒ Object
- #link_to_cart(text = nil) ⇒ Object
- #link_to_tracking(shipment, options = {}) ⇒ Object
- #logo(image_path = ) ⇒ Object
- #meta_data ⇒ Object
- #meta_data_tags ⇒ Object
- #plural_resource_name(resource_class) ⇒ Object
- #pretty_time(time, format = :long) ⇒ Object
- #seo_url(taxon) ⇒ Object
- #taxon_breadcrumbs(taxon, separator = ' » ', breadcrumb_class = 'inline') ⇒ Object
- #taxons_tree(root_taxon, current_taxon, max_level = 1) ⇒ Object
-
#variant_options(variant, _options = {}) ⇒ Object
human readable list of variant options.
Instance Method Details
#available_countries(restrict_to_zone: ) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/helpers/spree/base_helper.rb', line 113 def available_countries(restrict_to_zone: Spree::Config[:checkout_zone]) countries = Spree::Country.available(restrict_to_zone:) country_names = Carmen::Country.all.map do |country| [country.code, country.name] end.to_h country_names.update I18n.t('spree.country_names', default: {}).stringify_keys countries.collect do |country| country.name = country_names.fetch(country.iso, country.name) country end.sort_by { |country| country.name.parameterize } end |
#body_class ⇒ Object
55 56 57 58 |
# File 'app/helpers/spree/base_helper.rb', line 55 def body_class @body_class ||= content_for?(:sidebar) ? 'two-col' : 'one-col' @body_class end |
#display_price(product_or_variant) ⇒ Object
132 133 134 |
# File 'app/helpers/spree/base_helper.rb', line 132 def display_price(product_or_variant) product_or_variant.()&.money&.to_html end |
#flash_messages(opts = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'app/helpers/spree/base_helper.rb', line 64 def (opts = {}) ignore_types = ["order_completed"].concat(Array(opts[:ignore_types]).map(&:to_s) || []) flash.each do |msg_type, text| unless ignore_types.include?(msg_type) concat(content_tag(:div, text, class: "flash #{msg_type}")) end end nil end |
#link_to_cart(text = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/helpers/spree/base_helper.rb', line 7 def link_to_cart(text = nil) text = text ? h(text) : t('spree.cart') css_class = nil if current_order.nil? || current_order.item_count.zero? text = "#{text}: (#{t('spree.empty')})" css_class = 'empty' else text = "#{text}: (#{current_order.item_count}) <span class='amount'>#{current_order.display_total.to_html}</span>" css_class = 'full' end link_to text.html_safe, spree.cart_path, class: "cart-info #{css_class}" end |
#link_to_tracking(shipment, options = {}) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'app/helpers/spree/base_helper.rb', line 140 def link_to_tracking(shipment, = {}) return unless shipment.tracking && shipment.shipping_method if shipment.tracking_url link_to(shipment.tracking, shipment.tracking_url, ) else content_tag(:span, shipment.tracking) end end |
#logo(image_path = ) ⇒ Object
60 61 62 |
# File 'app/helpers/spree/base_helper.rb', line 60 def logo(image_path = Spree::Config[:logo]) link_to image_tag(image_path), spree.root_path end |
#meta_data ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/helpers/spree/base_helper.rb', line 27 def object = instance_variable_get('@' + controller_name.singularize) = {} if object.is_a? ActiveRecord::Base [:keywords] = object. if object[:meta_keywords].present? [:description] = object. if object[:meta_description].present? end if [:description].blank? && object.is_a?(Spree::Product) [:description] = truncate((object.description), length: 160, separator: ' ') end if [:keywords].blank? || [:description].blank? .reverse_merge!({ keywords: current_store., description: current_store. }) end end |
#meta_data_tags ⇒ Object
49 50 51 52 53 |
# File 'app/helpers/spree/base_helper.rb', line 49 def .map do |name, content| tag('meta', name:, content:) end.join("\n") end |
#plural_resource_name(resource_class) ⇒ Object
150 151 152 |
# File 'app/helpers/spree/base_helper.rb', line 150 def plural_resource_name(resource_class) resource_class.model_name.human(count: Spree::I18N_GENERIC_PLURAL) end |
#pretty_time(time, format = :long) ⇒ Object
136 137 138 |
# File 'app/helpers/spree/base_helper.rb', line 136 def pretty_time(time, format = :long) I18n.l(time, format: :"solidus.#{format}") end |
#seo_url(taxon) ⇒ Object
128 129 130 |
# File 'app/helpers/spree/base_helper.rb', line 128 def seo_url(taxon) spree.nested_taxons_path(taxon.permalink) end |
#taxon_breadcrumbs(taxon, separator = ' » ', breadcrumb_class = 'inline') ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/helpers/spree/base_helper.rb', line 75 def (taxon, separator = ' » ', = 'inline') return '' if current_page?('/') || taxon.nil? crumbs = [[t('spree.home'), spree.root_path]] crumbs << [t('spree.products'), products_path] if taxon crumbs += taxon.ancestors.collect { |ancestor| [ancestor.name, spree.nested_taxons_path(ancestor.permalink)] } unless taxon.ancestors.empty? crumbs << [taxon.name, spree.nested_taxons_path(taxon.permalink)] end separator = raw(separator) items = crumbs.each_with_index.collect do |crumb, index| content_tag(:li, itemprop: 'itemListElement', itemscope: '', itemtype: 'https://schema.org/ListItem') do link_to(crumb.last, itemprop: 'item') do content_tag(:span, crumb.first, itemprop: 'name') + tag('meta', { itemprop: 'position', content: (index + 1).to_s }, false, false) end + (crumb == crumbs.last ? '' : separator) end end content_tag(:nav, content_tag(:ol, raw(items.map(&:mb_chars).join), class: , itemscope: '', itemtype: 'https://schema.org/BreadcrumbList'), id: 'breadcrumbs', class: 'sixteen columns') end |
#taxons_tree(root_taxon, current_taxon, max_level = 1) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/helpers/spree/base_helper.rb', line 99 def taxons_tree(root_taxon, current_taxon, max_level = 1) return '' if max_level < 1 || root_taxon.children.empty? content_tag :ul, class: 'taxons-list' do taxons = root_taxon.children.map do |taxon| css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'current' : nil content_tag :li, class: css_class do link_to(taxon.name, seo_url(taxon)) + taxons_tree(taxon, current_taxon, max_level - 1) end end safe_join(taxons, "\n") end end |
#variant_options(variant, _options = {}) ⇒ Object
human readable list of variant options
23 24 25 |
# File 'app/helpers/spree/base_helper.rb', line 23 def (variant, = {}) variant. end |