class Make

Public Class Methods

new() click to toggle source

attr_accessor :html

# File lib/ulysseslin_test_gem.rb, line 3
def initialize
        @thead=''
        @tbody=''
end

Public Instance Methods

crud() click to toggle source
# File lib/ulysseslin_test_gem.rb, line 70
def crud

end
custom(columns,rows) click to toggle source

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

# 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() click to toggle source

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

# 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!() click to toggle source

Needs to be at end of links

# 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) click to toggle source

Must provide model as a String, uppercase

# 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) click to toggle source

Make custom headers

# 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