Class: RealTimeRails::RtrHelper
- Inherits:
-
Object
- Object
- RealTimeRails::RtrHelper
- Includes:
- ActionView::Helpers::JavaScriptHelper, ActionView::Helpers::PrototypeHelper, ActionView::Helpers::UrlHelper
- Defined in:
- lib/real_time_rails/rt_helper.rb
Instance Attribute Summary collapse
-
#html ⇒ Object
Returns the value of attribute html.
-
#id ⇒ Object
Returns the value of attribute id.
-
#javascript_options ⇒ Object
Returns the value of attribute javascript_options.
-
#options ⇒ Object
Returns the value of attribute options.
-
#remote_f_options ⇒ Object
Returns the value of attribute remote_f_options.
-
#render_options ⇒ Object
Returns the value of attribute render_options.
-
#websocket_options ⇒ Object
Returns the value of attribute websocket_options.
-
#wrap_options ⇒ Object
Returns the value of attribute wrap_options.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RtrHelper
constructor
A new instance of RtrHelper.
- #js_after_update ⇒ Object
-
#js_remote_function ⇒ Object
Creates the js method wrapper for ajax calls.
-
#js_start_websocket ⇒ Object
Adds the wrapper for creating the connection to the websocket server as well as registering for the correct channel on the server.
- #load_javascript ⇒ Object
-
#manual_buttons ⇒ Object
TODO remove test helper method for ajax update calls.
- #protect_against_forgery? ⇒ Boolean
-
#register_partial ⇒ Object
Writes data to cache for later use in the render_real_time controller.
- #set_options ⇒ Object
- #wrap_render ⇒ Object
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 = 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
#html ⇒ Object
Returns the value of attribute html.
14 15 16 |
# File 'lib/real_time_rails/rt_helper.rb', line 14 def html @html end |
#id ⇒ Object
Returns the value of attribute id.
14 15 16 |
# File 'lib/real_time_rails/rt_helper.rb', line 14 def id @id end |
#javascript_options ⇒ Object
Returns the value of attribute javascript_options.
14 15 16 |
# File 'lib/real_time_rails/rt_helper.rb', line 14 def @javascript_options end |
#options ⇒ Object
Returns the value of attribute options.
14 15 16 |
# File 'lib/real_time_rails/rt_helper.rb', line 14 def @options end |
#remote_f_options ⇒ Object
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 end |
#render_options ⇒ Object
Returns the value of attribute render_options.
14 15 16 |
# File 'lib/real_time_rails/rt_helper.rb', line 14 def @render_options end |
#websocket_options ⇒ Object
Returns the value of attribute websocket_options.
14 15 16 |
# File 'lib/real_time_rails/rt_helper.rb', line 14 def @websocket_options end |
#wrap_options ⇒ Object
Returns the value of attribute wrap_options.
14 15 16 |
# File 'lib/real_time_rails/rt_helper.rb', line 14 def @wrap_options end |
Instance Method Details
#js_after_update ⇒ Object
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_function ⇒ Object
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()} } function real_time_delete_#{@id}(){ $('##{@id}').remove; }" end |
#js_start_websocket ⇒ Object
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_javascript ⇒ Object
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_buttons ⇒ Object
TODO remove test helper method for ajax update calls.
66 67 68 |
# File 'lib/real_time_rails/rt_helper.rb', line 66 def "<a href='#' onclick='real_time_update_#{@id}();'>Manual Update</a>\n" end |
#protect_against_forgery? ⇒ Boolean
10 11 12 |
# File 'lib/real_time_rails/rt_helper.rb', line 10 def protect_against_forgery? false end |
#register_partial ⇒ Object
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_options ⇒ Object
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 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_render ⇒ Object
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 |