make 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fdb05908a60e9123a53a661f39d8c86050f21261
4
- data.tar.gz: fa17b88c27383792c321620785348540c8fd80c5
3
+ metadata.gz: 358db4322a2767db4c9dd0c00e99ee89ca7951a1
4
+ data.tar.gz: b50babfd88a375ca57b3d86a7844e442a9c59c0d
5
5
  SHA512:
6
- metadata.gz: 19940ec381374c5b9a21ba2181419a6d44d1c35a7a85ce15dc050886a3c0bec092d58b4cf3cb9c10ab8662e9c95ac72dee0dacd80c258ad00f3cdb906e7b07bf
7
- data.tar.gz: 940ea0f3bab7942ba5b1e13d7cb054c27a8b3f882cbc9efd6e07e96a6d6fcf26fb72155631f720426677dde53035f3a83a5e0bbf6274f971325143b65893739f
6
+ metadata.gz: 51f33682f3ec11e429d66a596ccf475741fc91940b23fd88a48f56284fbf65af61218215527b11fdb0e24afb9954f56373ec39f8dcba5297adede38a46727732
7
+ data.tar.gz: 53c09c37b3e70b2456c529610c7e85b8be709c06b73b75caeb2c40856132e000ae40fb5da1792b41c5fb3794e7b19b54cb8a4946bc74745bedfcb0f7e9e701fd
@@ -1,17 +1,19 @@
1
- require 'make/builder.rb'
1
+ require 'make/header.rb'
2
+ require 'make/form.rb'
3
+ require 'make/table.rb'
4
+
2
5
  module Make
3
- def initialize
4
- @thead=''
5
- @tbody=''
6
- end
7
6
  def self.header(title = 'title here')
8
- string = Builder.header(title)
7
+ string = Header.header(title)
9
8
  string = string.html_safe
10
9
  return string
11
10
  end
12
11
  def self.form(input, options = {})
13
- string = Builder.form(input.to_s, input.column_names, options)
12
+ string = Form.form(input.to_s, input.column_names, options)
14
13
  string = string.html_safe
15
14
  return string
16
15
  end
16
+ def self.table
17
+ return Table.new
18
+ end
17
19
  end
@@ -1,7 +1,4 @@
1
- module Builder
2
- def self.header(title)
3
- return '<meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>'+title+'</title><meta name="description" content=""><meta name="viewport" content="width=device-width, initial-scale=1">'
4
- end
1
+ module Form
5
2
  def self.form(model, columnNames, options = {})
6
3
  # Convert given model into String
7
4
  controller=model.to_s.downcase+"s"
@@ -0,0 +1,5 @@
1
+ class Header
2
+ def self.header(title)
3
+ return '<meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>'+title+'</title><meta name="description" content=""><meta name="viewport" content="width=device-width, initial-scale=1">'
4
+ end
5
+ end
@@ -0,0 +1,116 @@
1
+ class Table
2
+ def initialize
3
+ @thead=''
4
+ @tbody=''
5
+ # These are the table keys to ignore when rendering table
6
+ @keys_to_ignore=['id','created_at','updated_at']
7
+ @keys_to_show=[]
8
+ @combination=[]
9
+ end
10
+
11
+ # Assigns desired column names to be combined
12
+ def combo cols
13
+ cols.each { |result,parts|
14
+ @combination=parts
15
+ @keys_to_ignore+=parts
16
+ @keys_to_show.push(result)
17
+ }
18
+ return self
19
+ end
20
+
21
+ # Assigns previously hidden column names as to be shown
22
+ def show *to_show
23
+ @keys_to_ignore-=to_show
24
+ return self
25
+ end
26
+
27
+ # Must provide model as a String, uppercase
28
+ 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
33
+
34
+ # HEADER
35
+ # Makes default headers based on column names
36
+ columns.each {|key|
37
+ if key[-3..-1]=='_id'
38
+ key=key[0...-3]
39
+ end
40
+ @thead+="\n\t\t\t<th>"+key.gsub('_',' ').titleize+'</th>'
41
+ }
42
+ @thead+="\n\t\t\t<th>Action</th>"
43
+
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
51
+ end
52
+
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>'
61
+ 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>"
77
+ }
78
+ end
79
+ return self
80
+ end
81
+
82
+ # Makes custom headers
83
+ def th *ths
84
+ @thead=''
85
+ ths.each { |custom_header| @thead+="\n\t\t\t<th>%s</th>" % custom_header }
86
+ return self
87
+ end
88
+
89
+ # Makes custom-sized table; send in 0 for default col/row #
90
+ def custom columns,rows
91
+ for row in 1..rows
92
+ @tbody+="\n\t\t<tr>"
93
+ for column in 1..columns
94
+ @tbody+="\n\t\t\t<td></td>"
95
+ end
96
+ @tbody+="\n\t\t</tr>"
97
+ end
98
+ return self
99
+ end
100
+
101
+ # Writes table html code to 'table_html.txt' file located in application's root folder
102
+ def file
103
+ 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>")) }
104
+ return self
105
+ end
106
+
107
+ # 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>"
110
+ end
111
+
112
+ # Place table code into view
113
+ def now!
114
+ return ('<table><thead><tr>'+@thead+'</tr></thead><tbody>'+@tbody+'</tbody></table>').html_safe
115
+ end
116
+ 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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sara Wong
@@ -9,13 +9,13 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-06 00:00:00.000000000 Z
12
+ date: 2014-02-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A gem that shortcuts typing out forms for SQL users. Assuming your model
15
- is named DB_Name, just type <%= Make.form(DB_Name) %> wherever in your Rails view
15
+ is named Model_Name, just type <%= Make.form(Model) %> wherever in your Rails view
16
16
  and it will return an html-safe string from your column names for creating a new
17
- record. In addition, you can type <%= Make.table(DB_Name.all) %> to automatically
18
- generate a table. More functions to follow!
17
+ record. In addition, you can type <%= Make.table.model("Table_Name").now! %> to
18
+ automatically generate a table. More functions to follow!
19
19
  email:
20
20
  - swong2@wellesley.edu
21
21
  - utemoc@gmail.com
@@ -25,7 +25,9 @@ extensions: []
25
25
  extra_rdoc_files: []
26
26
  files:
27
27
  - lib/make.rb
28
- - lib/make/builder.rb
28
+ - lib/make/header.rb
29
+ - lib/make/form.rb
30
+ - lib/make/table.rb
29
31
  - bin/make
30
32
  homepage: http://rubygems.org/gems/make
31
33
  licenses: