-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,042 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
require 'lotus/helpers/html_helper/html_builder' | ||
require 'lotus/helpers/html_helper/html_node' | ||
require 'lotus/utils/string' | ||
|
||
module Lotus | ||
module Helpers | ||
module FormHelper | ||
DEFAULT_METHOD = 'POST'.freeze | ||
BROWSER_METHODS = ['GET', 'POST'].freeze | ||
|
||
CHECKED = 'checked'.freeze | ||
SELECTED = 'selected'.freeze | ||
ACCEPT_SEPARATOR = ','.freeze | ||
# ENCTYPE_MULTIPART = 'multipart/form-data'.freeze | ||
|
||
class HtmlNode < ::Lotus::Helpers::HtmlHelper::HtmlNode | ||
def initialize(name, content, attributes, options) | ||
super | ||
@builder = FormBuilder.new(options.fetch(:form_name), options.fetch(:params)) | ||
@verb = options.fetch(:verb, nil) | ||
end | ||
|
||
private | ||
def content | ||
_method_override! | ||
super | ||
end | ||
|
||
def _method_override! | ||
return if @verb.nil? | ||
|
||
verb = @verb | ||
@builder.resolve do | ||
input(type: :hidden, name: :_method, value: verb) | ||
end | ||
end | ||
end | ||
|
||
class FormBuilder < ::Lotus::Helpers::HtmlHelper::HtmlBuilder | ||
self.html_node = ::Lotus::Helpers::FormHelper::HtmlNode | ||
|
||
def initialize(name, params, attributes = {}, &blk) | ||
super() | ||
|
||
@name = name | ||
@params = params | ||
@attributes = attributes | ||
@blk = blk | ||
end | ||
|
||
def options | ||
Hash[form_name: @name, params: @params, verb: @verb] | ||
end | ||
|
||
def to_s | ||
if toplevel? | ||
_method_override! | ||
form(@blk, @attributes) | ||
end | ||
|
||
super | ||
end | ||
|
||
def fields_for(name) | ||
current_name = @name | ||
@name = _input_name(name) | ||
yield | ||
ensure | ||
@name = current_name | ||
end | ||
|
||
def label(content, attributes = {}, &blk) | ||
attributes = { for: _for(content, attributes.delete(:for)) }.merge(attributes) | ||
content = Utils::String.new(content).titleize | ||
|
||
super(content, attributes, &blk) | ||
end | ||
|
||
def color_field(name, attributes = {}) | ||
input _attributes(:color, name, attributes) | ||
end | ||
|
||
def date_field(name, attributes = {}) | ||
input _attributes(:date, name, attributes) | ||
end | ||
|
||
def datetime_field(name, attributes = {}) | ||
input _attributes(:datetime, name, attributes) | ||
end | ||
|
||
def datetime_local_field(name, attributes = {}) | ||
input _attributes(:'datetime-local', name, attributes) | ||
end | ||
|
||
def email_field(name, attributes = {}) | ||
input _attributes(:email, name, attributes) | ||
end | ||
|
||
def hidden_field(name, attributes = {}) | ||
input _attributes(:hidden, name, attributes) | ||
end | ||
|
||
def file_field(name, attributes = {}) | ||
attributes[:accept] = Array(attributes[:accept]).join(ACCEPT_SEPARATOR) if attributes.key?(:accept) | ||
attributes = { type: :file, name: _input_name(name), id: _input_id(name) }.merge(attributes) | ||
|
||
input(attributes) | ||
end | ||
|
||
def text_field(name, attributes = {}) | ||
input _attributes(:text, name, attributes) | ||
end | ||
alias_method :input_text, :text_field | ||
|
||
def radio_button(name, value, attributes = {}) | ||
attributes = { type: :radio, name: _input_name(name), value: value }.merge(attributes) | ||
attributes[:checked] = CHECKED if _value(name) == value | ||
input(attributes) | ||
end | ||
|
||
def select(name, values, attributes = {}) | ||
options = attributes.delete(:options) || {} | ||
attributes = { name: _input_name(name), id: _input_id(name) }.merge(attributes) | ||
|
||
super(attributes) do | ||
values.each do |value, content| | ||
if _value(name) == value | ||
option(content, {value: value, selected: SELECTED}.merge(options)) | ||
else | ||
option(content, {value: value}.merge(options)) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def submit(content, attributes = {}) | ||
attributes = { type: :submit }.merge(attributes) | ||
button(content, attributes) | ||
end | ||
|
||
private | ||
def toplevel? | ||
@attributes.any? | ||
end | ||
|
||
def _method_override! | ||
verb = (@attributes.fetch(:method) { DEFAULT_METHOD }).to_s.upcase | ||
|
||
if BROWSER_METHODS.include?(verb) | ||
@attributes[:method] = verb | ||
else | ||
@attributes[:method] = DEFAULT_METHOD | ||
@verb = verb | ||
end | ||
end | ||
|
||
def _attributes(type, name, attributes) | ||
{ type: type, name: _input_name(name), id: _input_id(name), value: _value(name) }.merge(attributes) | ||
end | ||
|
||
def _input_name(name) | ||
"#{ @name }[#{ name }]" | ||
end | ||
|
||
def _input_id(name) | ||
name = _input_name(name).gsub(/\[(?<token>[[[:word:]]\-]*)\]/, '-\k<token>') | ||
Utils::String.new(name).dasherize | ||
end | ||
|
||
def _value(name) | ||
name = _input_name(name).gsub(/\[(?<token>[[:word:]]*)\]/, '.\k<token>') | ||
@params.get(name) | ||
end | ||
|
||
def _for(content, name) | ||
_input_id(name || content) | ||
end | ||
end | ||
|
||
def form_for(name, url, attributes = {}, &blk) | ||
attributes = { action: url, id: "#{ name }-form", method: DEFAULT_METHOD }.merge(attributes) | ||
FormBuilder.new(name, params, attributes, &blk) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= album_form %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<%= | ||
form_for(:delivery, routes.deliveries, class: 'form-horizontal') do | ||
div class: 'form-group' do | ||
label :customer | ||
input_text :customer, class: 'form-control', placeholder: 'Customer', name: nil | ||
|
||
hidden_field :customer_id | ||
end | ||
|
||
fieldset do | ||
legend 'Address' | ||
|
||
fields_for :address do | ||
div class: 'form-group' do | ||
label :street | ||
input_text :street, class: 'form-control', placeholder: 'Street' | ||
end | ||
end | ||
end | ||
|
||
submit 'Create', class: 'btn btn-default' | ||
end | ||
%> |
Oops, something went wrong.