Class: Rho::RhoController

Inherits:
Object show all
Defined in:
lib/framework/rho/render.rb,
lib/framework/autocomplete/Rho.rb,
lib/framework/rho/rhocontroller.rb,
lib/framework/rho/rhoviewhelpers.rb

Constant Summary collapse

@@cached_metadata =
{}
@@m_geoview_callback =
nil
@@m_geoview_callback_data =
nil
@@m_geoview_timeout_sec =
0
@@m_arObjectNotify =
[]
@@m_arSrcIDNotify =
[]
@@before =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute menu.



402
403
# File 'lib/framework/autocomplete/Rho.rb', line 402

def menu
end

Class Method Details

.before(&block) ⇒ Object



370
371
# File 'lib/framework/autocomplete/Rho.rb', line 370

def self.before(block)
end

.cached_metadataObject



110
111
112
# File 'lib/framework/rho/render.rb', line 110

def self.
    @@cached_metadata
end

.clean_cached_metadataObject



114
115
116
117
# File 'lib/framework/rho/render.rb', line 114

def self.
    @@cached_metadata.clear()
    #puts "meta deleted"
end

.get_layout_nameObject



50
51
52
# File 'lib/framework/rho/render.rb', line 50

def self.get_layout_name
  @layout.nil? ? 'layout' : @layout
end

.layout(name) ⇒ Object



46
47
48
# File 'lib/framework/rho/render.rb', line 46

def self.layout(name)
  @layout = name
end

.process_rho_object(params) ⇒ Object



368
369
# File 'lib/framework/autocomplete/Rho.rb', line 368

def self.process_rho_object(params)
end

.renderfile(filename, req, res) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/framework/rho/render.rb', line 54

def self.renderfile(filename, req = {}, res = {})
  begin
      res = ""

      if filename.end_with?(RHO_ERB_EXT)
        if RhoApplication::current_controller()
            puts "reuse current action controller."
            res = RhoApplication::current_controller().inst_render_index(filename, req, res)            
        else
            res = (RhoController.new).inst_render_index(filename, req, res)
        end    
      else
        res = IO.read(filename)
      end

      if System.get_property('platform') != 'WP8'
        RhoController.start_objectnotify()
        RhoController.start_geoview_notification()
      end

      res
  rescue Exception => exception
    
    raise
  end    
end

.start_geoview_notificationObject



323
324
325
326
327
328
# File 'lib/framework/rho/render.rb', line 323

def self.start_geoview_notification()
    GeoLocation.set_view_notification(@@m_geoview_callback, @@m_geoview_callback_data, @@m_geoview_timeout_sec)
    @@m_geoview_callback = nil
    @@m_geoview_callback_data = nil
    @@m_geoview_timeout_sec = 0
end

.start_objectnotifyObject



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/framework/rho/render.rb', line 347

def self.start_objectnotify()
  if defined?(RHOCONNECT_CLIENT_PRESENT)
      Rho::RhoConnectClient.clean_objectnotify()
  end

  return unless @@m_arObjectNotify && @@m_arObjectNotify.length > 0 

  if defined?(RHOCONNECT_CLIENT_PRESENT)
      0.upto(@@m_arObjectNotify.length()-1) do |i|
        Rho::RhoConnectClient::add_objectnotify(@@m_arSrcIDNotify[i], @@m_arObjectNotify[i])
      end
  end
  
  @@m_arObjectNotify = []
  @@m_arSrcIDNotify = []
  
end

Instance Method Details

#__get_modelObject



119
120
121
122
123
124
125
# File 'lib/framework/rho/render.rb', line 119

def __get_model
    model = nil
    begin
        model = Object.const_get(@request['model'].to_sym()) if Object.const_defined?(@request['model'].to_sym() )
    rescue Exception => exc
    end
end

#add_objectnotify(arg) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/framework/rho/render.rb', line 332

def add_objectnotify(arg)
  return unless arg

  if arg.is_a?(Array)
    arg.each do |item|
      @@m_arObjectNotify.push(strip_braces(item.object))
      @@m_arSrcIDNotify.push(item.source_id)
    end
  else
    @@m_arObjectNotify.push(strip_braces(arg.object))
    @@m_arSrcIDNotify.push(arg.source_id)
  end

end

#app_error(str) ⇒ Object



412
413
# File 'lib/framework/autocomplete/Rho.rb', line 412

def app_error(str)
end

#app_info(str) ⇒ Object



410
411
# File 'lib/framework/autocomplete/Rho.rb', line 410

def app_info(str)
end

#click_to_call(phone, description = nil) ⇒ Object



392
393
# File 'lib/framework/autocomplete/Rho.rb', line 392

def click_to_call(phone,description)
end

#default_actionObject



414
415
# File 'lib/framework/autocomplete/Rho.rb', line 414

def default_action
end

#getBindingObject



105
106
107
# File 'lib/framework/rho/render.rb', line 105

def getBinding
    binding
end

#inst_render_index(filename, req, res) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/framework/rho/render.rb', line 81

def inst_render_index(filename, req, res)
  rho_info 'inst_render_index'
  @request, @response = req, res
  @params = RhoSupport::query_params req
  
  #@request, @response = {}
  #@params = {}
  require 'rho/rhoviewhelpers'
  
  @content = eval_compiled_file(filename, getBinding() )
  if !xhr?
      rho_info 'index layout' 
      layout = File.dirname(filename) + "/layout" + RHO_ERB_EXT
      @content = eval_compiled_file(layout, getBinding() ) if Rho::file_exist?(layout)
  else
      if @request["headers"]["Transition-Enabled"] == "true"
        #puts "add 'div' in inst_render_index"
        @content = "<div>#{@content}</div>"
      end
  end
      
  @content
end

Examples of how to use link_to method:

link_to “Visit Other Site”, “www.rhomobile.com/”

> <a href="www.rhomobile.com/" >Visit Other Site</a>

link_to “Help”, { :action => “help” }

> <a href="/application/model/help" >Help</a>

link_to “Delete”, { :action => “delete”, :id => ‘12’ }

> <a href=“/application/model/12/delete” onclick=“var f = document.createElement(‘form’);

f.style.display = ‘none’;this.parentNode.appendChild(f); f.method = ‘POST’; f.action = this.href;f.submit();return false;“>Delete</a>

link_to “Show”, { :action => “show”, :id => ‘12’},“style="height:4px;width:7px;border-width:0px;"”

> <a href=“/application/model/12/show” style=“height:4px;width:7px;border-width:0px;”>Show</a>

link_to “Delete”, { :action => “delete”, :id => ‘12’ }, “class="delete_link"”

f.style.display = ‘none’;this.parentNode.appendChild(f); f.method = ‘POST’; f.action = this.href;f.submit();return false;">Delete</a>“

link_to “Invate”,:action => :invite, :query => => ‘John Smith’, ‘address’ => “john.smith.com

> <a href=“/application/model/invite?name=John%20Smith&address=http%3A%2F%2Fjohn.smith.com” >Invate</a>



74
75
# File 'lib/framework/rho/rhoviewhelpers.rb', line 74

def link_to(name,url_params,html_options,confirm)
end

#mailto(address, description = nil) ⇒ Object



394
395
# File 'lib/framework/autocomplete/Rho.rb', line 394

def mailto(address,description)
end

#redirect(url_params = {}, options = {}) ⇒ Object



422
423
# File 'lib/framework/autocomplete/Rho.rb', line 422

def redirect(url_params,options)
end

#render(options) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/framework/rho/render.rb', line 127

def render(options = nil)
  if @params['rho_callback']
    rho_error( "render call in callback. Call WebView.navigate instead" ) 
    return ""
  end  

  RhoProfiler.start_counter('ERB_RENDER')      

  options = {} if options.nil? or !options.is_a?(Hash)
  options = options.symbolize_keys

  metaenabled = false
  
  action = nil
  action = options[:action] if options[:action]
  action = @request['action'].nil? ? default_action : @request['action'] unless action

  if @request['model'] != nil
    
    model = __get_model()
        
    if model && model.respond_to?( :metadata ) and model. != nil
      if $".include?( "rhodes_translator")
        metaenabled = model.[action.to_s] != nil
      else
        rho_error( "unable to load rhodes_translator gem." )
      end
    end
  end

  if not options[:string].nil?
    @content = options[:string]
    @back_action = options[:back] if options[:back]
    options[:layout] = false if options[:layout].nil?

  elsif metaenabled
    @content = (action,model.)

  elsif not options[:partial].nil?  # render the file and return, don't set rendered true for a partial.
    @content = render_partial(options)
    options[:layout] = false
  else
    if options[:file].nil? or !options[:file].is_a?(String)
      fname = @request[:modelpath]+action.to_s+RHO_ERB_EXT
      if Rho::file_exist?(fname)
        @content = eval_compiled_file(fname, getBinding() )
      else
        @content = ""
      end
    else
      options[:file] = options[:file].gsub(/\.erb$/,"").gsub(/^\/app/,"")
      @content = eval_compiled_file(RhoApplication::get_app_path(@request['application'])+options[:file]+RHO_ERB_EXT, getBinding() )
      options[:layout] = false if options[:layout].nil?
    end
  end
  #rho_info 'render content: ' + @content.length.to_s
  if xhr? and options[:use_layout_on_ajax] != true
    options[:layout] = false
    if options[:partial].nil? && @request["headers"]["Transition-Enabled"] == "true"
      #puts "add 'div' in render"
      if !(@request["headers"]["Accept"] =~ /^\*\/\*/ || @request["headers"]["Accept"] =~ /^text\/html/)
        @response["headers"]["Content-Type"] = @request["headers"]["Accept"].gsub(/\,.*/, '')
      else
        @content = "<div>#{@content}</div>"
      end
    end
  elsif options[:layout].nil? or options[:layout] == true
    options[:layout] = self.class.get_layout_name
  end

  if options[:layout] != false
    layoutfile = RhoApplication::get_app_path(@request['application']) + options[:layout].to_s + RHO_ERB_EXT
    @content = eval_compiled_file(layoutfile, binding ) if Rho::file_exist?(layoutfile)
    rho_info 'Layout file: ' + layoutfile + '. Content size: ' + @content.length.to_s
  end

  RhoController.start_objectnotify()
  RhoController.start_geoview_notification()
  @back_action = options[:back] if options[:back]
  @rendered = true
  
  RhoProfiler.stop_counter('ERB_RENDER')      
  
  @content
end

#render_metadata(action, metadata) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/framework/rho/render.rb', line 213

def (action = nil, = nil)
  if .nil?
    model = nil
    model = Object.const_get(@request['model'].to_sym) if Object.const_defined?(@request['model'].to_sym)
    
    if model && model.respond_to?( :metadata ) and model. != nil
       = model.
    end
  end

  if action.nil?
      action = @request['action'].nil? ? default_action : @request['action']
  end

  return "" if .nil?
  
  action = action.to_s
  data = {}
  self.instance_variables.each do |sym|
    data[sym.to_s] = self.instance_variable_get sym
  end

  data["self"] = self
  
  prepared = bind(data,[action])
  
  translate(prepared,action)

end

#render_partial(options) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/framework/rho/render.rb', line 243

def render_partial(options = nil)
  options = {} if options.nil? or !options.is_a?(Hash)
  options = options.symbolize_keys

  splitpartial = options[:partial].split('/')
  partial_name = splitpartial[-1]
  model = nil
  if splitpartial.length > 1
    model = splitpartial[-2]
  end

  options[:locals] = {} if options[:locals].nil? or !options[:locals].is_a?(Hash)
   
  content = ""
  if options[:collection].nil?
    locals = self.clone

    class << locals

      def set_vars(obj = nil)
        @vars = {}
        if obj
          obj.each do |key,value|
            @vars[key.to_sym()] = value if key && key.length > 0
          end
        end
      end

      def method_missing(name, *args)
        unless name == Fixnum
          if name[name.length()-1] == '='
            @vars[name.to_s.chop.to_sym()] = args[0]
          else
            @vars[name]
          end
        end
      end

      def get_binding
        binding
      end

    end
    
    locals.set_vars(options[:locals])
    
    modelpath = @request[:modelpath]
    modelpath = Rho::RhoFSConnector.get_model_path("app",model) if model
    content = eval_compiled_file(modelpath+'_' + partial_name.to_s+RHO_ERB_EXT, locals.get_binding )
  else
    #xruby issue - https://www.pivotaltracker.com/story/show/3454121
    content = render_partial_collection(options,partial_name)
  end
  content
end

#render_partial_collection(options, partial_name) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/framework/rho/render.rb', line 299

def render_partial_collection(options, partial_name)
    i = 0
    content = ""
    options[:collection].each do |x|
      options[:locals][partial_name] = x

      options[:locals][partial_name + '_counter'] = i
      i = i + 1
      #puts "render partial: #{x}"
      content += render_partial :partial => options[:partial], :locals => options[:locals]
    end
    
    content
end

#rho_error(str) ⇒ Object



408
409
# File 'lib/framework/autocomplete/Rho.rb', line 408

def rho_error(str)
end

#rho_info(str) ⇒ Object



406
407
# File 'lib/framework/autocomplete/Rho.rb', line 406

def rho_info(str)
end

#serve(application, object_mapping, req, res) ⇒ Object



416
417
# File 'lib/framework/autocomplete/Rho.rb', line 416

def serve(application,object_mapping,req,res)
end

#set_geoview_notification(callback, callback_data, timeout_sec) ⇒ Object



317
318
319
320
321
# File 'lib/framework/rho/render.rb', line 317

def set_geoview_notification(callback, callback_data="", timeout_sec=0)
    @@m_geoview_callback = callback
    @@m_geoview_callback_data = callback_data
    @@m_geoview_timeout_sec = timeout_sec
end

#strip_braces(str = nil) ⇒ Object



424
425
# File 'lib/framework/autocomplete/Rho.rb', line 424

def strip_braces(str)
end

#truncate(text, length) ⇒ Object



390
391
# File 'lib/framework/autocomplete/Rho.rb', line 390

def truncate(text,length)
end

#url_for(params = {}) ⇒ Object

Examples of how to use url_for method:

url_for ‘/some_url’

> /some_url

When generating a new URL, missing values may be filled in from the current request’s parameters. For example, if application name or model are not specifyed in the call parameters, they would be filled from the request.

url_for :action => :index

> /application/model

url_for :action => :create

> /application/model

url_for :action => :new

> /application/model/new

url_for :action => :show, :id => ‘12’

> /application/model/12/show

url_for :model => :another_model, :action => :show, :id => ‘12’

> /application/another_model/12/show

url_for :controller => :another_controller, :action => :show, :id => ‘12’

> /application/another_controller/12/show

url_for :application => :another_app, :model => :another_model, :action => :show, :id => ‘12’

> /another_app/another_model/12/show

url_for :action => :create, :query => => ‘John Smith’, ‘address’ => “john.smith.com

> /application/model?name=John%20Smith&address=http%3A%2F%2Fjohn.smith.com

url_for :action => :show, :id => ‘12’, :fragment => “an-anchor”

> /application/model/12/show#an-anchor



121
122
# File 'lib/framework/rho/rhoviewhelpers.rb', line 121

def url_for(params)
end

#url_for_nativeview(params = {}) ⇒ Object



400
401
# File 'lib/framework/autocomplete/Rho.rb', line 400

def url_for_nativeview(params)
end

#xml_http_request?Boolean Also known as: xhr?

Returns true if the request’s header contains “XMLHttpRequest”.

Returns:

  • (Boolean)


124
125
# File 'lib/framework/rho/rhocontroller.rb', line 124

def xml_http_request?
end