Class: Form

Inherits:
Object
  • Object
show all
Defined in:
lib/make/form.rb

Instance Method Summary collapse

Constructor Details

#initializeForm

Returns a new instance of 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

#class(myClass) ⇒ Object

Change form class



36
37
38
39
# File 'lib/make/form.rb', line 36

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.



41
42
43
44
45
46
# File 'lib/make/form.rb', line 41

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

#method(type, id) ⇒ Object

Change the form method to put, patch, or delete.



53
54
55
56
57
# File 'lib/make/form.rb', line 53

def method type, id
	@formMethodHidden = type
	@id = id
	return self
end

#middleObject

Build remaining code from class variables called upon by now!



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/make/form.rb', line 87

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
	else
		input = "\n\t<label>" + column.titleize + "\n\t</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_confirmObject

Default requires password confirmation, if none is needed change it will no longer require it.



48
49
50
51
# File 'lib/make/form.rb', line 48

def no_pw_confirm
	@password_confirmation = false
	return self
end

#now!Object

At the end of building the form, return the final string.



117
118
119
120
121
# File 'lib/make/form.rb', line 117

def now!
	starter
	middle
	return (@formStart+@formMiddle.join+@formEnd).html_safe
end

#select(column, array, assoc = false) ⇒ Object

Similar to default, but instead of a hidden specific value you pass in an array and create a select/option field.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/make/form.rb', line 59

def select column, array, assoc = false
	columnName = column.titleize
	input = "\n\t<label>" + columnName + "\n\t</label>\n\t<select name=\"" + @modelName + "[" + column + "]\">"
	if assoc		#if association is checked true
		array.each do |id|
			val = column[0...-3].capitalize.constantize.find(id).attributes.values[1]
			input += "\n\t\t\t<option value=\"" + id.to_s + "\">" + val.to_s + "</option>"
		end
	else			#if no associations exist for array
		array.each do |item| 
			input += "\n\t\t\t<option value=\"" + item.to_s + "\">" + item.to_s + "</option>"
		end
	end
	@potential_keys_to_ignore.push(column)
	input += "\n\t</select>"
	@formMiddle.push(input)
	return self
end

#starterObject

Build starting code from class variables called upon by now!



78
79
80
81
82
83
84
85
# File 'lib/make/form.rb', line 78

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