2009-11-18 23:50:43 +00:00
|
|
|
module SimpleForm
|
|
|
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
2009-12-10 13:57:05 +00:00
|
|
|
attr_reader :template, :object_name, :object, :attribute, :column,
|
|
|
|
:input_type, :options
|
2009-12-09 18:55:39 +00:00
|
|
|
|
2009-12-10 20:03:27 +00:00
|
|
|
# Creates a input with all components.
|
2009-11-19 21:26:16 +00:00
|
|
|
def input(attribute, options={})
|
2009-12-10 13:01:44 +00:00
|
|
|
@attribute, @options = attribute, options
|
2009-12-10 13:57:05 +00:00
|
|
|
@column = find_attribute_column
|
2009-12-10 13:01:44 +00:00
|
|
|
@input_type = default_input_type
|
2009-12-08 13:48:31 +00:00
|
|
|
|
2009-12-10 13:01:44 +00:00
|
|
|
component = SimpleForm.terminator
|
|
|
|
SimpleForm.components.reverse.each do |klass|
|
|
|
|
next if @options[klass.basename] == false
|
|
|
|
component = klass.new(self, component)
|
2009-12-09 19:41:20 +00:00
|
|
|
end
|
2009-12-10 13:01:44 +00:00
|
|
|
component.call
|
2009-11-19 21:26:16 +00:00
|
|
|
end
|
|
|
|
|
2009-12-10 20:03:27 +00:00
|
|
|
# Creates a button:
|
|
|
|
#
|
|
|
|
# form_for @user do |f|
|
|
|
|
# f.button :submit
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If the record is a new_record?, it will create a button with label "Create User",
|
|
|
|
# otherwise it will create with label "Update User". You can overwrite the label
|
|
|
|
# giving a second parameter or giving :label.
|
|
|
|
#
|
|
|
|
# f.button :submit, "Create a new user"
|
|
|
|
# f.button :submit, :label => "Create a new user"
|
|
|
|
#
|
|
|
|
# button is actually just a wrapper that adds a default text, that said, f.button
|
|
|
|
# above is just calling:
|
|
|
|
#
|
|
|
|
# submit_tag "Create a new user"
|
|
|
|
#
|
|
|
|
# All options given to button are given straight to submit_tag. That said, you can
|
|
|
|
# use :confirm normally:
|
|
|
|
#
|
|
|
|
# f.button :submit, :confirm => "Are you sure?"
|
|
|
|
#
|
|
|
|
# And if you want to use image_submit_tag, just give it as an option:
|
|
|
|
#
|
|
|
|
# f.button :image_submit, "/images/foo/bar.png"
|
|
|
|
#
|
|
|
|
# This comes with a bonus that any method added to your ApplicationController can
|
|
|
|
# be used by SimpleForm, as long as it ends with _tag. So is quite easy to customize
|
|
|
|
# your buttons.
|
|
|
|
#
|
|
|
|
def button(type, *args)
|
|
|
|
options = args.extract_options!
|
|
|
|
value = args.first || options.delete(:label)
|
|
|
|
|
|
|
|
value ||= begin
|
|
|
|
if @object
|
|
|
|
key = @object.new_record? ? :create : :update
|
|
|
|
model = @object.class.human_name if @object.class.respond_to?(:human_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
key ||= :submit
|
|
|
|
model ||= @object_name.to_s.humanize
|
|
|
|
|
|
|
|
I18n.t(:"simple_form.#{key}", :model => model, :default => "#{key.to_s.humanize} #{model}")
|
|
|
|
end
|
|
|
|
|
|
|
|
@template.send(:"#{type}_tag", value, options)
|
|
|
|
end
|
|
|
|
|
2009-12-10 01:57:05 +00:00
|
|
|
private
|
2009-12-08 16:49:14 +00:00
|
|
|
|
2009-12-10 13:01:44 +00:00
|
|
|
def default_input_type
|
|
|
|
return @options[:as].to_sym if @options[:as]
|
|
|
|
return :select if @options[:collection]
|
2009-12-10 01:11:57 +00:00
|
|
|
|
2009-12-10 13:47:07 +00:00
|
|
|
input_type = column.try(:type)
|
2009-12-10 00:55:42 +00:00
|
|
|
|
2009-12-10 01:57:05 +00:00
|
|
|
case input_type
|
|
|
|
when :timestamp
|
|
|
|
:datetime
|
|
|
|
when :string, nil
|
2009-12-10 13:23:48 +00:00
|
|
|
@attribute.to_s =~ /password/ ? :password : :string
|
2009-12-10 01:57:05 +00:00
|
|
|
else
|
|
|
|
input_type
|
2009-12-08 16:49:14 +00:00
|
|
|
end
|
2009-12-10 01:57:05 +00:00
|
|
|
end
|
2009-12-08 19:08:36 +00:00
|
|
|
|
2009-12-10 13:57:05 +00:00
|
|
|
def find_attribute_column
|
|
|
|
@object.column_for_attribute(@attribute) if @object.respond_to?(:column_for_attribute)
|
|
|
|
end
|
|
|
|
|
2009-11-18 23:50:43 +00:00
|
|
|
end
|
|
|
|
end
|