Module: JsApplicationReloader

Defined in:
lib/js_application_reloader.rb,
lib/js_application_reloader/version.rb,
lib/js_application_reloader/controller_extensions.rb,
lib/generators/js_application_reloader/install_generator.rb

Defined Under Namespace

Modules: ControllerExtensions Classes: InstallGenerator

Constant Summary collapse

VERSION =
"0.0.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.application_nameObject

Returns the value of attribute application_name.



21
22
23
# File 'lib/js_application_reloader.rb', line 21

def application_name
  @application_name
end

.async_js_projectObject

Returns the value of attribute async_js_project.



16
17
18
# File 'lib/js_application_reloader.rb', line 16

def async_js_project
  @async_js_project
end

.redirect_urlObject

Returns the value of attribute redirect_url.



22
23
24
# File 'lib/js_application_reloader.rb', line 22

def redirect_url
  @redirect_url
end

.reload_required_http_statusObject

Returns the value of attribute reload_required_http_status.



20
21
22
# File 'lib/js_application_reloader.rb', line 20

def reload_required_http_status
  @reload_required_http_status
end

.status_header_nameObject

Returns the value of attribute status_header_name.



19
20
21
# File 'lib/js_application_reloader.rb', line 19

def status_header_name
  @status_header_name
end

.tokenObject

Returns the value of attribute token.



17
18
19
# File 'lib/js_application_reloader.rb', line 17

def token
  @token
end

.token_header_nameObject

Returns the value of attribute token_header_name.



18
19
20
# File 'lib/js_application_reloader.rb', line 18

def token_header_name
  @token_header_name
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Nice configuration is a small twist on robots.thoughtbot.com/mygem-configure-block. Thanks!

Yields:

  • (_self)

Yield Parameters:



27
28
29
# File 'lib/js_application_reloader.rb', line 27

def self.configure
  yield(self)
end

.handle_reloader_token_expiration_on_clientObject

Expiration handler (you can customize this)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/js_application_reloader.rb', line 58

def self.handle_reloader_token_expiration_on_client
  <<-EOF
      JsApplicationReloader.handleTokenExpiration = function(xhr) {
        var contentType = xhr.getResponseHeader("content-type") || "";
        if (contentType.indexOf('html') > -1) {
          $('body').html(xhr.responseText); // Display the HTML returned by xhr.responseText as you see fit
        }
        if (contentType.indexOf('json') > -1) {
          $('body').html(xhr.responseJSON.message); // Display the HTML returned by xhr.responseJSON.message as you see fit
        }
        return false;
      };
  EOF
end

.inject_scriptObject

Must be injected into ERB template using raw TODO: Option to limit fields sent to the client for lightness



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/js_application_reloader.rb', line 33

def self.inject_script
  <<-EOF
    <script type="text/javascript">
      JsApplicationReloader = {
        token: '#{JsApplicationReloader.token}',
        tokenHeaderName: '#{JsApplicationReloader.token_header_name}',
        statusHeaderName: '#{JsApplicationReloader.status_header_name}',
        reloadRequiredHttpStatus: '#{JsApplicationReloader.reload_required_http_status}',
        applicationName: '#{JsApplicationReloader.application_name}',
        redirectUrl: '#{JsApplicationReloader.redirect_url}'
      };

      // -- Define Expiration check
      JsApplicationReloader.isTokenExpired = function(xhr) {
        return (xhr.status == JsApplicationReloader.reloadRequiredHttpStatus && (xhr.getResponseHeader(JsApplicationReloader.statusHeaderName) === 'token_expired'));
      };

      #{handle_reloader_token_expiration_on_client}

      #{JsApplicationReloader.non_async_js_request_script unless JsApplicationReloader.async_js_project}
    </script>
  EOF
end

.non_async_js_request_scriptObject

For non-async Javascript projects



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/js_application_reloader.rb', line 74

def self.non_async_js_request_script
  <<-EOF
    $(document).ready(function() {
        $(document).ajaxSend(function(event, request) {
            if (JsApplicationReloader && JsApplicationReloader.token) {
                request.setRequestHeader('#{JsApplicationReloader.token_header_name}', JsApplicationReloader.token);
           }
        });
    });
  EOF
end