Module: RouteTranslator::Translator::RouteHelpers

Defined in:
lib/route_translator/translator/route_helpers.rb

Constant Summary collapse

TEST_CASE_HOOKS =
%i[
  action_controller_test_case action_mailer_test_case action_view_test_case
].freeze

Class Method Summary collapse

Class Method Details

.add(old_name, named_route_collection) ⇒ Object

Add standard route helpers for default locale e.g.

I18n.locale = :de
people_path -> people_de_path
I18n.locale = :fr
people_path -> people_fr_path


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/route_translator/translator/route_helpers.rb', line 17

def add(old_name, named_route_collection)
  helper_list = named_route_collection.helper_names

  %w[path url].each do |suffix|
    helper_container = named_route_collection.send(:"#{suffix}_helpers_module")
    new_helper_name = :"#{old_name}_#{suffix}"

    helper_list.push(new_helper_name) unless helper_list.include?(new_helper_name)

    helper_container.__send__(:define_method, new_helper_name) do |*args|
      __send__(Translator.route_name_for(args, old_name, suffix, self), *args)
    end

    next unless ENV.fetch('RAILS_ENV', nil) == 'test'

    TEST_CASE_HOOKS.each do |test_case_hook|
      ActiveSupport.on_load(test_case_hook) do
        include helper_container
      end
    end
  end
end