ulysseslin_test_gem 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ulysseslin_test_gem.rb +65 -30
  3. metadata +7 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f06209383569568c3192c273c62587dd00acea9
4
- data.tar.gz: 6f08b52b68295a94107b525f5134bc8cf682e033
3
+ metadata.gz: ed1fc633f51c42e95467019ed96b72ed24c799b7
4
+ data.tar.gz: ecdb3b5556bae92c75031330adf0e3ffb8d6d01d
5
5
  SHA512:
6
- metadata.gz: 367215e284acde2bd7d9879f7cb0974fd4bc2ff24cbc6b4ba8a02c3f1d6ab4c8282f216c7ac164470e16b42e81cd357fe9bbfb5906491019e3f63a44ee6cf56f
7
- data.tar.gz: ff345e69e51d8e232c47aca3119bb38f25ead3f3c4b8a4aae1ee7ecec3232e4877aaac60661a9347bfece59028547c3cb7f4dec157ef429bcaee69182816b229
6
+ metadata.gz: d23458ce018f6be0ee793c5c23ac7e3db2ff47fdb39ba387d0a364d6344a69010493a19aa90570a00836fd588e0d4069cf112bef255dfb029fea20d73973b8b1
7
+ data.tar.gz: 8a8b762f2f88acce8eaedacffaca27885baca4b51b7e6ac2c2226d136df9626b0ab3cc99b16f70ea0bfc2b716eaebaf444c106b7a6e34475cd6ce428d44112be
@@ -1,49 +1,84 @@
1
1
  class Make
2
- # If you want a custom table, you MUST provide headers as an array
3
- # (so headers cannot be '0')
4
- def self.table model,*rest
2
+ # attr_accessor :html
3
+ def initialize
4
+ @thead=''
5
+ @tbody=''
6
+ end
7
+ # Must provide model as a String, uppercase
8
+ def table model,*rest
9
+ controller=model.downcase+"s"
10
+ # Convert given model String into Object
11
+ model=model.constantize.all
12
+
5
13
  # These are the table keys to ignore when rendering table
6
14
  @keys_to_ignore=['id','created_at','updated_at']
7
15
  columns=model.column_names-@keys_to_ignore
8
- @html='<table><thead><tr>'
9
16
  # HEADER
10
17
  # No headers specified; default headers used
11
- if rest[0]==nil
12
- columns.each {|key| @html+='<th>'+key.gsub('_',' ').titleize+'</th>'}
13
- @html+='</tr></thead>'
14
- # Headers specified
15
- else
16
- rest[0].each { |header| @html+='<th>%s</th>' % header }
17
- @html+='</tr></thead>'
18
- end
18
+ columns.each {|key|
19
+ if key[-3..-1]=='_id'
20
+ key=key[0...-3]
21
+ end
22
+ @thead+="\n\t\t\t<th>"+key.gsub('_',' ').titleize+'</th>'
23
+ }
24
+ @thead+="\n\t\t\t<th>Action</th>"
19
25
 
20
26
  # BODY
21
- @html+='<tbody>'
22
- # Make custom table if no model given
23
- if model==0
24
- for r in 1..rest[2]
25
- @html+='<tr>'
26
- for c in 1..rest[1]
27
- @html+='<td>%d</td>' % c
28
- end
29
- @html+='</tr>'
30
- end
31
27
  # Make table from given model and row limit, if any
32
- else
28
+ if rest.length
33
29
  limit=model.length-1
34
- if rest[2]!=nil && rest[2]>0
35
- limit=rest[2]-1
30
+ # Set limit to custom value if not nil, 0, or model.length
31
+ if ![nil,0,model.length].include?(rest[1])
32
+ limit=rest[1]-1
36
33
  end
37
34
  # Show rows from models array until specified or default limit
38
35
  model[0..limit].each {|user|
39
- @html+='<tr>'
36
+ @tbody+="\n\t\t<tr>"
40
37
  user.attributes.except(*@keys_to_ignore).each {|key,val|
41
- @html+='<td>'+val+'</td>'
38
+ if key[-3..-1]=='_id'
39
+ # Use '.keys' & '.values' to get certain # key or value from hash
40
+ val=key[0...-3].capitalize.constantize.find(val).attributes.values[val]
41
+ end
42
+ @tbody+="\n\t\t\t<td>"+val.to_s+'</td>'
42
43
  }
43
- @html+='</tr>'
44
+ 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>"
45
+ @tbody+=crud+"\n\t\t</tr>"
44
46
  }
45
47
  end
46
- @html+='</tbody></table>'
47
- @html.html_safe
48
+ return self
49
+ end
50
+
51
+ # Make custom headers
52
+ def th *ths
53
+ @thead=''
54
+ ths.each { |custom_header| @thead+="\n\t\t\t<th>%s</th>" % custom_header }
55
+ return self
56
+ end
57
+
58
+ # Make custom-sized table; send in 0 for default col/row #
59
+ def custom columns,rows
60
+ for row in 1..rows
61
+ @tbody+="\n\t\t<tr>"
62
+ for column in 1..columns
63
+ @tbody+="\n\t\t\t<td></td>"
64
+ end
65
+ @tbody+="\n\t\t</tr>"
66
+ end
67
+ return self
68
+ end
69
+
70
+ def crud
71
+
72
+ end
73
+
74
+ # Writes table html code to 'table_html.txt' file located in application's root folder
75
+ def file
76
+ 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>")) }
77
+ return self
78
+ end
79
+
80
+ # Needs to be at end of links
81
+ def now!
82
+ return ('<table><thead><tr>'+@thead+'</tr></thead><tbody>'+@tbody+'</tbody></table>').html_safe
48
83
  end
49
84
  end
metadata CHANGED
@@ -1,17 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ulysseslin_test_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ulysses Lin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-06 00:00:00.000000000 Z
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Allows user to create tables simply by referencing a table in the database
14
- and/or defining table headers and column and row number
13
+ description: Allows user to create tables simply by referencing a table in the database,
14
+ defining headers, and changing size. Version 1.0.2 allow for cross-table recognition
15
+ of headers and values needed; for example, a two-model table would show "Electronics"
16
+ for "Category" instead of "2" for "category_id". Version 1.0.3 allows for method
17
+ chaining, printing of table code into an external file, and default (C)RUD links.
15
18
  email: utemoc@gmail.com
16
19
  executables: []
17
20
  extensions: []