Class: Make

Inherits:
Object
  • Object
show all
Defined in:
lib/ulysseslin_test_gem.rb

Class Method Summary collapse

Class Method Details

.table(model, *rest) ⇒ Object

(so headers cannot be ‘0’)



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ulysseslin_test_gem.rb', line 4

def self.table model,*rest
	# These are the table keys to ignore when rendering table
	@keys_to_ignore=['id','created_at','updated_at']
	columns=model.column_names-@keys_to_ignore
	@html='<table><thead><tr>'
	# HEADER
	# No headers specified; default headers used
	if rest[0]==nil
		columns.each {|key| @html+='<th>'+key.gsub('_',' ').titleize+'</th>'}
		@html+='</tr></thead>'
	# Headers specified
	else
		rest[0].each { |header| @html+='<th>%s</th>' % header }
		@html+='</tr></thead>'
	end

	# BODY
	@html+='<tbody>'
	# Make custom table if no model given
	if model==0
		for r in 1..rest[2]
			@html+='<tr>'
			for c in 1..rest[1]
				@html+='<td>%d</td>' % c
			end
			@html+='</tr>'
		end
	# Make table from given model and row limit, if any
	else
		limit=model.length-1
		if rest[2]!=nil && rest[2]>0
			limit=rest[2]-1
		end
		# Show rows from models array until specified or default limit
		model[0..limit].each {|user|
			@html+='<tr>'
			user.attributes.except(*@keys_to_ignore).each {|key,val|
				@html+='<td>'+val+'</td>'
			}
			@html+='</tr>'
		}
	end
	@html+='</tbody></table>'
	@html.html_safe
end