File: watch.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 (58 lines) | stat: -rw-r--r-- 1,503 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
47
48
49
50
51
52
53
54
55
56
57
58
class Gon
  class Watch < Gon
    class << self

      JS_FUNCTION = File.read(File.expand_path('../../../js/watch.js', __FILE__))

      def render
        JS_FUNCTION + "window.gon.watchedVariables=#{Gon::JsonDumper.dump all_variables};"
      end

      def render_amd
        JS_FUNCTION + "gon.watchedVariables=#{Gon::JsonDumper.dump all_variables};"
      end

      def all_variables
        @watch_variables || {}
      end

      def clear
        @watch_variables = {}
      end

      private

      def set_variable(name, value)
        if return_variable?(name)
          return_variable value
        elsif Gon.send(:current_gon)
          variable = {}
          @watch_variables ||= {}
          env = Gon.send(:current_gon).env
          variable['url'] = env['ORIGINAL_FULLPATH'] || env['REQUEST_URI']
          variable['method'] = env['REQUEST_METHOD']
          variable['name'] = name

          @watch_variables[name] = variable
          super
        end
      end

      def return_variable?(variable)
        controller = Gon::EnvFinder.controller_env
        params = controller.params
        variable = variable.to_s.gsub('=', '')

        controller.request.xhr? &&
          params[:gon_return_variable] &&
          params[:gon_watched_variable] == variable
      end

      def return_variable(value)
        controller = Gon::EnvFinder.controller_env
        controller.render json: Gon::Escaper.escape_unicode(Gon::JsonDumper.dump value)
      end

    end
  end
end