module Optipipe::TagHelper

Public Instance Methods

optipipe_javascript_include_tag(*args) click to toggle source

options

mapper_conditions: Limiting conditions when select mapper.
default:           A mapper used when there aren't any one meeting a condition. This value is controller_path.
# File lib/optipipe/helpers/tag_helper.rb, line 6
def optipipe_javascript_include_tag(*args)
  # set args
  options             = args[0] || {}
  include_tag_options = args[1] || {}
  mapper_conditions   = options.delete(:mapper_conditions) || {}

  mapper_conditions.except!('controller_path', 'load_files', 'after_onload') # Using several key names at mapper_conditions are not allowed.
  mapper = get_mapper(Optipipe::Mapper.get_javascript_mappers(controller.controller_path), mapper_conditions)

  # default_mapper
  mapper = (Optipipe::Mapper.get_javascript_mappers(options[:default]) || []).first if mapper.blank?

  return if mapper.blank?

  script = (mapper['load_files'] || []).map do |file_name|
    javascript_include_tag("optipipe/precompiles/#{file_name}", include_tag_options)
  end.join("\n").html_safe

  # after onload
  paths = (mapper['after_onload'] || []).map do |file_name|
    asset_path("optipipe/precompiles/#{file_name}")
  end
  script += render(partial: 'optipipe/after_onload', locals: {paths: paths}) if paths.present?

  script
end

Private Instance Methods

get_mapper(mappers, conditions = {}) click to toggle source
# File lib/optipipe/helpers/tag_helper.rb, line 57
def get_mapper(mappers, conditions = {})
  return if mappers.blank?

  conditions.each do |key, value|
    mappers = mappers.select do |mapper|
      mapper[key.to_s] == value
    end
  end

  mappers.first
end