Class: Table

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

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/make/table.rb', line 2

def initialize
	@thead=''
	@tbody=''
	# These are the table keys to ignore when rendering table
	@keys_to_ignore=['id','created_at','updated_at']
	@keys_to_show=[]
	@combination=[]
	@columns=[]
	@crud=''
	@all_columns=[]
	@run_make_head=true
	@file=false
end

Instance Method Details

#combo(cols) ⇒ Object

Assigns desired column names to be combined



17
18
19
20
21
22
23
24
25
# File 'lib/make/table.rb', line 17

def combo cols
	cols.each { |result,parts|
		@combination=parts
		@keys_to_ignore+=parts
		@keys_to_show.push(result)
	}
	# remake_columns
	return self
end

#crud(id) ⇒ Object



120
121
122
# File 'lib/make/table.rb', line 120

def crud id
	return "<td><a href=\"/"+@controller+"/"+id+"\">Show</a> | <a href=\"/"+@controller+"/"+id+"/edit\">Edit</a> | <a href=\"/"+@controller+"/"+id+"\" data-method=\"delete\">Delete</a></td>"
end

#custom(columns, rows) ⇒ Object

Makes custom-sized table; send in 0 for default col/row #



133
134
135
136
137
138
139
140
141
142
# File 'lib/make/table.rb', line 133

def custom columns,rows
	for row in 1..rows
		@tbody+="\n\t\t<tr>"
		for column in 1..columns
			@tbody+="\n\t\t\t<td></td>"
		end
		@tbody+="\n\t\t</tr>"
	end
	return self
end

#cut(*columns) ⇒ Object

Defines column(s) to ignore



45
46
47
48
49
# File 'lib/make/table.rb', line 45

def cut *columns
	@keys_to_ignore+=columns
	# remake_columns
	return self
end

#file!Object

Writes table html code to ‘table_html.txt’ file located in application’s root folder



145
146
147
148
149
150
151
152
153
154
# File 'lib/make/table.rb', line 145

def file!
	remake_columns
	if @run_make_head
		make_head
	end
	make_body
	@file=true
	File.open('table_html.txt', 'w') { |f| f.write(("<table>\n\t<thead>\n\t\t<tr>"+@thead+"\n\t\t</tr>\n\t</thead>\n\t<tbody>"+@tbody+"\n\t</tbody>\n</table>")) }
	return self
end

#for!(variable) ⇒ Object

Generates for loop code in ‘table_html.txt’



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/make/table.rb', line 157

def for! variable
	@columns=@all_columns
	if @run_make_head
		make_head
	end
	singular=variable[1..-1].singularize
	tds=''
	@all_columns.each { |col| tds+="\n\t\t\t\t<td><%= "+singular+"['"+col+"'] %></td>" if !col.include? "password" }
	tds+="\n\t\t\t\t"+crud("<%= "+singular+"['id'] %>")
	File.open('table_html.txt', 'w') { |f| f.write(("<table>\n\t<thead>\n\t\t<tr>"+@thead+"\n\t\t</tr>\n\t</thead>\n\t<tbody>\n\t\t<% for |"+singular+"| in "+variable+" do %>\n\t\t\t<tr>"+tds+"\n\t\t\t</tr>\n\t\t<% end %>\n\t</tbody>\n</table>\n\n\n<--! PUT THE FOLLOWING LINE INTO YOUR CONTROLLER -->\n"+variable+" = "+singular.capitalize+".all")) }
	return self
end

#limit(row) ⇒ Object

Defines row limit



57
58
59
60
# File 'lib/make/table.rb', line 57

def limit row
	@limit=row-1
	return self
end

#make_bodyObject

Makes table from given model and row limit, if any



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/make/table.rb', line 86

def make_body
	# Set limit to custom value if not nil, 0, or @model.length


	# Makes <tr>s from @model array until specified or default limit
	@model[0..@limit].each {|user|
		@tbody+="\n\t\t<tr>"
		# Makes merged columns, if specified
		if @combination.length!=0
			all_vals=[]
			@combination.each { |col| all_vals.push(user.attributes[col].to_s) }
			@tbody+="\n\t\t\t<td>"+all_vals.join(" ")+'</td>'
		end

		# Makes <td>s
		user.attributes.except(*@keys_to_ignore).each {|key,val|
			# Auto connects with related table
			if key[-3..-1]=='_id'
				@temp=val
				# Use '.keys' & '.values' to get certain # key or value from hash
				# .values[1] grabs column after 'id'
				val=key[0...-3].capitalize.constantize.find(val).attributes.values[1]
			elsif key=='created_at' || key=='updated_at'
				val=val.strftime("%b %d, %Y %I:%M %p")
			end
			@tbody+="\n\t\t\t<td>"+val.to_s+'</td>'
		}

		# Generates (C)RUD links
		@tbody+="\n\t\t\t"+crud(user.id.to_s)+"\n\t\t</tr>"
	}
	# return self
end

#make_headObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/make/table.rb', line 68

def make_head
	# HEADER
	# Makes default headers based on column names
	@columns.each {|key|
		if key[-3..-1]=='_id'
			key=key[0...-3]
		end
		# Ignores any column name containing "password"
		if key.include? "password"
			@keys_to_ignore.push(key)
		else
			@thead+="\n\t\t\t<th>"+key.gsub('_',' ').titleize+'</th>'
		end
	}
	@thead+="\n\t\t\t<th>Action</th>"
end

#model(model, *rest) ⇒ Object

Must provide model as a String, uppercase



34
35
36
37
38
39
40
41
42
# File 'lib/make/table.rb', line 34

def model model,*rest
	@controller=model.downcase.pluralize
	# Convert given model String into Object with .constantize
	@model=model.constantize.all
	@all_columns=@model.column_names
	@limit=@model.length-1
	# remake_columns
	return self
end

#now!Object

Place table code into view



171
172
173
174
175
176
177
178
179
180
# File 'lib/make/table.rb', line 171

def now!
	if !@file
		remake_columns
		if @run_make_head
			make_head
		end
		make_body
	end	
	return ('<table><thead><tr>'+@thead+'</tr></thead><tbody>'+@tbody+'</tbody></table>').html_safe
end

#order(rule) ⇒ Object

Defines order



63
64
65
66
# File 'lib/make/table.rb', line 63

def order rule
	@model=@model.order(rule)
	return self
end

#remake_columnsObject

Redefines columns after some change



52
53
54
# File 'lib/make/table.rb', line 52

def remake_columns
	@columns=@keys_to_show+@all_columns-@keys_to_ignore
end

#show(*to_show) ⇒ Object

Assigns previously hidden column names as to be shown



28
29
30
31
# File 'lib/make/table.rb', line 28

def show *to_show
	@keys_to_ignore-=to_show
	return self
end

#th(*ths) ⇒ Object

Makes custom headers



125
126
127
128
129
130
# File 'lib/make/table.rb', line 125

def th *ths
	@thead=''
	ths.each { |custom_header| @thead+="\n\t\t\t<th>%s</th>" % custom_header }
	@run_make_head=false
	return self
end