module Stealth::Controller::Helpers

Public Instance Methods

add_template_helper(mod) click to toggle source
# File lib/stealth/controller/helpers.rb, line 117
def add_template_helper(mod)
  _helpers.module_eval { include mod }
end
all_bot_helpers() click to toggle source

Extract helper names from files in “bot/helpers/*/_helper.rb”

# File lib/stealth/controller/helpers.rb, line 122
def all_bot_helpers
  all_helpers_from_path(helpers_path)
end
all_helpers_from_path(path) click to toggle source

Returns a list of helper names in a given path.

Stealth::Controller.all_helpers_from_path 'bot/helpers'
# => ["bot", "estimates", "tickets"]
# File lib/stealth/controller/helpers.rb, line 106
def all_helpers_from_path(path)
  helpers = Array(path).flat_map do |_path|
    extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_helper.rb$/
    names = Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1'.freeze) }
    names.sort!
  end
  helpers.uniq!
  helpers
end
default_helper_module!() click to toggle source
# File lib/stealth/controller/helpers.rb, line 92
def default_helper_module!
  module_name = name.sub(/Controller$/, "".freeze)
  module_path = module_name.underscore
  helper module_path
rescue LoadError => e
  raise e unless e.is_missing? "helpers/#{module_path}_helper"
rescue NameError => e
  raise e unless e.missing_name? "#{module_name}Helper"
end
helper(*args, &block) click to toggle source
# File lib/stealth/controller/helpers.rb, line 84
def helper(*args, &block)
  modules_for_helpers(args).each do |mod|
    add_template_helper(mod)
  end

  _helpers.module_eval(&block) if block_given?
end
inherited(subclass) click to toggle source

When a class is inherited, wrap its helper module in a new module. This ensures that the parent class's module can be changed independently of the child class's.

Calls superclass method
# File lib/stealth/controller/helpers.rb, line 38
def inherited(subclass)
  helpers = _helpers
  subclass._helpers = Module.new { include helpers }

  if subclass.superclass == Stealth::Controller && Stealth::Controller.include_all_helpers
    subclass.helper :all
  else
    subclass.class_eval { default_helper_module! } unless subclass.anonymous?
  end

  include subclass._helpers

  super
end
modules_for_helpers(args) click to toggle source
# File lib/stealth/controller/helpers.rb, line 53
def modules_for_helpers(args)
  # Allow all helpers to be included
  args += all_bot_helpers if args.delete(:all)

  # Add each helper_path to the LOAD_PATH
  Array(helpers_path).each {|path| $:.unshift(path) }

  args.flatten.map! do |arg|
    case arg
    when String, Symbol
      file_name = "#{arg.to_s.underscore}_helper"
      begin
        require_dependency(file_name)
      rescue LoadError => e
        raise Stealth::Controller::Helpers::MissingHelperError.new(e, file_name)
      end

      mod_name = file_name.camelize
      begin
        mod_name.constantize
      rescue LoadError
        raise NameError, "Couldn't find #{mod_name}, expected it to be defined in helpers/#{file_name}.rb"
      end
    when Module
      arg
    else
      raise ArgumentError, "helper must be a String, Symbol, or Module"
    end
  end
end