make 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/make +3 -12
- data/lib/make.rb +3 -0
- data/lib/make/table.rb +96 -40
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6edbcf13869122e86247eb3fae3e2125536899f3
|
4
|
+
data.tar.gz: 4a753ba447898bb7938bf38eef4c5a0bcd24de42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d79b384a7f41e8547da259ec3133bfab9e64540a51450850d3d31bf44c1d179982541e87d900f264c62ae9175a0fc8a6ac8875480228733d04a3d35d6b0e658d
|
7
|
+
data.tar.gz: e55a3771bea39a6902ed6b254070cca907aa2b88f83a7b428e114af0e90b79d6058471a9336422b1427bef04d961106ecc12af808caacd50c4c76e0a92422eb4
|
data/bin/make
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require '
|
4
|
-
puts
|
5
|
-
model = gets.chomp
|
6
|
-
puts print "Would you like a table or form? (t or f)"
|
7
|
-
type = gets.chomp
|
8
|
-
if type == 't'
|
9
|
-
result = Make.table.model(model).now!
|
10
|
-
else
|
11
|
-
result = Make.form.model(model).now!
|
12
|
-
end
|
13
|
-
print result
|
2
|
+
|
3
|
+
require 'sarawong'
|
4
|
+
puts Sarawong.hi(ARGV[0])
|
data/lib/make.rb
CHANGED
data/lib/make/table.rb
CHANGED
@@ -6,6 +6,10 @@ class Table
|
|
6
6
|
@keys_to_ignore=['id','created_at','updated_at']
|
7
7
|
@keys_to_show=[]
|
8
8
|
@combination=[]
|
9
|
+
@columns=[]
|
10
|
+
@crud=''
|
11
|
+
@all_columns=[]
|
12
|
+
@run_make_head=true
|
9
13
|
end
|
10
14
|
|
11
15
|
# Assigns desired column names to be combined
|
@@ -15,6 +19,7 @@ class Table
|
|
15
19
|
@keys_to_ignore+=parts
|
16
20
|
@keys_to_show.push(result)
|
17
21
|
}
|
22
|
+
# remake_columns
|
18
23
|
return self
|
19
24
|
end
|
20
25
|
|
@@ -26,63 +31,100 @@ class Table
|
|
26
31
|
|
27
32
|
# Must provide model as a String, uppercase
|
28
33
|
def model model,*rest
|
29
|
-
controller=model.downcase
|
30
|
-
# Convert given model String into Object
|
31
|
-
model=model.constantize.all
|
32
|
-
|
34
|
+
@controller=model.downcase.pluralize
|
35
|
+
# Convert given model String into Object with .constantize
|
36
|
+
@model=model.constantize.all
|
37
|
+
@all_columns=@model.column_names
|
38
|
+
@limit=@model.length-1
|
39
|
+
# remake_columns
|
40
|
+
return self
|
41
|
+
end
|
42
|
+
|
43
|
+
# Defines column(s) to ignore
|
44
|
+
def cut *columns
|
45
|
+
@keys_to_ignore+=columns
|
46
|
+
# remake_columns
|
47
|
+
return self
|
48
|
+
end
|
49
|
+
|
50
|
+
# Redefines columns after some change
|
51
|
+
def remake_columns
|
52
|
+
@columns=@keys_to_show+@all_columns-@keys_to_ignore
|
53
|
+
end
|
33
54
|
|
55
|
+
# Defines row limit
|
56
|
+
def limit row
|
57
|
+
@limit=row-1
|
58
|
+
return self
|
59
|
+
end
|
60
|
+
|
61
|
+
# Defines order
|
62
|
+
def order rule
|
63
|
+
@model=@model.order(rule)
|
64
|
+
return self
|
65
|
+
end
|
66
|
+
|
67
|
+
def make_head
|
34
68
|
# HEADER
|
35
69
|
# Makes default headers based on column names
|
36
|
-
columns.each {|key|
|
70
|
+
@columns.each {|key|
|
37
71
|
if key[-3..-1]=='_id'
|
38
72
|
key=key[0...-3]
|
39
73
|
end
|
40
|
-
|
74
|
+
# Ignores any column name containing "password"
|
75
|
+
if key.include? "password"
|
76
|
+
@keys_to_ignore.push(key)
|
77
|
+
else
|
78
|
+
@thead+="\n\t\t\t<th>"+key.gsub('_',' ').titleize+'</th>'
|
79
|
+
end
|
41
80
|
}
|
42
81
|
@thead+="\n\t\t\t<th>Action</th>"
|
82
|
+
end
|
83
|
+
|
84
|
+
# Makes table from given model and row limit, if any
|
85
|
+
def make_body
|
86
|
+
# Set limit to custom value if not nil, 0, or @model.length
|
43
87
|
|
44
|
-
|
45
|
-
# Makes
|
46
|
-
|
47
|
-
|
48
|
-
#
|
49
|
-
if
|
50
|
-
|
88
|
+
|
89
|
+
# Makes <tr>s from @model array until specified or default limit
|
90
|
+
@model[0..@limit].each {|user|
|
91
|
+
@tbody+="\n\t\t<tr>"
|
92
|
+
# Makes merged columns, if specified
|
93
|
+
if @combination.length!=0
|
94
|
+
all_vals=[]
|
95
|
+
@combination.each { |col| all_vals.push(user.attributes[col].to_s) }
|
96
|
+
@tbody+="\n\t\t\t<td>"+all_vals.join(" ")+'</td>'
|
51
97
|
end
|
52
98
|
|
53
|
-
# Makes <
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
99
|
+
# Makes <td>s
|
100
|
+
user.attributes.except(*@keys_to_ignore).each {|key,val|
|
101
|
+
# Auto connects with related table
|
102
|
+
if key[-3..-1]=='_id'
|
103
|
+
@temp=val
|
104
|
+
# Use '.keys' & '.values' to get certain # key or value from hash
|
105
|
+
# .values[1] grabs column after 'id'
|
106
|
+
val=key[0...-3].capitalize.constantize.find(val).attributes.values[1]
|
107
|
+
elsif key=='created_at' || key=='updated_at'
|
108
|
+
val=val.strftime("%b %d, %Y %I:%M %p")
|
61
109
|
end
|
62
|
-
|
63
|
-
# Makes <td>s
|
64
|
-
user.attributes.except(*@keys_to_ignore).each {|key,val|
|
65
|
-
if key[-3..-1]=='_id'
|
66
|
-
# Use '.keys' & '.values' to get certain # key or value from hash
|
67
|
-
val=key[0...-3].capitalize.constantize.find(val).attributes.values[val]
|
68
|
-
elsif key=='created_at' || key=='updated_at'
|
69
|
-
val=val.strftime("%b %d, %Y %I:%M %p")
|
70
|
-
end
|
71
|
-
@tbody+="\n\t\t\t<td>"+val.to_s+'</td>'
|
72
|
-
}
|
73
|
-
|
74
|
-
# Generates (C)RUD links
|
75
|
-
crud="\n\t\t\t<td><a href=\"/"+controller+"/"+user.id.to_s+"\">Show</a> | <a href=\"/"+controller+"/"+user.id.to_s+"/edit\">Edit</a> | <a href=\"/"+controller+"/"+user.id.to_s+"\" data-method=\"delete\">Delete</a></td>"
|
76
|
-
@tbody+=crud+"\n\t\t</tr>"
|
110
|
+
@tbody+="\n\t\t\t<td>"+val.to_s+'</td>'
|
77
111
|
}
|
78
|
-
|
79
|
-
|
112
|
+
|
113
|
+
# Generates (C)RUD links
|
114
|
+
@tbody+="\n\t\t\t"+crud(user.id.to_s)+"\n\t\t</tr>"
|
115
|
+
}
|
116
|
+
# return self
|
117
|
+
end
|
118
|
+
|
119
|
+
def crud id
|
120
|
+
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>"
|
80
121
|
end
|
81
122
|
|
82
123
|
# Makes custom headers
|
83
124
|
def th *ths
|
84
125
|
@thead=''
|
85
126
|
ths.each { |custom_header| @thead+="\n\t\t\t<th>%s</th>" % custom_header }
|
127
|
+
@run_make_head=false
|
86
128
|
return self
|
87
129
|
end
|
88
130
|
|
@@ -105,12 +147,26 @@ class Table
|
|
105
147
|
end
|
106
148
|
|
107
149
|
# Generates for loop code in 'table_html.txt'
|
108
|
-
def for!
|
109
|
-
|
150
|
+
def for! variable
|
151
|
+
@columns=@all_columns
|
152
|
+
if @run_make_head
|
153
|
+
make_head
|
154
|
+
end
|
155
|
+
singular=variable[1..-1].singularize
|
156
|
+
tds=''
|
157
|
+
@all_columns.each { |col| tds+="\n\t\t\t\t<td><%= "+singular+"['"+col+"'] %></td>" if !col.include? "password" }
|
158
|
+
tds+="\n\t\t\t\t"+crud("<%= "+singular+"['id'] %>")
|
159
|
+
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")) }
|
160
|
+
return self
|
110
161
|
end
|
111
162
|
|
112
163
|
# Place table code into view
|
113
164
|
def now!
|
165
|
+
remake_columns
|
166
|
+
if @run_make_head
|
167
|
+
make_head
|
168
|
+
end
|
169
|
+
make_body
|
114
170
|
return ('<table><thead><tr>'+@thead+'</tr></thead><tbody>'+@tbody+'</tbody></table>').html_safe
|
115
171
|
end
|
116
172
|
end
|