Module: Sprangular::ApplicationHelper

Defined in:
app/helpers/sprangular/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#cached_templatesObject



82
83
84
85
86
# File 'app/helpers/sprangular/application_helper.rb', line 82

def cached_templates
  Sprangular::Engine.config.cached_paths.inject({}) do |files, dir|
    cached_templates_for_dir(files, dir)
  end
end

#cached_templates_for_dir(files, dir) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/sprangular/application_helper.rb', line 88

def cached_templates_for_dir(files, dir)
  root = Sprangular::Engine.root

  files = Dir[root + "app/assets/templates/#{dir}/**"].inject(files) do |hash, path|
    asset_path = asset_path path.gsub(root.to_s + "/app/assets/templates/", "")
    local_path = "app/assets/templates/" + asset_path

    hash[asset_path.gsub(/.slim$/, '')] = Tilt.new(path).render.html_safe if !File.exists?(local_path)

    hash
  end

  Dir["app/assets/templates/#{dir}/**"].inject(files) do |hash, path|
    sprockets_path = path.gsub("app/assets/templates/", "")

    asset_path = asset_path(sprockets_path).
      gsub(/^\/app\/assets\/templates/, '/assets').
      gsub(/.slim$/, '')

    hash[asset_path] = Rails.application.assets.find_asset(sprockets_path).body.html_safe
    hash
  end
end

#current_translationsObject

Get relevant translations for front end. For both a simple, and “Chainable” i18n Backend, which is used by spree i18n.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/sprangular/application_helper.rb', line 68

def current_translations
  Rails.cache.fetch("current_translations.#{I18n.locale}") do
    if I18n.backend.class == I18n::Backend::Simple
      I18n.backend.load_translations

      @translations ||= I18n.backend.send(:translations)
    else
      @translations ||= I18n.backend.backends.last.send(:translations)
    end
    # Return only sprangular keys for js environment
    @translations[I18n.locale][:sprangular]
  end
end

#js_configObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/sprangular/application_helper.rb', line 23

def js_config
  Rails.cache.fetch('js_config') do
    config = ::Spree::Config
    store = Spree::Store.current

    {
      site_name: store.seo_title || store.name,
      logo: asset_path(config.),
      supported_locales: supported_locales,
      default_country_id: config.default_country_id,
      payment_methods: payment_methods,
      image_sizes:
        Spree::Image.attachment_definitions[:attachment][:styles].keys,
      product_page_size: Spree::Config.products_per_page
    }
  end
end

#js_environmentObject



12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/sprangular/application_helper.rb', line 12

def js_environment
  {
    env: Rails.env,
    config: js_config,
    locale: I18n.locale,
    currency: Money::Currency.table[current_currency.downcase.to_sym],
    translations: current_translations,
    templates: template_paths
  }
end

#payment_methodsObject



3
4
5
6
7
8
9
10
# File 'app/helpers/sprangular/application_helper.rb', line 3

def payment_methods
  Spree::PaymentMethod.available(:front_end).map do |method|
    {
      id: method.id,
      name: method.name
    }
  end
end

#supported_localesObject



57
58
59
60
61
62
63
# File 'app/helpers/sprangular/application_helper.rb', line 57

def supported_locales
  if defined? SpreeI18n
    SpreeI18n::Config.supported_locales
  else
    Rails.application.config.i18n.available_locales
  end
end

#template_pathsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/sprangular/application_helper.rb', line 41

def template_paths
  Rails.cache.fetch('template_paths') do
    Hash[
      Rails
        .application
        .assets.each_logical_path
        .select { |file| file.end_with?('html') }
        .map do |file|
          path = digest_assets? ? File.join('/assets', Rails.application.assets[file].digest_path) : asset_path(file)

          [file, path]
        end
    ]
  end
end