make 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/make +3 -12
  3. data/lib/make.rb +3 -0
  4. data/lib/make/table.rb +96 -40
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 714cc72109a10f3fbad6757b95cda1cdb4fd99c6
4
- data.tar.gz: dd7a5ca7a7315a39e7bdc7b13cce87c190dd308d
3
+ metadata.gz: 6edbcf13869122e86247eb3fae3e2125536899f3
4
+ data.tar.gz: 4a753ba447898bb7938bf38eef4c5a0bcd24de42
5
5
  SHA512:
6
- metadata.gz: 48c8421360d0b7da25cbfda6da60014b345f4974141f3969e004927c18b8f83908ae7fce48f3cc6967ce81975d371a9b6776b34f7f9e70b5b9036552c5615aaf
7
- data.tar.gz: f15f7aceed3f4edce08d6c5566499fe21695f41b1b06ea56d6748d22cc00ae364676baabb2bd91979702780d06f1cfe444f3e4afa6481dfd3087674d1da5a978
6
+ metadata.gz: d79b384a7f41e8547da259ec3133bfab9e64540a51450850d3d31bf44c1d179982541e87d900f264c62ae9175a0fc8a6ac8875480228733d04a3d35d6b0e658d
7
+ data.tar.gz: e55a3771bea39a6902ed6b254070cca907aa2b88f83a7b428e114af0e90b79d6058471a9336422b1427bef04d961106ecc12af808caacd50c4c76e0a92422eb4
data/bin/make CHANGED
@@ -1,13 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- require "#{ENV['RAILS_ROOT']}/config/environment"
3
- require 'make'
4
- puts print "Please input your Model name"
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
@@ -14,4 +14,7 @@ module Make
14
14
  def self.table
15
15
  Table.new
16
16
  end
17
+ def self.ctable
18
+ Ctable.new
19
+ end
17
20
  end
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+"s"
30
- # Convert given model String into Object
31
- model=model.constantize.all
32
- columns=@keys_to_show+model.column_names-@keys_to_ignore
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
- @thead+="\n\t\t\t<th>"+key.gsub('_',' ').titleize+'</th>'
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
- # BODY
45
- # Makes table from given model and row limit, if any
46
- if rest.length
47
- limit=model.length-1
48
- # Set limit to custom value if not nil, 0, or model.length
49
- if ![nil,0,model.length].include?(rest[1])
50
- limit=rest[1]-1
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 <tr>s from models array until specified or default limit
54
- model[0..limit].each {|user|
55
- @tbody+="\n\t\t<tr>"
56
- # Makes merged columns, if specified
57
- if @combination.length!=0
58
- all_vals=[]
59
- @combination.each { |col| all_vals.push(user.attributes[col].to_s) }
60
- @tbody+="\n\t\t\t<td>"+all_vals.join(" ")+'</td>'
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
- end
79
- return self
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
- # "<table>\n\t<thead>\n\t\t<tr>"+@thead+"\n\t\t</tr>\n\t</thead>\n\t<tbody><% for |row| in "++"\n\t</tbody>\n</table>"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: make
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sara Wong