File: env_finder.rb

package info (click to toggle)
ruby-gon 6.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 504 kB
  • sloc: ruby: 1,383; makefile: 9
file content (46 lines) | stat: -rw-r--r-- 1,180 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Gon
  module EnvFinder
    ENV_CONTROLLER_KEY = 'action_controller.instance'
    ENV_RESPONSE_KEY = 'action_controller.rescue.response'

    class << self

      def controller_env(options = {})
        options[:controller] ||
          (
            current_gon &&
            current_gon.env[ENV_CONTROLLER_KEY] ||
            current_gon.env[ENV_RESPONSE_KEY].
              instance_variable_get('@template').
              instance_variable_get('@controller')
          )
      end

      def template_path(options, extension)
        if options[:template]
          if right_extension?(extension, options[:template])
            options[:template]
          else
            [options[:template], extension].join('.')
          end
        else
          controller = controller_env(options).controller_path
          action = controller_env(options).action_name
          "app/views/#{controller}/#{action}.json.#{extension}"
        end
      end

      private

      def right_extension?(extension, template_path)
        File.extname(template_path) == ".#{extension}"
      end

      def current_gon
        RequestStore.store[:gon]
      end

    end

  end
end