Class: Form
- Inherits:
-
Object
- Object
- Form
- Defined in:
- lib/make/form.rb
Instance Method Summary collapse
-
#action(url) ⇒ Object
change action url if specific and does not follow REST.
-
#assoc(column, assoc_column, assoc_model = nil) ⇒ Object
Similar to select, but association assumed and specific associated column name can be selected.
-
#class(myClass) ⇒ Object
Change form class.
-
#default(column, value) ⇒ Object
Determine default value for a specific field.
-
#hide(columns) ⇒ Object
hide array of specific fields that should not be submitted through.
-
#initialize ⇒ Form
constructor
A new instance of Form.
-
#method(type, id) ⇒ Object
Change the form method to put, patch, or delete.
-
#middle ⇒ Object
Build remaining code from class variables called upon by now!.
-
#model(model) ⇒ Object
First class method accessed in form, passing in form as a string.
-
#no_pw_confirm ⇒ Object
Default requires password confirmation, if none is needed change it will no longer require it.
-
#now! ⇒ Object
At the end of building the form, return the final string.
-
#select(column, array) ⇒ Object
Similar to default, but instead of a hidden specific value you pass in an array and create a select/option field.
-
#starter ⇒ Object
Build starting code from class variables called upon by now!.
Constructor Details
#initialize ⇒ Form
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/make/form.rb', line 2 def initialize # Convert given model into String @formClass='make-form' # By default, set form action and method to create using REST conventions @formMethod='post' @formMethodHidden=nil @formStart='' @formMiddle=[] @formEnd="\n\t<input id=\"authenticity_token\" name=\"authenticity_token\" type=\"hidden\" value=\"<%= form_authenticity_token %>\">\n\t<input type=\"submit\" value=\"Submit\">\n</form>" @default_keys_to_ignore = ['id', 'created_at', 'updated_at'] @potential_keys_to_ignore = ['salt'] @defaults = {} @keys_to_show=[] @selections = [] @password_confirmation = true @model = {} @modelName = '' @columns = [] @id = nil end |
Instance Method Details
#action(url) ⇒ Object
change action url if specific and does not follow REST
31 32 33 34 |
# File 'lib/make/form.rb', line 31 def action url @formAction = url return self end |
#assoc(column, assoc_column, assoc_model = nil) ⇒ Object
Similar to select, but association assumed and specific associated column name can be selected
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/make/form.rb', line 78 def assoc column, assoc_column, assoc_model=nil # titleize column columnName = column.titleize # Create for input input = "\n\t<label>" + columnName + "</label>\n\t<select name=\"" + @modelName + "[" + column + "]\">" # Remove _id and take the assocModel unless assocModel exists if assoc_model assoc_model = assoc_model.capitalize.constantize else assoc_model = column[0...-3].capitalize.constantize end # Keep track of how many records exist in assocModel total = assoc_model.distinct.count('id') # run a while loop for as many records there are counter = 1 while counter <= total do val = assoc_model.find(counter).attributes[assoc_column] input += "\n\t\t<option value=\"" + counter.to_s + "\">" + val.to_s + "</option>" counter+= 1 end input += "\n\t</select>" @potential_keys_to_ignore.push(column) @formMiddle.push(input) return self end |
#class(myClass) ⇒ Object
Change form class
43 44 45 46 |
# File 'lib/make/form.rb', line 43 def class myClass @formClass = myClass return self end |
#default(column, value) ⇒ Object
Determine default value for a specific field. Because no options exist, it will be hidden.
48 49 50 51 52 53 |
# File 'lib/make/form.rb', line 48 def default column, value @defaults = @defaults.merge({column => value}) # remove the created inputs from the columns Array (columnNames). @potential_keys_to_ignore.push(column.to_s) return self end |
#hide(columns) ⇒ Object
hide array of specific fields that should not be submitted through
36 37 38 39 40 41 |
# File 'lib/make/form.rb', line 36 def hide columns columns.each do |col| @potential_keys_to_ignore.push(col.to_s) end return self end |
#method(type, id) ⇒ Object
Change the form method to put, patch, or delete.
60 61 62 63 64 |
# File 'lib/make/form.rb', line 60 def method type, id @formMethodHidden = type @id = id return self end |
#middle ⇒ Object
Build remaining code from class variables called upon by now!
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/make/form.rb', line 113 def middle @columns = @keys_to_show + @model.column_names - @default_keys_to_ignore @potential_keys_to_ignore.each do |item| puts 'got in potential' @columns.delete(item) puts @columns puts @item end # Create text input labels @columns.each do |column| if column.include? 'password' input = "\n\t<label>Password</label>\n\t<input type=\"password\" name=\"" + @modelName + "[password]\">" @formMiddle.push(input) if @password_confirmation input = "\n\t<label>Confirm Password</label>\n\t<input type=\"password\" name=\"" + @modelName + "[password_confirmation]\">" @formMiddle.push(input) end elsif column.include? 'date' input = "\n\t<label>"+column.titleize+"</label>\n\t<input type=\"date\" name=\"" + @modelName + "[" + column + "]\">" @formMiddle.push(input) else input = "\n\t<label>" + column.titleize + "</label>\n\t<input type=\"text\" name=\"" + @modelName + "[" + column + "]\">" @formMiddle.push(input) end end @defaults.each_pair do |key, value| input = "\n\t<input type=\"hidden\" name=\"" + @modelName + '[' + key.to_s + "]\" value=\"" + value.to_s + "\">" puts input puts @formMiddle @formMiddle.push(input) end end |
#model(model) ⇒ Object
First class method accessed in form, passing in form as a string
23 24 25 26 27 28 29 |
# File 'lib/make/form.rb', line 23 def model model @modelName = model.downcase # Set default form's Action based on REST conventions /users @formAction = "/" + @modelName.downcase + "s" @model = model.constantize return self end |
#no_pw_confirm ⇒ Object
Default requires password confirmation, if none is needed change it will no longer require it.
55 56 57 58 |
# File 'lib/make/form.rb', line 55 def no_pw_confirm @password_confirmation = false return self end |
#now! ⇒ Object
At the end of building the form, return the final string.
146 147 148 149 150 |
# File 'lib/make/form.rb', line 146 def now! starter middle return (@formStart+@formMiddle.join+@formEnd).html_safe end |
#select(column, array) ⇒ Object
Similar to default, but instead of a hidden specific value you pass in an array and create a select/option field.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/make/form.rb', line 66 def select column, array columnName = column.titleize input = "\n\t<label>" + columnName + "</label>\n\t<select name=\"" + @modelName + "[" + column + "]\">" array.each do |item| input += "\n\t\t<option value=\"" + item.to_s + "\">" + item.to_s + "</option>" end @potential_keys_to_ignore.push(column) input += "\n\t</select>" @formMiddle.push(input) return self end |
#starter ⇒ Object
Build starting code from class variables called upon by now!
104 105 106 107 108 109 110 111 |
# File 'lib/make/form.rb', line 104 def starter if @formMethodHidden @formStart = "<form class=\"" + @formClass + "\" action=\"" + @formAction + "/"+ @id.to_s + "\" method=\"" + @formMethod + "\">" @formStart += "\n\t<input name=\"_method\" type=\"hidden\" value=\"" + @formMethodHidden + "\">" else @formStart = "<form class=\"" + @formClass + "\" action=\"" + @formAction + "\" method=\"" + @formMethod + "\">" end end |