Class: Rho::RhoController

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_layout_nameObject



10
11
12
# File 'lib/rho/render.rb', line 10

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

.layout(name) ⇒ Object



6
7
8
# File 'lib/rho/render.rb', line 6

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

.render_index(filename) ⇒ Object



22
23
24
25
26
27
# File 'lib/rho/render.rb', line 22

def self.render_index(filename)
	layout = File.dirname(filename) + "/layout_erb.iseq"
	@content = eval_compiled_file(filename, binding)
	@content = eval_compiled_file(layout, binding) if File.exist?(layout)
    @content
end

.renderfile(filename) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rho/render.rb', line 14

def self.renderfile(filename)
     	if File.extname(filename) == '.iseq'
    	render_index(filename)
  	else
       	IO.read(filename)
	end
end

Instance Method Details

#click_to_call(phone, description = nil) ⇒ Object



12
13
14
15
16
# File 'lib/rho/rhoviewhelpers.rb', line 12

def click_to_call(phone,description=nil)
  description = phone if description.nil?
  return "" if phone.nil? or phone.length == 0
  "<a href=\"tel:#{phone}\" target=\"_self\">#{description}</a>"
end

#default_actionObject



8
9
10
11
12
# File 'lib/rho/rhocontroller.rb', line 8

def default_action
  return Hash['GET','show','PUT','update','POST','update',
    'DELETE','delete'][@request['request-method']] unless @request['id'].nil?
  return Hash['GET','index','POST','create'][@request['request-method']]
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>



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rho/rhoviewhelpers.rb', line 48

def link_to(name,url_params = {},html_options = "",confirm = nil)
  url = url_for(url_params)
  if (url_params.is_a?(String) || url_params[:action].to_s != 'delete')
    "<a href=\"#{url}\" #{html_options}>#{name || url}</a>"
  else
    "<a href=\"#{url}\" #{html_options} onclick=\""+  #if (confirm('#{confirm}')) {
    "var f = document.createElement('form'); f.style.display = 'none';" +
    "this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();"+
    "return false;\">#{name || url}</a>"
  end
end

#mailto(address, description = nil) ⇒ Object



18
19
20
21
22
# File 'lib/rho/rhoviewhelpers.rb', line 18

def mailto(address,description=nil)
  description = address if description.nil?
  return "" if address.nil? or address.length == 0
  "<a href=\"mailto:#{address}\" target=\"_self\">#{description}</a>"
end

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



27
28
29
30
31
32
# File 'lib/rho/rhocontroller.rb', line 27

def redirect(url_params = {},options = {})
  @response['status'] = options['status'] || 302 
  @response['headers']['Location'] = url_for(url_params)
  @response['message'] = options['message'] || 'Moved temporarily'
  return ''
end

#render(options = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rho/render.rb', line 29

def render(options = nil)
	options = {} if options.nil? or !options.is_a?(Hash)
		options[:action] = :index if options[:action].nil?

		@content = eval_compiled_file(@request[:modelpath]+options[:action].to_s+'_erb.iseq', binding )

		if xhr? and options[:use_layout_on_ajax] != true
			options[:layout] = false
		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 + "_erb.iseq"
			puts 'Layout file: ' + layoutfile
			@content = eval_compiled_file(layoutfile, binding ) if File.exist?(layoutfile)
		end

		@content
end

#serve(object_mapping, req, res) ⇒ Object



14
15
16
17
18
19
# File 'lib/rho/rhocontroller.rb', line 14

def serve(object_mapping,req,res)
  @request, @response = req, res;
  @object_mapping = object_mapping
  @params = RhoSupport::query_params req
  send req['action'].nil? ? default_action : req['action']
end

#truncate(text, length) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/rho/rhoviewhelpers.rb', line 4

def truncate(text,length)
  if text
    omission = '...'
    l = length - omission.length
    (text.length > length ? text[0...l] + omission : text).to_s
  end
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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rho/rhoviewhelpers.rb', line 95

def url_for(params = {})
  return params.to_s if params.is_a? String or params.is_a? Symbol
  return '/' if not params.is_a? Hash or params.nil?

  application = params[:application] || @request['application']
  model = params[:controller] || params[:model] || @request['model'] 
  action = params[:action].nil? ? nil : params[:action].to_s
  id = params[:id].nil? ? nil : params[:id].to_s
  query = query_to_s(params[:query])
  fragment = params[:fragment].nil? ? '' : '#' + params[:fragment]
  
  amurl = '/' + application.to_s + '/' + model.to_s

  return amurl + query + fragment if action.nil? or action == 'create' or action == 'index'
  return amurl +'/'+ (id.nil? ? action : id + '/' + action) + query + fragment
end

#xml_http_request?Boolean Also known as: xhr?

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

Returns:

  • (Boolean)


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

def xml_http_request?
  not /XMLHttpRequest/i.match(@request['headers']['X-Requested-With']).nil?
end