1
0
Fork 0
mirror of https://github.com/omniauth/omniauth.git synced 2022-11-09 12:31:49 -05:00
omniauth--omniauth/lib/omniauth/form.rb

113 lines
2.5 KiB
Ruby
Raw Permalink Normal View History

2011-09-03 14:08:07 -04:00
module OmniAuth
2015-12-18 22:14:29 -05:00
class Form
2014-01-15 23:00:46 -05:00
DEFAULT_CSS = File.read(File.expand_path('../form.css', __FILE__))
2011-09-03 14:08:07 -04:00
attr_accessor :options
def initialize(options = {})
2014-01-15 23:00:46 -05:00
options[:title] ||= 'Authentication Info Required'
options[:header_info] ||= ''
2011-09-03 14:08:07 -04:00
self.options = options
2020-12-10 10:21:53 -05:00
@html = +'' # unary + string allows it to be mutable if strings are frozen
@with_custom_button = false
@footer = nil
2014-01-15 23:00:46 -05:00
header(options[:title], options[:header_info])
2011-09-03 14:08:07 -04:00
end
2014-01-15 23:00:46 -05:00
def self.build(options = {}, &block)
form = OmniAuth::Form.new(options)
if block.arity > 0
yield form
else
form.instance_eval(&block)
end
form
2011-09-03 14:08:07 -04:00
end
def label_field(text, target)
@html << "\n<label for='#{target}'>#{text}:</label>"
self
end
def input_field(type, name)
@html << "\n<input type='#{type}' id='#{name}' name='#{name}'/>"
self
end
def text_field(label, name)
label_field(label, name)
input_field('text', name)
self
end
def password_field(label, name)
label_field(label, name)
input_field('password', name)
self
end
def button(text)
@with_custom_button = true
2011-09-03 14:08:07 -04:00
@html << "\n<button type='submit'>#{text}</button>"
end
def html(html)
@html << html
end
def fieldset(legend, options = {}, &block)
@html << "\n<fieldset#{" style='#{options[:style]}'" if options[:style]}#{" id='#{options[:id]}'" if options[:id]}>\n <legend>#{legend}</legend>\n"
2014-01-15 23:00:46 -05:00
instance_eval(&block)
2011-09-03 14:08:07 -04:00
@html << "\n</fieldset>"
self
end
2014-01-15 23:00:46 -05:00
def header(title, header_info)
2011-09-03 14:08:07 -04:00
@html << <<-HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2011-09-03 14:08:07 -04:00
<title>#{title}</title>
#{css}
#{header_info}
</head>
<body>
<h1>#{title}</h1>
<form method='post' #{"action='#{options[:url]}' " if options[:url]}noValidate='noValidate'>
HTML
self
end
def footer
return self if @footer
@html << "\n<button type='submit'>Connect</button>" unless @with_custom_button
2011-09-03 14:08:07 -04:00
@html << <<-HTML
</form>
</body>
</html>
HTML
@footer = true
self
end
def to_html
footer
@html
end
def to_response
footer
2014-01-15 23:00:46 -05:00
Rack::Response.new(@html, 200, 'content-type' => 'text/html').finish
2011-09-03 14:08:07 -04:00
end
2014-01-15 23:00:46 -05:00
protected
2011-09-03 14:08:07 -04:00
def css
"\n<style type='text/css'>#{OmniAuth.config.form_css}</style>"
end
end
end