Class: RealTimeRails::RtrHelper

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::JavaScriptHelper, ActionView::Helpers::PrototypeHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/real_time_rails/rt_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RtrHelper

Returns a new instance of RtrHelper.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/real_time_rails/rt_helper.rb', line 24

def initialize(options = {})
  @options = options
  set_options
  register_partial
  html = load_javascript
  #html += manual_buttons #TODO remove test helper for ajax update calls. 
  html += wrap_render do
    yield
  end
  @html = html
end

Instance Attribute Details

#htmlObject

Returns the value of attribute html.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def html
  @html
end

#idObject

Returns the value of attribute id.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def id
  @id
end

#javascript_optionsObject

Returns the value of attribute javascript_options.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def javascript_options
  @javascript_options
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def options
  @options
end

#remote_f_optionsObject

Returns the value of attribute remote_f_options.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def remote_f_options
  @remote_f_options
end

#render_optionsObject

Returns the value of attribute render_options.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def render_options
  @render_options
end

#websocket_optionsObject

Returns the value of attribute websocket_options.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def websocket_options
  @websocket_options
end

#wrap_optionsObject

Returns the value of attribute wrap_options.



14
15
16
# File 'lib/real_time_rails/rt_helper.rb', line 14

def wrap_options
  @wrap_options
end

Instance Method Details

#js_after_updateObject



100
101
102
103
104
105
106
107
# File 'lib/real_time_rails/rt_helper.rb', line 100

def js_after_update
  if @options[:after_update]
    @remote_f_options[:complete] = "after_real_time_update_#{@id}();"
    return "function after_real_time_update_#{@id}(){#{@options[:after_update]}}"
  else
    return ""
  end
end

#js_remote_functionObject

Creates the js method wrapper for ajax calls.



110
111
112
113
114
115
116
117
# File 'lib/real_time_rails/rt_helper.rb', line 110

def js_remote_function
  "function real_time_update_#{@id}(){
    #{remote_function(remote_f_options)}
  }
  function real_time_delete_#{@id}(){
    $('##{@id}').remove;
  }"
end

#js_start_websocketObject

Adds the wrapper for creating the connection to the websocket server as well as registering for the correct channel on the server.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/real_time_rails/rt_helper.rb', line 85

def js_start_websocket
  "
  var Socket = \"MozWebSocket\" in window ? MozWebSocket : WebSocket;
  ws_#{@id} = new Socket('ws://#{RealTimeRails.config["websocket_host"]}:#{RealTimeRails.config["websocket_port"]}');
  ws_#{@id}.onmessage = function(evt) { 
    if(evt.data=='update'){real_time_update_#{@id}()}; 
    if(evt.data=='delete'){real_time_delete_#{@id}()};  
  };
  ws_#{@id}.onclose = function() {  };
  ws_#{@id}.onopen = function() {
    ws_#{@id}.send('#{@websocket_options.to_json}');
  };
  "
end

#load_javascriptObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/real_time_rails/rt_helper.rb', line 70

def load_javascript
  @remote_f_options = {
    url: "/render_real_time/id/#{@id}"
  }

  html = "<script>\n"
  html += js_after_update
  html += js_remote_function
  html += js_start_websocket
  html += "</script>\n"
  return html
end

#manual_buttonsObject

TODO remove test helper method for ajax update calls.



66
67
68
# File 'lib/real_time_rails/rt_helper.rb', line 66

def manual_buttons
  "<a href='#' onclick='real_time_update_#{@id}();'>Manual Update</a>\n"
end

#protect_against_forgery?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/real_time_rails/rt_helper.rb', line 10

def protect_against_forgery?
  false
end

#register_partialObject

Writes data to cache for later use in the render_real_time controller.



129
130
131
132
# File 'lib/real_time_rails/rt_helper.rb', line 129

def register_partial
  Rails.cache.write("real_time_#{@id}", @websocket_options.to_yaml)
  Rails.cache.write("real_time_#{@id}_options", @options.to_yaml)
end

#set_optionsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/real_time_rails/rt_helper.rb', line 36

def set_options
  model_list = []
  @options[:locals].each do |key,value|
    if value.is_a?(ActiveRecord::Base)
      model_list << {type: :single, key: key, name: value.class.name, id: value.id}
    end
    if value.is_a?(Array)
      if (class_name = value.map{|v| v.class.name}.uniq).length==1
        model_list << {type: :array, key: key, name: class_name.first, ids: value.map(&:id)}
      else
        raise "Can not do real time updates on arrays containing different models.\n#{value.map{|v| v.class.name}.uniq.to_yaml}"
      end
    end
    if value.is_a?(ActiveRecord::Relation)
      model_list << {type: :relation, key: key, name: value.ancestors.first.name, sql: value.to_sql.gsub('"','\"')}
    end
  end
  @websocket_options = {
    models: model_list,
    command: 'listen'
  }
  @id = Digest::MD5.hexdigest(@websocket_options.to_yaml)
  @websocket_options = {
    models: model_list,
    command: 'listen',
    id: @id
  }
end

#wrap_renderObject



119
120
121
122
123
124
125
# File 'lib/real_time_rails/rt_helper.rb', line 119

def wrap_render
  html = "<div id='#{@id}' class='real_time_wrapper'>\n"
  html += yield
  #html += @websocket_options.to_yaml # TODO remove debugging data.
  html += "</div>\n"
  return html
end