Class: Make
- Inherits:
-
Object
- Object
- Make
- Defined in:
- lib/ulysseslin_test_gem.rb
Instance Method Summary collapse
- #crud ⇒ Object
-
#custom(columns, rows) ⇒ Object
Make custom-sized table; send in 0 for default col/row #.
-
#file ⇒ Object
Writes table html code to ‘table_html.txt’ file located in application’s root folder.
-
#initialize ⇒ Make
constructor
attr_accessor :html.
-
#now! ⇒ Object
Needs to be at end of links.
-
#table(model, *rest) ⇒ Object
Must provide model as a String, uppercase.
-
#th(*ths) ⇒ Object
Make custom headers.
Constructor Details
#initialize ⇒ Make
attr_accessor :html
3 4 5 6 |
# File 'lib/ulysseslin_test_gem.rb', line 3 def initialize @thead='' @tbody='' end |
Instance Method Details
#crud ⇒ Object
70 71 72 |
# File 'lib/ulysseslin_test_gem.rb', line 70 def crud end |
#custom(columns, rows) ⇒ Object
Make custom-sized table; send in 0 for default col/row #
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ulysseslin_test_gem.rb', line 59 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 |
#file ⇒ Object
Writes table html code to ‘table_html.txt’ file located in application’s root folder
75 76 77 78 |
# File 'lib/ulysseslin_test_gem.rb', line 75 def file 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 |
#now! ⇒ Object
Needs to be at end of links
81 82 83 |
# File 'lib/ulysseslin_test_gem.rb', line 81 def now! return ('<table><thead><tr>'+@thead+'</tr></thead><tbody>'+@tbody+'</tbody></table>').html_safe end |
#table(model, *rest) ⇒ Object
Must provide model as a String, uppercase
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 49 |
# File 'lib/ulysseslin_test_gem.rb', line 8 def table model,*rest controller=model.downcase+"s" # Convert given model String into Object model=model.constantize.all # 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 # HEADER # No headers specified; default headers used columns.each {|key| if key[-3..-1]=='_id' key=key[0...-3] end @thead+="\n\t\t\t<th>"+key.gsub('_',' ').titleize+'</th>' } @thead+="\n\t\t\t<th>Action</th>" # BODY # Make table from given model and row limit, if any if rest.length limit=model.length-1 # Set limit to custom value if not nil, 0, or model.length if ![nil,0,model.length].include?(rest[1]) limit=rest[1]-1 end # Show rows from models array until specified or default limit model[0..limit].each {|user| @tbody+="\n\t\t<tr>" user.attributes.except(*@keys_to_ignore).each {|key,val| if key[-3..-1]=='_id' # Use '.keys' & '.values' to get certain # key or value from hash val=key[0...-3].capitalize.constantize.find(val).attributes.values[val] end @tbody+="\n\t\t\t<td>"+val.to_s+'</td>' } 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>" @tbody+=crud+"\n\t\t</tr>" } end return self end |
#th(*ths) ⇒ Object
Make custom headers
52 53 54 55 56 |
# File 'lib/ulysseslin_test_gem.rb', line 52 def th *ths @thead='' ths.each { |custom_header| @thead+="\n\t\t\t<th>%s</th>" % custom_header } return self end |